forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImmutableKit.java
More file actions
136 lines (119 loc) · 4.91 KB
/
Copy pathImmutableKit.java
File metadata and controls
136 lines (119 loc) · 4.91 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
package graphql.collect;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import graphql.Internal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import static graphql.Assert.assertNotNull;
@Internal
public final class ImmutableKit {
public static <T> ImmutableList<T> emptyList() {
return ImmutableList.of();
}
public static <T> ImmutableList<T> nonNullCopyOf(Collection<T> collection) {
return collection == null ? emptyList() : ImmutableList.copyOf(collection);
}
public static <K, V> ImmutableMap<K, V> emptyMap() {
return ImmutableMap.of();
}
/**
* ImmutableMaps are hard to build via {@link Map#computeIfAbsent(Object, Function)} style. This methods
* allows you to take a mutable map with mutable list of keys and make it immutable.
* <p>
* This of course has a cost - if the map is very large you will be using more memory. But for static
* maps that live a long life it maybe be worth it.
*
* @param startingMap the starting input map
* @param <K> for key
* @param <V> for victory
*
* @return and Immutable map of ImmutableList values
*/
public static <K, V> ImmutableMap<K, ImmutableList<V>> toImmutableMapOfLists(Map<K, List<V>> startingMap) {
assertNotNull(startingMap);
ImmutableMap.Builder<K, ImmutableList<V>> map = ImmutableMap.builder();
for (Map.Entry<K, List<V>> e : startingMap.entrySet()) {
ImmutableList<V> value = ImmutableList.copyOf(startingMap.getOrDefault(e.getKey(), emptyList()));
map.put(e.getKey(), value);
}
return map.build();
}
public static <K, V> ImmutableMap<K, V> addToMap(Map<K, V> existing, K newKey, V newVal) {
return ImmutableMap.<K, V>builder().putAll(existing).put(newKey, newVal).build();
}
public static <K, V> ImmutableMap<K, V> mergeMaps(Map<K, V> m1, Map<K, V> m2) {
return ImmutableMap.<K, V>builder().putAll(m1).putAll(m2).build();
}
public static <T> ImmutableList<T> concatLists(List<T> l1, List<T> l2) {
return ImmutableList.<T>builder().addAll(l1).addAll(l2).build();
}
/**
* This is more efficient than `c.stream().map().collect()` because it does not create the intermediate objects needed
* for the flexible style. Benchmarking has shown this to outperform `stream()`.
*
* @param iterable the iterable to map
* @param mapper the mapper function
* @param <T> for two
* @param <R> for result
*
* @return a map immutable list of results
*/
public static <T, R> ImmutableList<R> map(Iterable<? extends T> iterable, Function<? super T, ? extends R> mapper) {
assertNotNull(iterable);
assertNotNull(mapper);
@SuppressWarnings("RedundantTypeArguments")
ImmutableList.Builder<R> builder = ImmutableList.<R>builder();
for (T t : iterable) {
R r = mapper.apply(t);
builder.add(r);
}
return builder.build();
}
/**
* This constructs a new Immutable list from an existing collection and adds a new element to it.
*
* @param existing the existing collection
* @param newValue the new value to add
* @param extraValues more values to add
* @param <T> for two
*
* @return an Immutable list with the extra items.
*/
public static <T> ImmutableList<T> addToList(Collection<? extends T> existing, T newValue, T... extraValues) {
assertNotNull(existing);
assertNotNull(newValue);
int expectedSize = existing.size() + 1 + extraValues.length;
ImmutableList.Builder<T> newList = ImmutableList.builderWithExpectedSize(expectedSize);
newList.addAll(existing);
newList.add(newValue);
for (T extraValue : extraValues) {
newList.add(extraValue);
}
return newList.build();
}
/**
* This constructs a new Immutable set from an existing collection and adds a new element to it.
*
* @param existing the existing collection
* @param newValue the new value to add
* @param extraValues more values to add
* @param <T> for two
*
* @return an Immutable Set with the extra itens.
*/
public static <T> ImmutableSet<T> addToSet(Collection<? extends T> existing, T newValue, T... extraValues) {
assertNotNull(existing);
assertNotNull(newValue);
int expectedSize = existing.size() + 1 + extraValues.length;
ImmutableSet.Builder<T> newSet = ImmutableSet.builderWithExpectedSize(expectedSize);
newSet.addAll(existing);
newSet.add(newValue);
for (T extraValue : extraValues) {
newSet.add(extraValue);
}
return newSet.build();
}
}