Package graphql.util
Class FpKit
java.lang.Object
graphql.util.FpKit
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> @NonNull List
<T> arrayListSizedTo
(@NonNull Collection<?> collection) Creates anArrayList
sized appropriately to the collection, typically for copyingstatic <T> List
<T> Concatenates two lists into onestatic <T> List
<T> Concatenates (appends) a single elements to an existing liststatic <T,
NewKey>
Map<NewKey, com.google.common.collect.ImmutableList<T>> filterAndGroupingBy
(Collection<T> list, Predicate<? super T> predicate, Function<T, NewKey> function) static <T> List
<T> filterList
(Collection<T> list, Predicate<T> filter) static <T> Set
<T> filterSet
(Collection<T> input, Predicate<T> filter) static <T> int
static <T> Optional
<T> findOne
(Collection<T> list, Predicate<T> filter) static <T> T
findOneOrNull
(List<T> list, Predicate<T> filter) getByName
(List<T> namedObjects, Function<T, String> nameFn, BinaryOperator<T> mergeFunc) static <T,
NewKey>
Map<NewKey, com.google.common.collect.ImmutableList<T>> groupingBy
(Collection<T> list, Function<T, NewKey> function) static <T> Set
<T> intersection
(Set<T> set1, Set<T> set2) Faster set intersection.static <T> Supplier
<T> interThreadMemoize
(Supplier<T> delegate) This will memoize the Supplier across threads and make sure the Supplier is exactly called once.static <T> Supplier
<T> intraThreadMemoize
(Supplier<T> delegate) This will memoize the Supplier within the current thread's visibility, that is it does not use volatile reads but rather use a sentinel check and re-reads the delegate supplier value if the read has not stuck to this thread.static boolean
isIterable
(Object result) static <T> BinaryOperator
<T> newList()
Used in simpleMap.computeIfAbsent(Object, java.util.function.Function)
casesstatic <T> Collection
<T> toCollection
(Object iterableResult) Converts an object that should be an Iterable into a Collection efficiently, leaving it alone if it is already is one.static <T> Iterable
<T> toIterable
(Object iterableResult) static <T> List
<T> toListOrSingletonList
(Object possibleIterable) Converts a value into a list if it's really a collection or array of things else it turns it into a singleton list containing that one valuestatic <T,
NewKey>
Map<NewKey, T> toMap
(Collection<T> collection, Function<T, NewKey> keyFunction, BinaryOperator<T> mergeFunc) static <T,
NewKey>
Map<NewKey, T> toMapByUniqueKey
(Collection<T> list, Function<T, NewKey> keyFunction) static OptionalInt
transposeMatrix
(List<? extends List<T>> matrix) static <T> List
<T> valuesToList
(Map<?, T> map)
-
Constructor Details
-
FpKit
public FpKit()
-
-
Method Details
-
getByName
-
toMap
public static <T,NewKey> Map<NewKey,T> toMap(Collection<T> collection, Function<T, NewKey> keyFunction, BinaryOperator<T> mergeFunc) -
groupingBy
public static <T,NewKey> Map<NewKey,com.google.common.collect.ImmutableList<T>> groupingBy(Collection<T> list, Function<T, NewKey> function) -
filterAndGroupingBy
public static <T,NewKey> Map<NewKey,com.google.common.collect.ImmutableList<T>> filterAndGroupingBy(Collection<T> list, Predicate<? super T> predicate, Function<T, NewKey> function) -
toMapByUniqueKey
public static <T,NewKey> Map<NewKey,T> toMapByUniqueKey(Collection<T> list, Function<T, NewKey> keyFunction) -
getByName
-
mergeFirst
-
toCollection
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- Type Parameters:
T
- the type of thing- Parameters:
iterableResult
- the result object- Returns:
- an Iterable from that object
- Throws:
ClassCastException
- if it's not an Iterable
-
arrayListSizedTo
Creates anArrayList
sized appropriately to the collection, typically for copying- Type Parameters:
T
- to two- Parameters:
collection
- the collection of a certain size- Returns:
- a new
ArrayList
initially sized to the same as the collection
-
toListOrSingletonList
Converts a value into a list if it's really a collection or array of things else it turns it into a singleton list containing that one value- Type Parameters:
T
- for two- Parameters:
possibleIterable
- the possible- Returns:
- an list one way or another
-
isIterable
-
toIterable
-
toSize
-
concat
Concatenates (appends) a single elements to an existing list- Type Parameters:
T
- the type of elements of the list- Parameters:
l
- the list onto which to append the elementt
- the element to append- Returns:
- a new list composed of the first list elements and the new element
-
concat
Concatenates two lists into one- Type Parameters:
T
- the type of element of the lists- Parameters:
l1
- the first list to concatenatel2
- the second list to concatenate- Returns:
- a new list composed of the two concatenated lists elements
-
valuesToList
-
transposeMatrix
-
findOne
-
findOneOrNull
-
findIndex
-
filterList
-
filterSet
-
newList
Used in simpleMap.computeIfAbsent(Object, java.util.function.Function)
cases- Type Parameters:
K
- for KeyV
- for Value- Returns:
- a function that allocates a list
-
intraThreadMemoize
This will memoize the Supplier within the current thread's visibility, that is it does not use volatile reads but rather use a sentinel check and re-reads the delegate supplier value if the read has not stuck to this thread. This means that it's possible that your delegate supplier MAY be called more than once across threads, but only once on the same thread.- Type Parameters:
T
- for two- Parameters:
delegate
- the supplier to delegate to- Returns:
- a supplier that will memoize values in the context of the current thread
-
interThreadMemoize
This will memoize the Supplier across threads and make sure the Supplier is exactly called once.Use for potentially costly actions. Otherwise consider
intraThreadMemoize(Supplier)
- Type Parameters:
T
- for two- Parameters:
delegate
- the supplier to delegate to- Returns:
- a supplier that will memoize values in the context of the all the threads
-
intersection
Faster set intersection.- Type Parameters:
T
- for two- Parameters:
set1
- first setset2
- second set- Returns:
- intersection set
-