source: josm/trunk/src/org/openstreetmap/josm/tools/Pair.java@ 9396

Last change on this file since 9396 was 9371, checked in by simon04, 8 years ago

Java 7: use Objects.equals and Objects.hash

  • Property svn:eol-style set to native
File size: 1.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Objects;
6
7/**
8 * A pair of objects.
9 * @param <A> Type of first item
10 * @param <B> Type of second item
11 * @since 429
12 */
13public final class Pair<A, B> {
14
15 /**
16 * The first item
17 */
18 public A a;
19
20 /**
21 * The second item
22 */
23 public B b;
24
25 /**
26 * Constructs a new {@code Pair}.
27 * @param a The first item
28 * @param b The second item
29 */
30 public Pair(A a, B b) {
31 this.a = a;
32 this.b = b;
33 }
34
35 @Override
36 public int hashCode() {
37 return Objects.hash(a, b);
38 }
39
40 @Override
41 public boolean equals(Object other) {
42 if (this == other) return true;
43 if (other == null || getClass() != other.getClass()) return false;
44 Pair<?, ?> pair = (Pair<?, ?>) other;
45 return Objects.equals(a, pair.a) &&
46 Objects.equals(b, pair.b);
47 }
48
49 public static <T> List<T> toList(Pair<T, T> p) {
50 List<T> l = new ArrayList<>(2);
51 l.add(p.a);
52 l.add(p.b);
53 return l;
54 }
55
56 public static <T> Pair<T, T> sort(Pair<T, T> p) {
57 if (p.b.hashCode() < p.a.hashCode()) {
58 T tmp = p.a;
59 p.a = p.b;
60 p.b = tmp;
61 }
62 return p;
63 }
64
65 @Override
66 public String toString() {
67 return "<"+a+','+b+'>';
68 }
69
70 /**
71 * Convenient constructor method
72 * @param <U> type of first item
73 * @param <V> type of second item
74 * @param u The first item
75 * @param v The second item
76 * @return The newly created Pair(u,v)
77 */
78 public static <U, V> Pair<U, V> create(U u, V v) {
79 return new Pair<>(u, v);
80 }
81}
Note: See TracBrowser for help on using the repository browser.