3 import java.util.Iterator;
5 import java.util.Objects;
8 * A generic immutable pair of values.
17 public class Pair<T, U> implements Iterable<Object>
23 public Pair(T val0, U val1)
30 * Return value at the specified index cast to type R
31 * @param <R> return type
32 * @param index item index
33 * @return value at given index
34 * @throws ClassCastException value cannot be cast to R
35 * @throws IndexOutOfBoundsException index outside tuple size
37 @SuppressWarnings("unchecked")
38 public <R> R get(int index) throws ClassCastException, IndexOutOfBoundsException
44 throw new IndexOutOfBoundsException(index);
48 * @return 0th value of the pair
56 * @return 1st value of the pair
72 public boolean equals(Object obj)
74 if (obj instanceof Pair)
76 Pair<?, ?> other = (Pair<?, ?>) obj;
77 return Objects.equals(val0, other.val0) &&
78 Objects.equals(val1, other.val1);
84 public Iterator<Object> iterator()
86 return List.of(val0, val1).iterator();