62 lines
1.3 KiB
Java
62 lines
1.3 KiB
Java
import java.util.Objects;
|
|
import be.kuleuven.cs.som.annotate.*;
|
|
|
|
|
|
/**
|
|
* A tuple (pair) implementation
|
|
*
|
|
* @param <T1> first element type
|
|
* @param <T2> second element type
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author Arthur Bols & Leen Dereu
|
|
*/
|
|
@Value
|
|
public class Tuple<T1, T2> {
|
|
|
|
|
|
public final T1 item1;
|
|
public final T2 item2;
|
|
|
|
|
|
/**
|
|
*
|
|
* @param item1
|
|
* The first element for this tuple
|
|
* @param item2
|
|
* The second element for this tuple
|
|
*/
|
|
private Tuple(T1 item1, T2 item2) {
|
|
|
|
if (item1 == null)
|
|
throw new IllegalArgumentException("item1 may not be null");
|
|
if (item2 == null)
|
|
throw new IllegalArgumentException("item2 may not be null");
|
|
|
|
this.item1 = item1;
|
|
this.item2 = item2;
|
|
}
|
|
|
|
@Immutable
|
|
public static <T1, T2> Tuple<T1, T2> create(T1 item1, T2 item2){
|
|
return new Tuple<>(item1, item2);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object o) {
|
|
if (this == o) return true;
|
|
if (!(o instanceof Tuple)) return false;
|
|
Tuple<?, ?> tuple = (Tuple<?, ?>) o;
|
|
return Objects.equals(item1, tuple.item1) &&
|
|
Objects.equals(item2, tuple.item2);
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
|
|
return Objects.hash(item1, item2);
|
|
}
|
|
|
|
}
|