-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pair.java
81 lines (69 loc) · 1.7 KB
/
Pair.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package fi.natroutter.natlibs.objects;
import java.util.Objects;
public class Pair<U, V> {
private final U first;
private final V second;
protected Pair(
final U first,
final V second
) {
this.first = first;
this.second = second;
}
/**
* Create a new 2-tuple
*
* @param first First value
* @param second Second value
* @param <U> First type
* @param <V> Second type
* @return Created pair
*/
public static <U, V> Pair<U, V> of(
final U first,
final V second
) {
return new Pair<>(first, second);
}
/**
* Get the first value
*
* @return First value
*/
public final U getFirst() {
return this.first;
}
/**
* Get the second value
*
* @return Second value
*/
public final V getSecond() {
return this.second;
}
@Override
public final boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Pair<?, ?> pair = (Pair<?, ?>) o;
return Objects.equals(getFirst(), pair.getFirst()) && Objects.equals(getSecond(), pair.getSecond());
}
@Override
public final int hashCode() {
return Objects.hash(getFirst(), getSecond());
}
@Override
public final String toString() {
return String.format("(%s, %s)", this.first, this.second);
}
public final Object[] toArray() {
final Object[] array = new Object[2];
array[0] = this.first;
array[1] = this.second;
return array;
}
}