// License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.tools; import java.util.ArrayList; /** * 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 a.hashCode() + b.hashCode(); } @Override public boolean equals(Object other) { if (other instanceof Pair) { Pair o = (Pair)other; return a.equals(o.a) && b.equals(o.b); } else return false; } public static ArrayList toArrayList(Pair p) { ArrayList l = new ArrayList<>(2); l.add(p.a); l.add(p.b); return l; } 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+","+b+">"; } /** * Convenient constructor method * @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); } }