-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionUtils.java
More file actions
231 lines (180 loc) · 7.22 KB
/
Copy pathCollectionUtils.java
File metadata and controls
231 lines (180 loc) · 7.22 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package ru.cwcode.cwutils.collections;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.cwcode.cwutils.numbers.Rand;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class CollectionUtils {
public static <K, V> @NotNull V getValueOrDefault(@Nullable Map<K, V> map, @Nullable K type, @NotNull V def) {
if (map == null || type == null) return def;
V v = map.get(type);
if (v == null) return def;
return v;
}
public static <K extends Enum<K>, V> EnumMap<K, V> toEnumMap(Class<K> type, Map<K, V> map) {
if (map == null) return null;
EnumMap<K, V> enumMap = new EnumMap<>(type);
enumMap.putAll(map);
return enumMap;
}
public static <K, V, tK, tV, M extends Map<tK, tV>> M transformMap(Map<K, V> map,
Function<K, tK> keyTransformer,
Function<V, tV> valueTransformer,
Supplier<M> mapCreator) {
M result = mapCreator.get();
for (Map.Entry<K, V> x : map.entrySet()) {
result.put(keyTransformer.apply(x.getKey()), valueTransformer.apply(x.getValue()));
}
return result;
}
public static <K, V, tK, tV> HashMap<tK, tV> transformMap(Map<K, V> map,
Function<K, tK> keyTransformer,
Function<V, tV> valueTransformer) {
return transformMap(map, keyTransformer, valueTransformer, HashMap::new);
}
public static <K, V, tV> HashMap<K, tV> transformValue(Map<K, V> map,
Function<V, tV> valueTransformer) {
return transformMap(map, k -> k, valueTransformer, HashMap::new);
}
public static <K, V, tK> HashMap<tK, V> transformKey(Map<K, V> map,
Function<K, tK> keyTransformer) {
return transformMap(map, keyTransformer, v -> v, HashMap::new);
}
public static <T> @Nullable T getRandomListEntry(List<T> list) {
if (list == null) return null;
if (list.isEmpty()) return null;
if (list.size() == 1) return list.get(0);
return list.get(Rand.ofInt(list.size()));
}
@SafeVarargs
public static <T> T getRandomArrayEntry(T... values) {
if (values.length == 0) return null;
if (values.length == 1) return values[0];
return values[Rand.ofInt(values.length)];
}
@Deprecated
public static void shuffleArray(int[] ar) {
for (int i = ar.length - 1; i > 0; i--) {
int index = Rand.ofInt(i + 1);
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
public static <T> T[] shuffleArray(T[] array) {
for (int i = array.length - 1; i > 0; --i) {
int index = Rand.ofInt(i + 1);
T a = array[index];
array[index] = array[i];
array[i] = a;
}
return array;
}
@SafeVarargs
public static <T> Collection<T> combine(Collection<T>... lists) {
int size = 0;
for (Collection<T> list : lists) {
size += list.size();
}
List<T> combined = new ArrayList<>(size);
for (Collection<T> list : lists) {
combined.addAll(list);
}
return combined;
}
/**
* Используйте HashSet для параметров, если коллекции достаточно большие
*/
public static <T> boolean containsAny(Collection<T> one, Collection<T> two) {
for (T t : one) {
if (two.contains(t)) return true;
}
return false;
}
/**
* Используйте HashSet для параметров, если коллекции достаточно большие
*/
public static <T> boolean hasAllElements(Collection<T> toCheck, Collection<T> elements) {
for (T t : elements) {
if (!toCheck.contains(t)) return false;
}
return true;
}
public static String toString(Object[] values) {
return getStringOfArray(values);
}
@Deprecated(forRemoval = true)
public static String getStringOfArray(Object[] values) {
return toString(values, "\n - ", "", false);
}
public static String toString(Object[] values, String prefix, String suffix, boolean removeLastSuffix) {
return toString(Arrays.asList(values), prefix, suffix, removeLastSuffix);
}
public static <T> String toString(List<T> values) {
return toString((Iterable<?>) values, "", ", ", true);
}
public static <T> String toString(Iterable<T> values) {
return toString(values, "", ", ", true);
}
public static <T> String toString(Collection<T> values) {
return toString((Iterable<?>) values, "", ", ", true);
}
public static <T> String toString(List<T> values, String prefix, String suffix, boolean removeLastSuffix) {
return toString((Iterable<?>) values, prefix, suffix, removeLastSuffix);
}
public static <T> String toString(Collection<T> values, String prefix, String suffix, boolean removeLastSuffix) {
return toString((Iterable<?>) values, prefix, suffix, removeLastSuffix);
}
public static <T> String toString(Iterable<T> values, String prefix, String suffix, boolean removeLastSuffix) {
StringBuilder sb = new StringBuilder();
for (T value : values) {
sb.append(prefix).append(value).append(suffix);
}
if (removeLastSuffix && sb.length() >= suffix.length()) sb.setLength(sb.length() - suffix.length());
return sb.toString();
}
public static <T> int increment(T item, Map<T, Integer> map) {
return add(item, 1, map);
}
public static <T> int add(T item, int toAdd, Map<T, Integer> map) {
int newValue = map.getOrDefault(item, 0) + toAdd;
map.put(item, newValue);
return newValue;
}
public static <T> int decrement(T item, Map<T, Integer> map) {
return add(item, -1, map);
}
public static <T> T getRandomWeightedElement(Map<T, ? extends Number> map) {
if (map.isEmpty()) return null;
double totalWeight = map.values().stream().mapToDouble(Number::doubleValue).sum();
double randomWeight = Rand.ofDouble(totalWeight);
for (Map.Entry<T, ? extends Number> entry : map.entrySet()) {
randomWeight -= entry.getValue().doubleValue();
if (randomWeight < 0) {
return entry.getKey();
}
}
return null;
}
public static <T> T getRandomWeightedElement(Collection<T> elements, Function<T, ? extends Number> chanceExtractor) {
if (elements == null || elements.isEmpty()) return null;
return getRandomWeightedElement(elements.stream().collect(Collectors.toMap(x -> x, chanceExtractor)));
}
public static int getFirstAvailableID(Collection<Integer> ids, int minID) {
HashSet<Integer> idSet = new HashSet<>(ids);
int availableId = minID;
while (idSet.contains(availableId)) {
availableId++;
}
return availableId;
}
public static @NotNull List<String> toStringList(Collection<?> collection) {
List<String> list = new ArrayList<>();
for (Object entry : collection) {
list.add(entry.toString());
}
return list;
}
}