/
Sets.java
141 lines (113 loc) · 3.46 KB
/
Sets.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
133
134
135
136
137
138
139
140
141
package org.javers.common.collections;
import org.javers.common.validation.Validate;
import java.util.*;
import java.util.Collections;
import java.util.function.Function;
import java.util.stream.Collector;
import static java.util.Arrays.asList;
import static java.util.Collections.EMPTY_SET;
/**
* @author Maciej Zasada
*/
public class Sets {
private Sets() {
}
public static Set wrapNull(Object set) {
if (set == null) {
return Collections.emptySet();
}
return (Set) set;
}
/**
* null args are allowed
*/
public static <E> Set<E> intersection(Set<E> first, Set<E> second) {
if (first == null || second == null) {
return EMPTY_SET;
}
Set<E> intersection = new HashSet<>();
for (E e : first) {
if (second.contains(e)) {
intersection.add(e);
}
}
return intersection;
}
public static <E> Set<E> xor(Set<E> first, Set<E> second) {
Set<E> xor = difference(first, second);
xor.addAll(difference(second, first));
return xor;
}
/**
* null args are allowed
*/
public static <E> Set<E> difference(Set<E> first, Set<E> second) {
if (first == null || first.size() == 0) {
return Collections.emptySet();
}
if (second == null || second.size() == 0) {
return first;
}
Set<E> difference = new HashSet<>(first);
difference.removeAll(second);
return difference;
}
/**
* null args are allowed
*/
public static <E> Collection<E> difference(Set<E> first, Set<E> second, Function<E, Integer> hasher) {
if (first == null || first.size() == 0) {
return Collections.emptyList();
}
if (second == null || second.size() == 0) {
return first;
}
Map<Integer, E> map = new HashMap();
first.stream().forEach(e -> map.put(hasher.apply(e), e));
second.stream().forEach(e -> map.remove(hasher.apply(e)));
return Collections.unmodifiableCollection(map.values());
}
public static <E> Set<E> asSet(E... elements) {
return asSet(asList(elements));
}
public static <E> Set<E> asSet(Collection<E> elements) {
if (elements == null) {
return Collections.emptySet();
}
return Collections.unmodifiableSet(new HashSet<>(elements));
}
/**
* @return ImmutableSet
*/
public static <F, T> Set<T> transform(Set<F> input, Function<F, T> transformation) {
Validate.argumentsAreNotNull(input, transformation);
return input.stream().map(transformation::apply).collect(toImmutableSet());
}
private static <E> Set<E> nullSafe(Set<E> set) {
if (set == null) {
return EMPTY_SET;
}
return set;
}
/**
* @return index -> value
*/
public static <T> Map<Integer, T> asMap(Set<T> input) {
if (input == null){
return null;
}
Map<Integer, T> result = new HashMap<>();
int i = 0;
for (T element : input) {
result.put(i, element);
i++;
}
return result;
}
public static <t> Collector<t, Set<t>, Set<t>> toImmutableSet() {
return Collector.of(HashSet::new, Set::add, (left, right) -> {
left.addAll(right);
return left;
}, Collections::unmodifiableSet);
}
}