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

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

sonar - Performance - Method passes constant String of length 1 to character overridden method + add unit tests/javadoc

  • 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.Objects;
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 */
11public 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
34 public int hashCode() {
35 return Objects.hash(a, b);
36 }
37
38 @Override
39 public boolean equals(Object other) {
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) &&
44 Objects.equals(b, pair.b);
45 }
46
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 */
53 public static <T> Pair<T, T> sort(Pair<T, T> p) {
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 }
61
62 @Override
63 public String toString() {
64 return '<'+a.toString()+','+b.toString()+'>';
65 }
66
67 /**
68 * Convenient constructor method
69 * @param <U> type of first item
70 * @param <V> type of second item
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.