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

Last change on this file since 9697 was 9542, checked in by Don-vip, 8 years ago

javadoc

  • Property svn:eol-style set to native
File size: 2.1 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 /**
50 * Converts a single-typed pair to a list.
51 * @param <T> type of both elements
52 * @param p pair
53 * @return list containing a and b
54 */
55 public static <T> List<T> toList(Pair<T, T> p) {
56 List<T> l = new ArrayList<>(2);
57 l.add(p.a);
58 l.add(p.b);
59 return l;
60 }
61
62 /**
63 * Sorts a single-typed pair so {@code a <= b}.
64 * @param <T> type of both elements
65 * @param p pair
66 * @return {@code p}
67 */
68 public static <T> Pair<T, T> sort(Pair<T, T> p) {
69 if (p.b.hashCode() < p.a.hashCode()) {
70 T tmp = p.a;
71 p.a = p.b;
72 p.b = tmp;
73 }
74 return p;
75 }
76
77 @Override
78 public String toString() {
79 return "<"+a+','+b+'>';
80 }
81
82 /**
83 * Convenient constructor method
84 * @param <U> type of first item
85 * @param <V> type of second item
86 * @param u The first item
87 * @param v The second item
88 * @return The newly created Pair(u,v)
89 */
90 public static <U, V> Pair<U, V> create(U u, V v) {
91 return new Pair<>(u, v);
92 }
93}
Note: See TracBrowser for help on using the repository browser.