// License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.tools; import java.util.Objects; /** * A pair of objects. * @param Type of first item * @param Type of second item * @since 429 */ public final class Pair { /** * The first item */ public A a; /** * The second item */ public B b; /** * Constructs a new {@code Pair}. * @param a The first item * @param b The second item */ public Pair(A a, B b) { this.a = a; this.b = b; } @Override public int hashCode() { return Objects.hash(a, b); } @Override public boolean equals(Object other) { if (this == other) return true; if (other == null || getClass() != other.getClass()) return false; Pair pair = (Pair) other; return Objects.equals(a, pair.a) && Objects.equals(b, pair.b); } /** * Sorts a single-typed pair so {@code a <= b}. * @param type of both elements * @param p pair * @return {@code p} */ public static Pair sort(Pair p) { if (p.b.hashCode() < p.a.hashCode()) { T tmp = p.a; p.a = p.b; p.b = tmp; } return p; } @Override public String toString() { return '<'+a.toString()+','+b.toString()+'>'; } /** * Convenient constructor method * @param type of first item * @param type of second item * @param u The first item * @param v The second item * @return The newly created Pair(u,v) */ public static Pair create(U u, V v) { return new Pair<>(u, v); } }