diff --git a/src/main/groovy/lang/AbstractTuple.java b/src/main/groovy/lang/AbstractTuple.java new file mode 100644 index 0000000000..dfe1778bee --- /dev/null +++ b/src/main/groovy/lang/AbstractTuple.java @@ -0,0 +1,58 @@ +/* + * Copyright 2003-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package groovy.lang; + +import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation; + +import java.util.AbstractList; + +/** + * @since 2.4.0 + */ +public abstract class AbstractTuple extends AbstractList { + private int hashCode; + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || !(o instanceof AbstractTuple)) return false; + + AbstractTuple that = (AbstractTuple) o; + if (size() != that.size()) return false; + for (int i = 0; i < size(); i++) { + if (!DefaultTypeTransformation.compareEqual(get(i), that.get(i))) { + return false; + } + } + return true; + } + + @Override + public int hashCode() { + if (hashCode == 0) { + for (int i = 0; i < size(); i++) { + Object value = get(i); + int hash = (value != null) ? value.hashCode() : 0xbabe; + hashCode ^= hash; + } + if (hashCode == 0) { + hashCode = 0xbabe; + } + } + return hashCode; + } +} diff --git a/src/main/groovy/lang/Tuple.java b/src/main/groovy/lang/Tuple.java index 3e8bb8d74d..68159c1840 100644 --- a/src/main/groovy/lang/Tuple.java +++ b/src/main/groovy/lang/Tuple.java @@ -15,22 +15,18 @@ */ package groovy.lang; -import java.util.AbstractList; import java.util.List; -import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation; - /** * Represents a list of Objects. - * + * * @author James Strachan * @version $Revision$ */ -public class Tuple extends AbstractList { +public class Tuple extends AbstractTuple { private final Object[] contents; - private int hashCode; - public Tuple(Object[] contents) { + public Tuple(Object... contents) { if (contents == null) throw new NullPointerException(); this.contents = contents; } @@ -43,36 +39,6 @@ public int size() { return contents.length; } - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || !(o instanceof Tuple)) return false; - - Tuple that = (Tuple) o; - if (size() != that.size()) return false; - for (int i = 0; i < contents.length; i++) { - if (!DefaultTypeTransformation.compareEqual(contents[i], that.contents[i])) { - return false; - } - } - return true; - } - - @Override - public int hashCode() { - if (hashCode == 0) { - for (int i = 0; i < contents.length; i++ ) { - Object value = contents[i]; - int hash = (value != null) ? value.hashCode() : 0xbabe; - hashCode ^= hash; - } - if (hashCode == 0) { - hashCode = 0xbabe; - } - } - return hashCode; - } - @Override public List subList(int fromIndex, int toIndex) { int size = toIndex - fromIndex; diff --git a/src/main/groovy/lang/Tuple1.java b/src/main/groovy/lang/Tuple1.java new file mode 100644 index 0000000000..39d34450ba --- /dev/null +++ b/src/main/groovy/lang/Tuple1.java @@ -0,0 +1,47 @@ +/* + * Copyright 2003-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package groovy.lang; + +/** + * Represents a list of 1 typed Object. + * + * @since 2.4.0 + */ +public class Tuple1 extends AbstractTuple { + private final T1 first; + + public Tuple1(T1 first) { + this.first = first; + } + + public Object get(int index) { + switch (index) { + case 0: + return first; + default: + throw new IndexOutOfBoundsException(); + } + } + + public int size() { + return 1; + } + + public T1 getFirst() { + return first; + } +} diff --git a/src/main/groovy/lang/Tuple2.java b/src/main/groovy/lang/Tuple2.java index cefa363703..03ff7616a8 100644 --- a/src/main/groovy/lang/Tuple2.java +++ b/src/main/groovy/lang/Tuple2.java @@ -13,23 +13,43 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package groovy.lang; /** * Represents a list of 2 typed Objects. + * + * @since 2.4.0 */ -public class Tuple2 extends Tuple { +public class Tuple2 extends AbstractTuple { + private final T1 first; + private final T2 second; + public Tuple2(T1 first, T2 second) { - super(new Object[]{first, second}); + this.first = first; + this.second = second; + } + + public Object get(int index) { + switch (index) { + case 0: + return first; + case 1: + return second; + default: + throw new IndexOutOfBoundsException(); + } + } + + public int size() { + return 2; } - @SuppressWarnings("unchecked") public T1 getFirst() { - return (T1) get(0); + return first; } - @SuppressWarnings("unchecked") public T2 getSecond() { - return (T2) get(1); + return second; } } diff --git a/src/main/groovy/lang/Tuple3.java b/src/main/groovy/lang/Tuple3.java new file mode 100644 index 0000000000..538d768a05 --- /dev/null +++ b/src/main/groovy/lang/Tuple3.java @@ -0,0 +1,63 @@ +/* + * Copyright 2003-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package groovy.lang; + +/** + * Represents a list of 3 typed Objects. + * + * @since 2.4.0 + */ +public class Tuple3 extends AbstractTuple { + private final T1 first; + private final T2 second; + private final T3 third; + + public Tuple3(T1 first, T2 second, T3 third) { + this.first = first; + this.second = second; + this.third = third; + } + + public Object get(int index) { + switch (index) { + case 0: + return first; + case 1: + return second; + case 2: + return third; + default: + throw new IndexOutOfBoundsException(); + } + } + + public int size() { + return 3; + } + + public T1 getFirst() { + return first; + } + + public T2 getSecond() { + return second; + } + + public T3 getThird() { + return third; + } +} diff --git a/src/main/groovy/lang/Tuple4.java b/src/main/groovy/lang/Tuple4.java new file mode 100644 index 0000000000..06376cb1d5 --- /dev/null +++ b/src/main/groovy/lang/Tuple4.java @@ -0,0 +1,71 @@ +/* + * Copyright 2003-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package groovy.lang; + +/** + * Represents a list of 4 typed Objects. + * + * @since 2.4.0 + */ +public class Tuple4 extends AbstractTuple { + private final T1 first; + private final T2 second; + private final T3 third; + private final T4 fourth; + + public Tuple4(T1 first, T2 second, T3 third, T4 fourth) { + this.first = first; + this.second = second; + this.third = third; + this.fourth = fourth; + } + + public Object get(int index) { + switch (index) { + case 0: + return first; + case 1: + return second; + case 2: + return third; + case 3: + return fourth; + default: + throw new IndexOutOfBoundsException(); + } + } + + public int size() { + return 4; + } + + public T1 getFirst() { + return first; + } + + public T2 getSecond() { + return second; + } + + public T3 getThird() { + return third; + } + + public T4 getFourth() { + return fourth; + } +} diff --git a/src/main/groovy/lang/Tuple5.java b/src/main/groovy/lang/Tuple5.java new file mode 100644 index 0000000000..3ace26cd6c --- /dev/null +++ b/src/main/groovy/lang/Tuple5.java @@ -0,0 +1,79 @@ +/* + * Copyright 2003-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package groovy.lang; + +/** + * Represents a list of 5 typed Objects. + * + * @since 2.4.0 + */ +public class Tuple5 extends AbstractTuple { + private final T1 first; + private final T2 second; + private final T3 third; + private final T4 fourth; + private final T5 fifth; + + public Tuple5(T1 first, T2 second, T3 third, T4 fourth, T5 fifth) { + this.first = first; + this.second = second; + this.third = third; + this.fourth = fourth; + this.fifth = fifth; + } + + public Object get(int index) { + switch (index) { + case 0: + return first; + case 1: + return second; + case 2: + return third; + case 3: + return fourth; + case 4: + return fifth; + default: + throw new IndexOutOfBoundsException(); + } + } + + public int size() { + return 5; + } + + public T1 getFirst() { + return first; + } + + public T2 getSecond() { + return second; + } + + public T3 getThird() { + return third; + } + + public T4 getFourth() { + return fourth; + } + + public T5 getFifth() { + return fifth; + } +} diff --git a/src/main/groovy/lang/Tuple6.java b/src/main/groovy/lang/Tuple6.java new file mode 100644 index 0000000000..3c2bb89058 --- /dev/null +++ b/src/main/groovy/lang/Tuple6.java @@ -0,0 +1,87 @@ +/* + * Copyright 2003-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package groovy.lang; + +/** + * Represents a list of 6 typed Objects. + * + * @since 2.4.0 + */ +public class Tuple6 extends AbstractTuple { + private final T1 first; + private final T2 second; + private final T3 third; + private final T4 fourth; + private final T5 fifth; + private final T6 sixth; + + public Tuple6(T1 first, T2 second, T3 third, T4 fourth, T5 fifth, T6 sixth) { + this.first = first; + this.second = second; + this.third = third; + this.fourth = fourth; + this.fifth = fifth; + this.sixth = sixth; + } + + public Object get(int index) { + switch (index) { + case 0: + return first; + case 1: + return second; + case 2: + return third; + case 3: + return fourth; + case 4: + return fifth; + case 5: + return sixth; + default: + throw new IndexOutOfBoundsException(); + } + } + + public int size() { + return 6; + } + + public T1 getFirst() { + return first; + } + + public T2 getSecond() { + return second; + } + + public T3 getThird() { + return third; + } + + public T4 getFourth() { + return fourth; + } + + public T5 getFifth() { + return fifth; + } + + public T6 getSixth() { + return sixth; + } +} diff --git a/src/main/groovy/lang/Tuple7.java b/src/main/groovy/lang/Tuple7.java new file mode 100644 index 0000000000..854cf67f0a --- /dev/null +++ b/src/main/groovy/lang/Tuple7.java @@ -0,0 +1,95 @@ +/* + * Copyright 2003-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package groovy.lang; + +/** + * Represents a list of 7 typed Objects. + * + * @since 2.4.0 + */ +public class Tuple7 extends AbstractTuple { + private final T1 first; + private final T2 second; + private final T3 third; + private final T4 fourth; + private final T5 fifth; + private final T6 sixth; + private final T7 seventh; + + public Tuple7(T1 first, T2 second, T3 third, T4 fourth, T5 fifth, T6 sixth, T7 seventh) { + this.first = first; + this.second = second; + this.third = third; + this.fourth = fourth; + this.fifth = fifth; + this.sixth = sixth; + this.seventh = seventh; + } + + public Object get(int index) { + switch (index) { + case 0: + return first; + case 1: + return second; + case 2: + return third; + case 3: + return fourth; + case 4: + return fifth; + case 5: + return sixth; + case 6: + return seventh; + default: + throw new IndexOutOfBoundsException(); + } + } + + public int size() { + return 7; + } + + public T1 getFirst() { + return first; + } + + public T2 getSecond() { + return second; + } + + public T3 getThird() { + return third; + } + + public T4 getFourth() { + return fourth; + } + + public T5 getFifth() { + return fifth; + } + + public T6 getSixth() { + return sixth; + } + + public T7 getSeventh() { + return seventh; + } +} diff --git a/src/main/groovy/lang/Tuple8.java b/src/main/groovy/lang/Tuple8.java new file mode 100644 index 0000000000..585b3f8572 --- /dev/null +++ b/src/main/groovy/lang/Tuple8.java @@ -0,0 +1,103 @@ +/* + * Copyright 2003-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package groovy.lang; + +/** + * Represents a list of 8 typed Objects. + * + * @since 2.4.0 + */ +public class Tuple8 extends AbstractTuple { + private final T1 first; + private final T2 second; + private final T3 third; + private final T4 fourth; + private final T5 fifth; + private final T6 sixth; + private final T7 seventh; + private final T8 eighth; + + public Tuple8(T1 first, T2 second, T3 third, T4 fourth, T5 fifth, T6 sixth, T7 seventh, T8 eighth) { + this.first = first; + this.second = second; + this.third = third; + this.fourth = fourth; + this.fifth = fifth; + this.sixth = sixth; + this.seventh = seventh; + this.eighth = eighth; + } + + public Object get(int index) { + switch (index) { + case 0: + return first; + case 1: + return second; + case 2: + return third; + case 3: + return fourth; + case 4: + return fifth; + case 5: + return sixth; + case 6: + return seventh; + case 7: + return eighth; + default: + throw new IndexOutOfBoundsException(); + } + } + + public int size() { + return 8; + } + + public T1 getFirst() { + return first; + } + + public T2 getSecond() { + return second; + } + + public T3 getThird() { + return third; + } + + public T4 getFourth() { + return fourth; + } + + public T5 getFifth() { + return fifth; + } + + public T6 getSixth() { + return sixth; + } + + public T7 getSeventh() { + return seventh; + } + + public T8 getEighth() { + return eighth; + } +} diff --git a/src/main/groovy/lang/Tuple9.java b/src/main/groovy/lang/Tuple9.java new file mode 100644 index 0000000000..75b7446821 --- /dev/null +++ b/src/main/groovy/lang/Tuple9.java @@ -0,0 +1,111 @@ +/* + * Copyright 2003-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package groovy.lang; + +/** + * Represents a list of 9 typed Objects. + * + * @since 2.4.0 + */ +public class Tuple9 extends AbstractTuple { + private final T1 first; + private final T2 second; + private final T3 third; + private final T4 fourth; + private final T5 fifth; + private final T6 sixth; + private final T7 seventh; + private final T8 eighth; + private final T9 ninth; + + public Tuple9(T1 first, T2 second, T3 third, T4 fourth, T5 fifth, T6 sixth, T7 seventh, T8 eighth, T9 ninth) { + this.first = first; + this.second = second; + this.third = third; + this.fourth = fourth; + this.fifth = fifth; + this.sixth = sixth; + this.seventh = seventh; + this.eighth = eighth; + this.ninth = ninth; + } + + public Object get(int index) { + switch (index) { + case 0: + return first; + case 1: + return second; + case 2: + return third; + case 3: + return fourth; + case 4: + return fifth; + case 5: + return sixth; + case 6: + return seventh; + case 7: + return eighth; + case 8: + return ninth; + default: + throw new IndexOutOfBoundsException(); + } + } + + public int size() { + return 9; + } + + public T1 getFirst() { + return first; + } + + public T2 getSecond() { + return second; + } + + public T3 getThird() { + return third; + } + + public T4 getFourth() { + return fourth; + } + + public T5 getFifth() { + return fifth; + } + + public T6 getSixth() { + return sixth; + } + + public T7 getSeventh() { + return seventh; + } + + public T8 getEighth() { + return eighth; + } + + public T9 getNinth() { + return ninth; + } +}