-
Notifications
You must be signed in to change notification settings - Fork 20
/
ComparableTuple.java
132 lines (121 loc) · 4.04 KB
/
ComparableTuple.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.kernel.commons;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Provides a tuple of values where the key is used as comparator.
* <p>
* Subclasses {@link Tuple} to implement <tt>Comparable</tt> based on the key, that is the first element of the tuple.
*
* @param <F> defines the first type of the tuple. The supplied class must implement {@code Comparable}
* @param <S> defines the second type of the tuple
* @see Tuple
* @see Comparable
*/
public class ComparableTuple<F extends Comparable<F>, S> extends Tuple<F, S>
implements Comparable<ComparableTuple<F, S>> {
protected ComparableTuple(F first, S second) {
super(first, second);
}
/**
* Creates a new tuple without any values.
*
* @param <F> the type for the first value
* @param <S> the type for the second value
* @return the newly created tuple
*/
@Nonnull
public static <F extends Comparable<F>, S> ComparableTuple<F, S> createTuple() {
return new ComparableTuple<>(null, null);
}
/**
* Creates a new tuple by only specifying the first value of the tuple.
* <p>
* The second value will remain <tt>null</tt>.
*
* @param first defines the first value of the tuple
* @param <F> the type for the first value
* @param <S> the type for the second value
* @return the newly created tuple
*/
@Nonnull
public static <F extends Comparable<F>, S> ComparableTuple<F, S> create(@Nullable F first) {
return new ComparableTuple<>(first, null);
}
/**
* Creates a new tuple with the given values.
*
* @param first defines the first value of the tuple
* @param second defines the second value of the tuple
* @param <F> the type for the first value
* @param <S> the type for the second value
* @return the newly created tuple
*/
@Nonnull
public static <F extends Comparable<F>, S> ComparableTuple<F, S> create(@Nullable F first, @Nullable S second) {
return new ComparableTuple<>(first, second);
}
/**
* Converts a map into a list of tuples.
*
* @param map the map to be converted
* @param <K> the key type of the map and therefore the type of the first component of the tuples
* @param <V> the value type of the map and therefore the type of the second component of the tuples
* @return a list of tuples, containing one tuple per map entry where the first component is the key,
* and the second component is the value of the map entry.
*/
public static <K extends Comparable<K>, V> List<ComparableTuple<K, V>> fromComparableMap(@Nonnull Map<K, V> map) {
List<ComparableTuple<K, V>> result = new ArrayList<>(map.size());
for (Map.Entry<K, V> e : map.entrySet()) {
result.add(new ComparableTuple<>(e.getKey(), e.getValue()));
}
return result;
}
@Override
public int compareTo(ComparableTuple<F, S> o) {
if (o == null) {
return 1;
}
if (o.getFirst() == null) {
if (getFirst() != null) {
return 1;
} else {
return 0;
}
}
if (getFirst() == null) {
return -1;
}
return getFirst().compareTo(o.getFirst());
}
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (!(obj instanceof ComparableTuple<?, ?>)) {
return false;
}
return compareTo((ComparableTuple<F, S>) obj) == 0;
}
@Override
public int hashCode() {
if (getFirst() == null) {
return 0;
}
return getFirst().hashCode();
}
}