Skip to content

Commit 0dd461d

Browse files
yukobadaniellansun
authored andcommitted
Field version of Tuple1, Tuple3, ... , Tuple9
1 parent a86a4f1 commit 0dd461d

11 files changed

+786
-42
lines changed
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package groovy.lang;
21+
22+
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
23+
24+
import java.util.AbstractList;
25+
26+
/**
27+
* @since 2.5.0
28+
*/
29+
public abstract class AbstractTuple<E> extends AbstractList<E> {
30+
private int hashCode;
31+
32+
@Override
33+
public boolean equals(Object o) {
34+
if (this == o) return true;
35+
if (o == null || !(o instanceof AbstractTuple)) return false;
36+
37+
AbstractTuple that = (AbstractTuple) o;
38+
if (size() != that.size()) return false;
39+
for (int i = 0; i < size(); i++) {
40+
if (!DefaultTypeTransformation.compareEqual(get(i), that.get(i))) {
41+
return false;
42+
}
43+
}
44+
return true;
45+
}
46+
47+
@Override
48+
public int hashCode() {
49+
if (hashCode == 0) {
50+
for (int i = 0; i < size(); i++) {
51+
Object value = get(i);
52+
int hash = (value != null) ? value.hashCode() : 0xbabe;
53+
hashCode ^= hash;
54+
}
55+
if (hashCode == 0) {
56+
hashCode = 0xbabe;
57+
}
58+
}
59+
return hashCode;
60+
}
61+
}

src/main/groovy/lang/Tuple.java

+4-36
Original file line numberDiff line numberDiff line change
@@ -18,63 +18,31 @@
1818
*/
1919
package groovy.lang;
2020

21-
import java.util.AbstractList;
2221
import java.util.List;
2322

24-
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
25-
2623
/**
2724
* Represents a list of Objects.
2825
*
2926
* @author <a href="mailto:[email protected]">James Strachan</a>
3027
*/
31-
public class Tuple extends AbstractList {
28+
public class Tuple extends AbstractTuple {
3229
private final Object[] contents;
33-
private int hashCode;
3430

35-
public Tuple(Object[] contents) {
31+
public Tuple(Object... contents) {
3632
if (contents == null) throw new NullPointerException();
3733
this.contents = contents;
3834
}
3935

36+
@Override
4037
public Object get(int index) {
4138
return contents[index];
4239
}
4340

41+
@Override
4442
public int size() {
4543
return contents.length;
4644
}
4745

48-
@Override
49-
public boolean equals(Object o) {
50-
if (this == o) return true;
51-
if (o == null || !(o instanceof Tuple)) return false;
52-
53-
Tuple that = (Tuple) o;
54-
if (size() != that.size()) return false;
55-
for (int i = 0; i < contents.length; i++) {
56-
if (!DefaultTypeTransformation.compareEqual(contents[i], that.contents[i])) {
57-
return false;
58-
}
59-
}
60-
return true;
61-
}
62-
63-
@Override
64-
public int hashCode() {
65-
if (hashCode == 0) {
66-
for (int i = 0; i < contents.length; i++ ) {
67-
Object value = contents[i];
68-
int hash = (value != null) ? value.hashCode() : 0xbabe;
69-
hashCode ^= hash;
70-
}
71-
if (hashCode == 0) {
72-
hashCode = 0xbabe;
73-
}
74-
}
75-
return hashCode;
76-
}
77-
7846
@Override
7947
public List subList(int fromIndex, int toIndex) {
8048
int size = toIndex - fromIndex;

src/main/groovy/lang/Tuple1.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package groovy.lang;
20+
21+
/**
22+
* Represents a list of 1 typed Object.
23+
*
24+
* @since 2.5.0
25+
*/
26+
public class Tuple1<T1> extends AbstractTuple {
27+
private final T1 first;
28+
29+
public Tuple1(T1 first) {
30+
this.first = first;
31+
}
32+
33+
@Override
34+
public Object get(int index) {
35+
switch (index) {
36+
case 0:
37+
return first;
38+
default:
39+
throw new IndexOutOfBoundsException("index: " + index);
40+
}
41+
}
42+
43+
@Override
44+
public int size() {
45+
return 1;
46+
}
47+
48+
public T1 getFirst() {
49+
return first;
50+
}
51+
}

src/main/groovy/lang/Tuple2.java

+25-6
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,37 @@
2121
/**
2222
* Represents a list of 2 typed Objects.
2323
*/
24-
public class Tuple2<T1, T2> extends Tuple {
24+
public class Tuple2<T1, T2> extends AbstractTuple {
25+
private final T1 first;
26+
private final T2 second;
27+
2528
public Tuple2(T1 first, T2 second) {
26-
super(new Object[]{first, second});
29+
this.first = first;
30+
this.second = second;
31+
}
32+
33+
@Override
34+
public Object get(int index) {
35+
switch (index) {
36+
case 0:
37+
return first;
38+
case 1:
39+
return second;
40+
default:
41+
throw new IndexOutOfBoundsException("index: " + index);
42+
}
43+
}
44+
45+
@Override
46+
public int size() {
47+
return 2;
2748
}
2849

29-
@SuppressWarnings("unchecked")
3050
public T1 getFirst() {
31-
return (T1) get(0);
51+
return first;
3252
}
3353

34-
@SuppressWarnings("unchecked")
3554
public T2 getSecond() {
36-
return (T2) get(1);
55+
return second;
3756
}
3857
}

src/main/groovy/lang/Tuple3.java

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package groovy.lang;
21+
22+
/**
23+
* Represents a list of 3 typed Objects.
24+
*
25+
* @since 2.5.0
26+
*/
27+
public class Tuple3<T1, T2, T3> extends AbstractTuple {
28+
private final T1 first;
29+
private final T2 second;
30+
private final T3 third;
31+
32+
public Tuple3(T1 first, T2 second, T3 third) {
33+
this.first = first;
34+
this.second = second;
35+
this.third = third;
36+
}
37+
38+
@Override
39+
public Object get(int index) {
40+
switch (index) {
41+
case 0:
42+
return first;
43+
case 1:
44+
return second;
45+
case 2:
46+
return third;
47+
default:
48+
throw new IndexOutOfBoundsException("index: " + index);
49+
}
50+
}
51+
52+
@Override
53+
public int size() {
54+
return 3;
55+
}
56+
57+
public T1 getFirst() {
58+
return first;
59+
}
60+
61+
public T2 getSecond() {
62+
return second;
63+
}
64+
65+
public T3 getThird() {
66+
return third;
67+
}
68+
}

src/main/groovy/lang/Tuple4.java

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package groovy.lang;
21+
22+
/**
23+
* Represents a list of 4 typed Objects.
24+
*
25+
* @since 2.5.0
26+
*/
27+
public class Tuple4<T1, T2, T3, T4> extends AbstractTuple {
28+
private final T1 first;
29+
private final T2 second;
30+
private final T3 third;
31+
private final T4 fourth;
32+
33+
public Tuple4(T1 first, T2 second, T3 third, T4 fourth) {
34+
this.first = first;
35+
this.second = second;
36+
this.third = third;
37+
this.fourth = fourth;
38+
}
39+
40+
@Override
41+
public Object get(int index) {
42+
switch (index) {
43+
case 0:
44+
return first;
45+
case 1:
46+
return second;
47+
case 2:
48+
return third;
49+
case 3:
50+
return fourth;
51+
default:
52+
throw new IndexOutOfBoundsException("index: " + index);
53+
}
54+
}
55+
56+
@Override
57+
public int size() {
58+
return 4;
59+
}
60+
61+
public T1 getFirst() {
62+
return first;
63+
}
64+
65+
public T2 getSecond() {
66+
return second;
67+
}
68+
69+
public T3 getThird() {
70+
return third;
71+
}
72+
73+
public T4 getFourth() {
74+
return fourth;
75+
}
76+
}

0 commit comments

Comments
 (0)