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