1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 | import java.util.ArrayList;
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * A pair of objects.
|
---|
7 | * @param <A> Type of first item
|
---|
8 | * @param <B> Type of second item
|
---|
9 | * @since 429
|
---|
10 | */
|
---|
11 | public final class Pair<A,B> {
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * The first item
|
---|
15 | */
|
---|
16 | public A a;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * The second item
|
---|
20 | */
|
---|
21 | public B b;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Constructs a new {@code Pair}.
|
---|
25 | * @param a The first item
|
---|
26 | * @param b The second item
|
---|
27 | */
|
---|
28 | public Pair(A a, B b) {
|
---|
29 | this.a = a;
|
---|
30 | this.b = b;
|
---|
31 | }
|
---|
32 |
|
---|
33 | @Override public int hashCode() {
|
---|
34 | return a.hashCode() + b.hashCode();
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override public boolean equals(Object other) {
|
---|
38 | if (other instanceof Pair<?, ?>) {
|
---|
39 | Pair<?, ?> o = (Pair<?, ?>)other;
|
---|
40 | return a.equals(o.a) && b.equals(o.b);
|
---|
41 | } else
|
---|
42 | return false;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public static <T> ArrayList<T> toArrayList(Pair<T, T> p) {
|
---|
46 | ArrayList<T> l = new ArrayList<>(2);
|
---|
47 | l.add(p.a);
|
---|
48 | l.add(p.b);
|
---|
49 | return l;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public static <T> Pair<T,T> sort(Pair<T,T> p) {
|
---|
53 | if (p.b.hashCode() < p.a.hashCode()) {
|
---|
54 | T tmp = p.a;
|
---|
55 | p.a = p.b;
|
---|
56 | p.b = tmp;
|
---|
57 | }
|
---|
58 | return p;
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public String toString() {
|
---|
63 | return "<"+a+","+b+">";
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Convenient constructor method
|
---|
68 | * @param u The first item
|
---|
69 | * @param v The second item
|
---|
70 | * @return The newly created Pair(u,v)
|
---|
71 | */
|
---|
72 | public static <U,V> Pair<U,V> create(U u, V v) {
|
---|
73 | return new Pair<>(u,v);
|
---|
74 | }
|
---|
75 | }
|
---|