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
|
package org.zwobble.mammoth.internal.util;
import java.util.*;
import java.util.function.Function;
public class Maps {
public static <K, V> Map<K, V> map() {
return Collections.emptyMap();
}
public static <K, V> Map<K, V> map(K key1, V value1) {
return Collections.singletonMap(key1, value1);
}
public static <K, V> Map<K, V> map(K key1, V value1, K key2, V value2) {
Map<K, V> map = new HashMap<>();
map.put(key1, value1);
map.put(key2, value2);
return map;
}
public static <K, V> Map<K, V> map(K key1, V value1, K key2, V value2, K key3, V value3) {
Map<K, V> map = new HashMap<>();
map.put(key1, value1);
map.put(key2, value2);
map.put(key3, value3);
return map;
}
public static <K, V> Map.Entry<K, V> entry(K key, V value) {
return new AbstractMap.SimpleImmutableEntry<K, V>(key, value);
}
public static <K, V> Optional<V> lookup(Map<K, V> map, K key) {
return Optional.ofNullable(map.get(key));
}
public static <T, K> Map<K, T> toMapWithKey(Iterable<T> iterable, Function<T, K> function) {
return toMap(iterable, element -> entry(function.apply(element), element));
}
public static <T, K, V> Map<K, V> toMap(Iterable<T> iterable, Function<T, Map.Entry<K, V>> function) {
Map<K, V> map = new HashMap<>();
for (T element : iterable) {
Map.Entry<K, V> entry = function.apply(element);
map.put(entry.getKey(), entry.getValue());
}
return map;
}
public static <T, K> Map<K, List<T>> toMultiMapWithKey(Iterable<T> iterable, Function<T, K> function) {
return toMultiMap(iterable, element -> entry(function.apply(element), element));
}
public static <T, K, V> Map<K, List<V>> toMultiMap(Iterable<T> iterable, Function<T, Map.Entry<K, V>> function) {
Map<K, List<V>> map = new HashMap<>();
for (T element : iterable) {
Map.Entry<K, V> pair = function.apply(element);
map.computeIfAbsent(pair.getKey(), key -> new ArrayList<V>())
.add(pair.getValue());
}
return map;
}
public static <K1, K2, V> Map<K2, V> eagerMapKeys(Map<K1, V> map, Function<K1, K2> function) {
Map<K2, V> result = new HashMap<>();
for (Map.Entry<K1, V> element : map.entrySet()) {
result.put(function.apply(element.getKey()), element.getValue());
}
return result;
}
public static <K, V1, V2> Map<K, V2> eagerMapValues(Map<K, V1> map, Function<V1, V2> function) {
Map<K, V2> result = new HashMap<>();
for (Map.Entry<K, V1> element : map.entrySet()) {
result.put(element.getKey(), function.apply(element.getValue()));
}
return result;
}
public static <K, V> Builder<K, V> builder() {
return new Builder<>();
}
public static class Builder<K, V> {
private final Map<K, V> values = new HashMap<>();
public Builder<K, V> put(K key, V value) {
values.put(key, value);
return this;
}
public Map<K, V> build() {
return values;
}
}
}
|