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

Last change on this file since 8394 was 8338, checked in by Don-vip, 9 years ago

fix squid:S1319 - Declarations should use Java collection interfaces rather than specific implementation classes

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