forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFpKit.java
More file actions
167 lines (145 loc) · 5.67 KB
/
FpKit.java
File metadata and controls
167 lines (145 loc) · 5.67 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
package graphql.util;
import graphql.Internal;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static java.util.Collections.singletonList;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.mapping;
@Internal
public class FpKit {
//
// From a list of named things, get a map of them by name, merging them according to the merge function
public static <T> Map<String, T> getByName(List<T> namedObjects, Function<T, String> nameFn, BinaryOperator<T> mergeFunc) {
return namedObjects.stream().collect(Collectors.toMap(
nameFn,
identity(),
mergeFunc,
LinkedHashMap::new)
);
}
// normal groupingBy but with LinkedHashMap
public static <T, NewKey> Map<NewKey, List<T>> groupingBy(List<T> list, Function<T, NewKey> function) {
return list.stream().collect(Collectors.groupingBy(function, LinkedHashMap::new, mapping(Function.identity(), Collectors.toList())));
}
//
// From a list of named things, get a map of them by name, merging them first one added
public static <T> Map<String, T> getByName(List<T> namedObjects, Function<T, String> nameFn) {
return getByName(namedObjects, nameFn, mergeFirst());
}
public static <T> BinaryOperator<T> mergeFirst() {
return (o1, o2) -> o1;
}
/**
* Converts an object that should be an Iterable into a Collection efficiently, leaving
* it alone if it is already is one. Useful when you want to get the size of something
*
* @param iterableResult the result object
* @param <T> the type of thing
*
* @return an Iterable from that object
*
* @throws java.lang.ClassCastException if its not an Iterable
*/
@SuppressWarnings("unchecked")
public static <T> Collection<T> toCollection(Object iterableResult) {
if (iterableResult.getClass().isArray()) {
List<Object> collect = IntStream.range(0, Array.getLength(iterableResult))
.mapToObj(i -> Array.get(iterableResult, i))
.collect(Collectors.toList());
return (List<T>) collect;
}
if (iterableResult instanceof Collection) {
return (Collection<T>) iterableResult;
}
Iterable<T> iterable = (Iterable<T>) iterableResult;
Iterator<T> iterator = iterable.iterator();
List<T> list = new ArrayList<>();
while (iterator.hasNext()) {
list.add(iterator.next());
}
return list;
}
/**
* Concatenates (appends) a single elements to an existing list
*
* @param l the list onto which to append the element
* @param t the element to append
* @param <T> the type of elements of the list
*
* @return a <strong>new</strong> list componsed of the first list elements and the new element
*/
public static <T> List<T> concat(List<T> l, T t) {
return concat(l, singletonList(t));
}
/**
* Concatenates two lists into one
*
* @param l1 the first list to concatenate
* @param l2 the second list to concatenate
* @param <T> the type of element of the lists
*
* @return a <strong>new</strong> list composed of the two concatenated lists elements
*/
public static <T> List<T> concat(List<T> l1, List<T> l2) {
ArrayList<T> l = new ArrayList<>(l1);
l.addAll(l2);
l.trimToSize();
return l;
}
//
// quickly turn a map of values into its list equivalent
public static <T> List<T> valuesToList(Map<?, T> map) {
return new ArrayList<>(map.values());
}
public static <T, U> List<U> map(List<T> list, Function<T, U> function) {
return list.stream().map(function).collect(Collectors.toList());
}
public static <K, V, U> List<U> mapEntries(Map<K, V> map, BiFunction<K, V, U> function) {
return map.entrySet().stream().map(entry -> function.apply(entry.getKey(), entry.getValue())).collect(Collectors.toList());
}
public static <T> List<List<T>> transposeMatrix(List<? extends List<T>> matrix) {
int rowCount = matrix.size();
int colCount = matrix.get(0).size();
List<List<T>> result = new ArrayList<>();
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < colCount; j++) {
T val = matrix.get(i).get(j);
if (result.size() <= j) {
result.add(j, new ArrayList());
}
result.get(j).add(i, val);
}
}
return result;
}
public static <T> CompletableFuture<List<T>> flatList(CompletableFuture<List<List<T>>> cf) {
return cf.thenApply(FpKit::flatList);
}
public static <T> List<T> flatList(List<List<T>> listLists) {
return listLists.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
}
public static <T> Optional<T> findOne(List<T> list, Predicate<T> filter) {
return list
.stream()
.filter(filter)
.findFirst();
}
public static <T> T findOneOrNull(List<T> list, Predicate<T> filter) {
return findOne(list, filter).orElse(null);
}
}