001
002package gudusoft.gsqlparser.dlineage.util;
003
004/**
005 * Container to ease passing around a tuple of two objects. This object provides
006 * a sensible implementation of equals(), returning true if equals() is true on
007 * each of the contained objects.
008 */
009public class Pair<F, S> {
010
011    public final F first;
012    public final S second;
013
014    /**
015     * Constructor for a Pair.
016     *
017     * @param first  the first object in the Pair
018     * @param second the second object in the pair
019     */
020    public Pair(F first, S second) {
021        this.first = first;
022        this.second = second;
023    }
024
025    /**
026     * Checks the two objects for equality by delegating to their respective
027     * {@link Object#equals(Object)} methods.
028     *
029     * @param o the {@link Pair} to which this one is to be checked for
030     *          equality
031     * @return true if the underlying objects of the Pair are both considered
032     * equal
033     */
034    @Override
035    public boolean equals(Object o) {
036        if (!(o instanceof Pair)) {
037            return false;
038        }
039        Pair<?, ?> p = (Pair<?, ?>) o;
040        return Objects.equal(p.first, first)
041                && Objects.equal(p.second, second);
042    }
043
044    /**
045     * Compute a hash code using the hash codes of the underlying objects
046     *
047     * @return a hashcode of the Pair
048     */
049    @Override
050    public int hashCode() {
051        return (first == null ? 0 : first.hashCode())
052                ^ (second == null ? 0 : second.hashCode());
053    }
054
055    /**
056     * Convenience method for creating an appropriately typed pair.
057     *
058     * @param a the first object in the Pair
059     * @param b the second object in the pair
060     * @return a Pair that is templatized with the types of a and b
061     */
062    public static <A, B> Pair<A, B> create(A a, B b) {
063        return new Pair<A, B>(a, b);
064    }
065
066    public String toString() {
067        return "[" + first + "," + second + "]";
068    }
069}