groovy.lang.Tuple Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of groovy Show documentation
Show all versions of groovy Show documentation
Groovy: A powerful multi-faceted language for the JVM
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.io.Serializable;
import java.util.AbstractList;
import java.util.List;
import java.util.Objects;
/**
* Represents a list of Objects.
*/
public class Tuple extends AbstractList implements Serializable, Cloneable, Comparable> {
private static final long serialVersionUID = -6707770506387821031L;
private final E[] contents;
@SafeVarargs
public Tuple(E... contents) {
if (contents == null) throw new NullPointerException();
this.contents = contents;
}
public Tuple(Tuple tuple) {
this.contents = tuple.contents;
}
@Override
public E get(int index) {
return contents[index];
}
@Override
public int size() {
return contents.length;
}
@Override
public E[] toArray() {
return contents;
}
@SuppressWarnings("unchecked")
@Override
public List subList(int fromIndex, int toIndex) {
int size = toIndex - fromIndex;
E[] newContent = (E[]) new Object[size];
System.arraycopy(contents, fromIndex, newContent, 0, size);
return new Tuple<>(newContent);
}
public Tuple subTuple(int fromIndex, int toIndex) {
return (Tuple) subList(fromIndex, toIndex);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Tuple)) return false;
Tuple that = (Tuple) o;
int size = size();
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() {
return Objects.hash(contents);
}
@Override
public int compareTo(Tuple other) {
int thisSize = this.size();
int otherSize = other.size();
for (int i = 0, n = Math.min(thisSize, otherSize); i < n; i++) {
int result = compare(this.get(i), other.get(i));
if (result != 0) {
return result;
}
}
return Integer.compare(thisSize, otherSize);
}
@SuppressWarnings("unchecked")
private static int compare(T t1, T t2) {
return t1 == null && t2 == null
? 0
: t1 == null
? 1
: t2 == null
? -1
: t1 instanceof Comparable
? ((Comparable) t1).compareTo(t2)
: DefaultTypeTransformation.compareEqual(t1, t2)
? 0
: 1;
}
/**
* Construct a tuple of degree 0.
*/
public static Tuple0 tuple() {
return Tuple0.INSTANCE;
}
/**
* Construct a tuple of degree 1.
*/
public static Tuple1 tuple(T1 v1) {
return new Tuple1<>(v1);
}
/**
* Construct a tuple of degree 2.
*/
public static Tuple2 tuple(T1 v1, T2 v2) {
return new Tuple2<>(v1, v2);
}
/**
* Construct a tuple of degree 3.
*/
public static Tuple3 tuple(T1 v1, T2 v2, T3 v3) {
return new Tuple3<>(v1, v2, v3);
}
/**
* Construct a tuple of degree 4.
*/
public static Tuple4 tuple(T1 v1, T2 v2, T3 v3, T4 v4) {
return new Tuple4<>(v1, v2, v3, v4);
}
/**
* Construct a tuple of degree 5.
*/
public static Tuple5 tuple(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5) {
return new Tuple5<>(v1, v2, v3, v4, v5);
}
/**
* Construct a tuple of degree 6.
*/
public static Tuple6 tuple(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6) {
return new Tuple6<>(v1, v2, v3, v4, v5, v6);
}
/**
* Construct a tuple of degree 7.
*/
public static Tuple7 tuple(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7) {
return new Tuple7<>(v1, v2, v3, v4, v5, v6, v7);
}
/**
* Construct a tuple of degree 8.
*/
public static Tuple8 tuple(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8) {
return new Tuple8<>(v1, v2, v3, v4, v5, v6, v7, v8);
}
/**
* Construct a tuple of degree 9.
*/
public static Tuple9 tuple(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9) {
return new Tuple9<>(v1, v2, v3, v4, v5, v6, v7, v8, v9);
}
/**
* Construct a tuple of degree 10.
*/
public static Tuple10 tuple(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10) {
return new Tuple10<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10);
}
/**
* Construct a tuple of degree 11.
*/
public static Tuple11 tuple(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11) {
return new Tuple11<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11);
}
/**
* Construct a tuple of degree 12.
*/
public static Tuple12 tuple(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12) {
return new Tuple12<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12);
}
/**
* Construct a tuple of degree 13.
*/
public static Tuple13 tuple(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13) {
return new Tuple13<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13);
}
/**
* Construct a tuple of degree 14.
*/
public static Tuple14 tuple(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14) {
return new Tuple14<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14);
}
/**
* Construct a tuple of degree 15.
*/
public static Tuple15 tuple(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15) {
return new Tuple15<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15);
}
/**
* Construct a tuple of degree 16.
*/
public static Tuple16 tuple(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16) {
return new Tuple16<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16);
}
@Override
public Tuple clone() {
return new Tuple<>(this);
}
}