public final class Guavate extends Object
Guava has the FluentIterable concept which is similar to streams.
In many ways, fluent iterable is nicer, because it directly binds to the
immutable collection classes. However, on balance it seems wise to use
the stream API rather than FluentIterable in Java 8.
| Modifier and Type | Method and Description |
|---|---|
static Class<?> |
callerClass(int callStackDepth)
Finds the caller class.
|
static <T,R extends T> |
casting(Class<R> cls)
Function used in a stream to cast instances to a particular type without filtering.
|
static <T> CompletableFuture<List<T>> |
combineFuturesAsList(List<? extends CompletableFuture<? extends T>> futures)
Converts a list of futures to a single future, combining the values into a list.
|
static <K,V,F extends CompletableFuture<? extends V>> |
combineFuturesAsMap(Map<? extends K,? extends F> futures)
Converts a map of futures to a single future.
|
static <K,V> ImmutableMap<K,V> |
combineMaps(Map<? extends K,? extends V> first,
Map<? extends K,? extends V> second)
Combines two distinct maps into a single map.
|
static <K,V> ImmutableMap<K,V> |
combineMaps(Map<? extends K,? extends V> first,
Map<? extends K,? extends V> second,
BiFunction<? super V,? super V,? extends V> mergeFn)
Combines two maps into a single map.
|
static <T> ImmutableList<T> |
concatToList(Iterable<? extends T>... iterables)
Concatenates a number of iterables into a single list.
|
static <T> ImmutableSet<T> |
concatToSet(Iterable<? extends T>... iterables)
Concatenates a number of iterables into a single set.
|
static <T> BinaryOperator<T> |
ensureOnlyOne()
Reducer used in a stream to ensure there is no more than one matching element.
|
static <K,V> Collector<Map.Entry<? extends K,? extends V>,?,ImmutableMap<K,V>> |
entriesToImmutableMap()
Collector used at the end of a stream to build an immutable map
from a stream containing map entries.
|
static <K,V> Collector<Map.Entry<? extends K,? extends V>,?,ImmutableMap<K,V>> |
entriesToImmutableMap(BiFunction<? super V,? super V,? extends V> mergeFn)
Collector used at the end of a stream to build an immutable map
from a stream containing map entries which could have duplicate keys.
|
static <K,V> Map.Entry<K,V> |
entry(K key,
V value)
Creates a single
Map.Entry. |
static <T,R extends T> |
filtering(Class<R> cls)
Function used in a stream to filter instances to a particular type.
|
static <T> Function<Optional<T>,Stream<T>> |
filteringOptional()
Function used in a stream to filter optionals.
|
static <T> Optional<T> |
first(Iterable<T> iterable)
Gets the first value from the iterable, returning empty if the iterable is empty.
|
static <T> Optional<T> |
firstNonEmpty(Optional<? extends T>... optionals)
Chooses the first optional that is not empty.
|
static <T> Optional<T> |
firstNonEmpty(Supplier<Optional<? extends T>>... suppliers)
Uses a number of suppliers to create a single optional result.
|
static <T> Iterable<T> |
in(Stream<T> stream)
Converts a stream to an iterable for use in the for-each statement.
|
static <T> Iterable<T> |
inOptional(Optional<T> optional)
Converts an optional to an iterable for use in the for-each statement.
|
static ThreadFactoryBuilder |
namedThreadFactory()
Creates a ThreadFactoryBuilder which names new threads with the name of the calling class plus a unique integer.
|
static ThreadFactoryBuilder |
namedThreadFactory(String threadNamePrefix)
Creates a ThreadFactoryBuilder which names new threads with the given name prefix plus a unique integer.
|
static <R> Predicate<R> |
not(Predicate<R> predicate)
Returns a predicate that negates the original.
|
static <K,V> Collector<Pair<? extends K,? extends V>,?,ImmutableMap<K,V>> |
pairsToImmutableMap()
Collector used at the end of a stream to build an immutable map
from a stream containing pairs.
|
static <T> CompletableFuture<T> |
poll(ScheduledExecutorService executorService,
Duration initialDelay,
Duration frequency,
Supplier<T> pollingTask)
Polls on a regular frequency until a result is found.
|
static <T> Collector<T,?,ImmutableList<ImmutableList<T>>> |
splittingBySize(int size)
Collector used at the end of a stream to build an immutable list of
immutable lists of size equal to or less than size.
|
static <T> Stream<T> |
stream(Iterable<T> iterable)
Converts an iterable to a serial stream.
|
static <T> Stream<T> |
stream(Optional<T> optional)
Converts an optional to a stream with zero or one elements.
|
static <T,S extends CompletableFuture<? extends T>> |
toCombinedFuture()
Collector used at the end of a stream to convert a list of futures to a single future,
combining the values into a list.
|
static <K,V,F extends CompletableFuture<? extends V>> |
toCombinedFutureMap()
Collector used at the end of a stream to convert a map of futures to a single future,
combining the values into a map.
|
static <T> Collector<T,ImmutableList.Builder<T>,ImmutableList<T>> |
toImmutableList()
Collector used at the end of a stream to build an immutable list.
|
static <T,K> Collector<T,?,ImmutableListMultimap<K,T>> |
toImmutableListMultimap(Function<? super T,? extends K> keyExtractor)
Collector used at the end of a stream to build an immutable multimap.
|
static <T,K,V> Collector<T,?,ImmutableListMultimap<K,V>> |
toImmutableListMultimap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends V> valueExtractor)
Collector used at the end of a stream to build an immutable multimap.
|
static <T,K> Collector<T,?,ImmutableMap<K,T>> |
toImmutableMap(Function<? super T,? extends K> keyExtractor)
Collector used at the end of a stream to build an immutable map.
|
static <T,K,V> Collector<T,?,ImmutableMap<K,V>> |
toImmutableMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends V> valueExtractor)
Collector used at the end of a stream to build an immutable map.
|
static <T,K,V> Collector<T,Map<K,V>,ImmutableMap<K,V>> |
toImmutableMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends V> valueExtractor,
BiFunction<? super V,? super V,? extends V> mergeFn)
Collector used at the end of a stream to build an immutable map.
|
static <T> Collector<T,ImmutableMultiset.Builder<T>,ImmutableMultiset<T>> |
toImmutableMultiset()
Collector used at the end of a stream to build an immutable multiset.
|
static <T> Collector<T,ImmutableSet.Builder<T>,ImmutableSet<T>> |
toImmutableSet()
Collector used at the end of a stream to build an immutable set.
|
static <T,K> Collector<T,?,ImmutableSetMultimap<K,T>> |
toImmutableSetMultimap(Function<? super T,? extends K> keyExtractor)
Collector used at the end of a stream to build an immutable multimap.
|
static <T,K,V> Collector<T,?,ImmutableSetMultimap<K,V>> |
toImmutableSetMultimap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends V> valueExtractor)
Collector used at the end of a stream to build an immutable multimap.
|
static <T,K extends Comparable<?>> |
toImmutableSortedMap(Function<? super T,? extends K> keyExtractor)
Collector used at the end of a stream to build an immutable sorted map.
|
static <T,K extends Comparable<?>,V> |
toImmutableSortedMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends V> valueExtractor)
Collector used at the end of a stream to build an immutable sorted map.
|
static <T,K extends Comparable<?>,V> |
toImmutableSortedMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends V> valueExtractor,
BiFunction<? super V,? super V,? extends V> mergeFn)
Collector used at the end of a stream to build an immutable sorted map.
|
static <T extends Comparable<?>> |
toImmutableSortedSet()
Collector used at the end of a stream to build an immutable sorted set.
|
static <T> Collector<T,ImmutableSortedSet.Builder<T>,ImmutableSortedSet<T>> |
toImmutableSortedSet(Comparator<? super T> comparator)
Collector used at the end of a stream to build an immutable sorted set.
|
static <T> Optional<T> |
tryCatchToOptional(Supplier<T> supplier)
Wraps a try-catch block around an expression, avoiding exceptions.
|
static <A,B> Stream<Pair<A,B>> |
zip(Stream<A> stream1,
Stream<B> stream2)
Creates a stream that combines two other streams, continuing until either stream ends.
|
static <T> Stream<ObjIntPair<T>> |
zipWithIndex(Stream<T> stream)
Creates a stream that wraps a stream with the index.
|
@SafeVarargs public static <T> ImmutableList<T> concatToList(Iterable<? extends T>... iterables)
This is harder than it should be, a method Stream.of(Iterable)
would have been appropriate, but cannot be added now.
T - the type of element in the iterableiterables - the iterables to combine@SafeVarargs public static <T> ImmutableSet<T> concatToSet(Iterable<? extends T>... iterables)
This is harder than it should be, a method Stream.of(Iterable)
would have been appropriate, but cannot be added now.
T - the type of element in the iterableiterables - the iterables to combinepublic static <K,V> ImmutableMap<K,V> combineMaps(Map<? extends K,? extends V> first, Map<? extends K,? extends V> second)
K - the type of the keysV - the type of the valuesfirst - the first mapsecond - the second mapIllegalArgumentException - if the same key is encountered in both mapspublic static <K,V> ImmutableMap<K,V> combineMaps(Map<? extends K,? extends V> first, Map<? extends K,? extends V> second, BiFunction<? super V,? super V,? extends V> mergeFn)
If the maps have shared keys then the merge function is used on the two values and the result is placed in the resulting map.
K - the type of the keysV - the type of the valuesfirst - the first mapsecond - the second mapmergeFn - the function used to merge valuespublic static <T> Optional<T> tryCatchToOptional(Supplier<T> supplier)
This converts an exception throwing method into an optional returning one by discarding the exception. In most cases it is better to add a `findXxx()` method to the code you want to call.
T - the type of the result in the optionalsupplier - the supplier that might throw an exception@SafeVarargs public static <T> Optional<T> firstNonEmpty(Supplier<Optional<? extends T>>... suppliers)
This invokes each supplier in turn until a non empty optional is returned. As such, not all suppliers are necessarily invoked.
The Java 8 Optional class does not have an or method,
so this provides an alternative.
T - the type of element in the optionalsuppliers - the suppliers to combine@SafeVarargs public static <T> Optional<T> firstNonEmpty(Optional<? extends T>... optionals)
The Java 8 Optional class does not have an or method,
so this provides an alternative.
T - the type of element in the optionaloptionals - the optionals to combinepublic static <T> Optional<T> first(Iterable<T> iterable)
T - the type of element in the optionaliterable - the iterable to querypublic static <K,V> Map.Entry<K,V> entry(K key, V value)
Map.Entry.
The entry is immutable.
K - the type of the keyV - the type of the valuekey - the keyvalue - the valuepublic static <T> Stream<T> stream(Iterable<T> iterable)
This is harder than it should be, a method Stream.of(Iterable)
would have been appropriate, but cannot be added now.
T - the type of element in the iterableiterable - the iterable to convertpublic static <T> Stream<T> stream(Optional<T> optional)
T - the type of optional elementoptional - the optionalpublic static <T> Iterable<T> in(Stream<T> stream)
For some use cases this approach is nicer than Stream.forEach(Consumer).
Notably code that mutates a local variable or has to handle checked exceptions will benefit.
for (Item item : in(stream)) {
// lazily use each item in the stream
}
NOTE: The result of this method can only be iterated once, which does not
meet the expected specification of Iterable.
Use in the for-each statement is safe as it will only be called once.
T - the type of stream elementstream - the streampublic static <T> Iterable<T> inOptional(Optional<T> optional)
For some use cases this approach is nicer than Optional.isPresent()
followed by Optional.get().
for (Item item : inOptional(optional)) {
// use the optional value, code not called if the optional is empty
}
NOTE: This method is intended only for use with the for-each statement.
It does in fact return a general purpose Iterable, but the method name
is focussed on the for-each use case.
T - the type of optional elementoptional - the optionalpublic static <T> Stream<ObjIntPair<T>> zipWithIndex(Stream<T> stream)
Each input object is decorated with an ObjIntPair.
The int is the index of the element in the stream.
See also MapStream.zipWithIndex(Stream).
T - the type of the streamstream - the stream to indexpublic static <A,B> Stream<Pair<A,B>> zip(Stream<A> stream1, Stream<B> stream2)
Each pair of input objects is combined into a Pair.
See also MapStream.zip(Stream, Stream).
A - the type of the first streamB - the type of the second streamstream1 - the first streamstream2 - the first streampublic static <R> Predicate<R> not(Predicate<R> predicate)
The JDK provides Predicate.negate() however this requires a predicate.
Sometimes, it can be useful to have a static method to achieve this.
stream.filter(not(String::isEmpty))
R - the type of the object the predicate works onpredicate - the predicate to negatepublic static <T> BinaryOperator<T> ensureOnlyOne()
This method returns an operator that can be used with Stream.reduce(BinaryOperator)
that returns either zero or one elements from the stream. Unlike Stream.findFirst()
or Stream.findAny(), this approach ensures an exception is thrown if there
is more than one element in the stream.
This would be used as follows (with a static import):
stream.filter(...).reduce(ensureOnlyOne()).get();
T - the type of element in the streampublic static <T,R extends T> Function<T,R> casting(Class<R> cls)
This method returns a function that can be used with Stream.map(Function)
to cast elements in a stream to a particular type, throwing an exception if any
element is not of the specified type.
This would be used as follows (with a static import):
stream.map(casting(Foo.class));
This replaces code of the form:
stream.map(Foo.class::cast);
T - the type of element in the input streamR - the type of element in the output streamcls - the type of element in the output streampublic static <T,R extends T> Function<T,Stream<R>> filtering(Class<R> cls)
This method returns a function that can be used with Stream.flatMap(Function)
to filter elements in a stream to a particular type.
This would be used as follows (with a static import):
stream.flatMap(filtering(Foo.class));
This replaces code of the form:
stream.filter(Foo.class::isInstance).map(Foo.class::cast);
T - the type of element in the input streamR - the type of element in the output streamcls - the type of element in the output streampublic static <T> Function<Optional<T>,Stream<T>> filteringOptional()
This method returns a function that can be used with Stream.flatMap(Function)
to filter optional elements in a stream.
The resulting stream only contains the optional elements that are present.
This would be used as follows (with a static import):
stream.flatMap(filteringOptional());
This replaces code of the form:
stream.filter(Optional::isPresent).map(Optional::get);
T - the type of element in the output streampublic static <T> Collector<T,ImmutableList.Builder<T>,ImmutableList<T>> toImmutableList()
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableList.
T - the type of element in the listpublic static <T> Collector<T,?,ImmutableList<ImmutableList<T>>> splittingBySize(int size)
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableList of ImmutableList.
T - the type of element in the listsize - the size of the partitions of the original listpublic static <T> Collector<T,ImmutableSet.Builder<T>,ImmutableSet<T>> toImmutableSet()
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableSet.
T - the type of element in the setpublic static <T extends Comparable<?>> Collector<T,ImmutableSortedSet.Builder<T>,ImmutableSortedSet<T>> toImmutableSortedSet()
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableSet.
T - the type of element in the sorted setpublic static <T> Collector<T,ImmutableSortedSet.Builder<T>,ImmutableSortedSet<T>> toImmutableSortedSet(Comparator<? super T> comparator)
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableSet.
T - the type of element in the sorted setcomparator - the comparatorpublic static <T> Collector<T,ImmutableMultiset.Builder<T>,ImmutableMultiset<T>> toImmutableMultiset()
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableMultiset.
T - the type of element in the multisetpublic static <T,K> Collector<T,?,ImmutableMap<K,T>> toImmutableMap(Function<? super T,? extends K> keyExtractor)
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableMap, retaining insertion order.
This returns a map by extracting a key from each element.
The input stream must resolve to unique keys.
The value associated with each key is the stream element.
See Collectors.toMap(Function, Function) for more details.
T - the type of the stream elementsK - the type of the keys in the result mapkeyExtractor - function to produce keys from stream elementsIllegalArgumentException - if the same key is generated twicepublic static <T,K,V> Collector<T,?,ImmutableMap<K,V>> toImmutableMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends V> valueExtractor)
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableMap, retaining insertion order.
This returns a map by converting each stream element to a key and value.
The input stream must resolve to unique keys.
See Collectors.toMap(Function, Function) for more details.
T - the type of the stream elementsK - the type of the keys in the result mapV - the type of the values in the result mapkeyExtractor - function to produce keys from stream elementsvalueExtractor - function to produce values from stream elementsIllegalArgumentException - if the same key is generated twicepublic static <T,K,V> Collector<T,Map<K,V>,ImmutableMap<K,V>> toImmutableMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends V> valueExtractor, BiFunction<? super V,? super V,? extends V> mergeFn)
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableMap, retaining insertion order.
This returns a map by converting each stream element to a key and value. If the same key is generated more than once the merge function is applied to the values and the return value of the function is used as the value in the map.
T - the type of the stream elementsK - the type of the keys in the result mapV - the type of the values in the result mapkeyExtractor - function to produce keys from stream elementsvalueExtractor - function to produce values from stream elementsmergeFn - function to merge values with the same keypublic static <T,K extends Comparable<?>> Collector<T,?,ImmutableSortedMap<K,T>> toImmutableSortedMap(Function<? super T,? extends K> keyExtractor)
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableSortedMap.
This returns a map by extracting a key from each element.
The input stream must resolve to unique keys.
The value associated with each key is the stream element.
See Collectors.toMap(Function, Function) for more details.
T - the type of the stream elementsK - the type of the keys in the result mapkeyExtractor - function to produce keys from stream elementsIllegalArgumentException - if the same key is generated twicepublic static <T,K extends Comparable<?>,V> Collector<T,?,ImmutableSortedMap<K,V>> toImmutableSortedMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends V> valueExtractor)
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableSortedMap.
This returns a map by converting each stream element to a key and value.
The input stream must resolve to unique keys.
See Collectors.toMap(Function, Function) for more details.
T - the type of the stream elementsK - the type of the keys in the result mapV - the type of the values in the result mapkeyExtractor - function to produce keys from stream elementsvalueExtractor - function to produce values from stream elementsIllegalArgumentException - if the same key is generated twicepublic static <T,K extends Comparable<?>,V> Collector<T,?,ImmutableSortedMap<K,V>> toImmutableSortedMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends V> valueExtractor, BiFunction<? super V,? super V,? extends V> mergeFn)
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableSortedMap.
This returns a map by converting each stream element to a key and value.
See Collectors.toMap(Function, Function) for more details.
T - the type of the stream elementsK - the type of the keys in the result mapV - the type of the values in the result mapkeyExtractor - function to produce keys from stream elementsvalueExtractor - function to produce values from stream elementsmergeFn - function to merge values with the same keypublic static <T,K> Collector<T,?,ImmutableListMultimap<K,T>> toImmutableListMultimap(Function<? super T,? extends K> keyExtractor)
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableListMultimap.
This returns a multimap by extracting a key from each element.
The value associated with each key is the stream element.
Stream elements may be converted to the same key, with the values forming a multimap list.
See Collectors.groupingBy(Function) for more details.
T - the type of the stream elementsK - the type of the keys in the result multimapkeyExtractor - function to produce keys from stream elementspublic static <T,K,V> Collector<T,?,ImmutableListMultimap<K,V>> toImmutableListMultimap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends V> valueExtractor)
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableListMultimap.
This returns a multimap by converting each stream element to a key and value. Stream elements may be converted to the same key, with the values forming a multimap list.
T - the type of the stream elementsK - the type of the keys in the result multimapV - the type of the values in the result multimapkeyExtractor - function to produce keys from stream elementsvalueExtractor - function to produce values from stream elementspublic static <T,K> Collector<T,?,ImmutableSetMultimap<K,T>> toImmutableSetMultimap(Function<? super T,? extends K> keyExtractor)
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableSetMultimap.
This returns a multimap by extracting a key from each element.
The value associated with each key is the stream element.
Stream elements may be converted to the same key, with the values forming a multimap set.
See Collectors.groupingBy(Function) for more details.
T - the type of the stream elementsK - the type of the keys in the result multimapkeyExtractor - function to produce keys from stream elementspublic static <T,K,V> Collector<T,?,ImmutableSetMultimap<K,V>> toImmutableSetMultimap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends V> valueExtractor)
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableSetMultimap.
This returns a multimap by converting each stream element to a key and value. Stream elements may be converted to the same key, with the values forming a multimap set.
T - the type of the stream elementsK - the type of the keys in the result multimapV - the type of the values in the result multimapkeyExtractor - function to produce keys from stream elementsvalueExtractor - function to produce values from stream elementspublic static <K,V> Collector<Map.Entry<? extends K,? extends V>,?,ImmutableMap<K,V>> entriesToImmutableMap()
entrySet has undergone a filter operation. For example:
Map<String, Integer> input = ImmutableMap.of("a", 1, "b", 2, "c", 3, "d", 4, "e", 5);
ImmutableMap<String, Integer> output =
input.entrySet()
.stream()
.filter(e -> e.getValue() % 2 == 1)
.collect(entriesToImmutableMap());
// Produces map with "a" -> 1, "c" -> 3, "e" -> 5
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableMap.
This returns a map by converting each Map.Entry to a key and value.
The input stream must resolve to unique keys.
K - the type of the keys in the result mapV - the type of the values in the result mapIllegalArgumentException - if the same key is generated twicepublic static <K,V> Collector<Map.Entry<? extends K,? extends V>,?,ImmutableMap<K,V>> entriesToImmutableMap(BiFunction<? super V,? super V,? extends V> mergeFn)
This is a common case if a map's entrySet has undergone a map operation. For example:
Map<Integer, String> input = ImmutableMap.of(1, "a", 2, "b", 3, "c", 4, "d", 5, "e");
ImmutableMap<String, Integer> output =
input.entrySet()
.stream()
.map(e -> Guavate.entry(e.getKey() % 2, e.getValue()))
.collect(entriesToImmutableMap(String::concat));
// Produces map with 0 -> "bd", 1 -> "ace"
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableMap.
This returns a map by converting each Map.Entry to a key and value.
K - the type of the keys in the result mapV - the type of the values in the result mapmergeFn - function to merge values with the same keypublic static <K,V> Collector<Pair<? extends K,? extends V>,?,ImmutableMap<K,V>> pairsToImmutableMap()
entrySet has undergone a map operation with the
Map.Entry converted to a Pair. For example:
Map<String, Integer> input = ImmutableMap.of("a", 1, "b", 2, "c", 3, "d", 4);
ImmutableMap<String, Double> output =
input.entrySet()
.stream()
.map(e -> Pair.of(e.getKey().toUpperCase(), Math.pow(e.getValue(), 2)))
.collect(pairsToImmutableMap());
// Produces map with "A" -> 1.0, "B" -> 4.0, "C" -> 9.0, "D" -> 16.0
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing streams to be gathered into
an ImmutableMap.
This returns a map by converting each stream element to a key and value. The input stream must resolve to unique keys.
K - the type of the keys in the result mapV - the type of the values in the result mapIllegalArgumentException - if the same key is generated twicepublic static <T> CompletableFuture<List<T>> combineFuturesAsList(List<? extends CompletableFuture<? extends T>> futures)
The CompletableFuture.allOf(CompletableFuture...) method is useful
but it returns Void. This method combines the futures but also
returns the resulting value as a list.
Effectively, this converts List<CompletableFuture<T>> to CompletableFuture<List<T>>.
If any input future completes exceptionally, the result will also complete exceptionally.
T - the type of the values in the listfutures - the futures to convert, may be emptypublic static <T,S extends CompletableFuture<? extends T>> Collector<S,?,CompletableFuture<List<T>>> toCombinedFuture()
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing a stream of futures to be combined into a single future.
This converts List<CompletableFuture<T>> to CompletableFuture<List<T>>.
S - the type of the input futuresT - the type of the valuespublic static <K,V,F extends CompletableFuture<? extends V>> CompletableFuture<Map<K,V>> combineFuturesAsMap(Map<? extends K,? extends F> futures)
This is similar to combineFuturesAsList(List) but for maps.
Effectively, this converts Map<K, CompletableFuture<V>> to CompletableFuture<Map<K, V>>.
If any input future completes exceptionally, the result will also complete exceptionally. The results must be non-null.
K - the type of the keys in the mapV - the type of the values in the mapF - the type of the futures, must not be Voidfutures - the futures to convert, may be emptypublic static <K,V,F extends CompletableFuture<? extends V>> Collector<Map.Entry<? extends K,? extends F>,?,CompletableFuture<Map<K,V>>> toCombinedFutureMap()
A collector is used to gather data at the end of a stream operation.
This method returns a collector allowing a stream of futures to be combined into a single future.
This converts Map<K, CompletableFuture<V>> to CompletableFuture<Map<K, V>>.
K - the type of the keys in the mapV - the type of the values in the mapF - the type of the input futurespublic static <T> CompletableFuture<T> poll(ScheduledExecutorService executorService, Duration initialDelay, Duration frequency, Supplier<T> pollingTask)
Polling is performed via the specified supplier, which must return null until the result is available. If the supplier throws an exception, polling will stop and the future will complete exceptionally.
If the future is cancelled, the polling will also be cancelled. It is advisable to consider using a timeout when querying the future.
In most cases, there needs to be an initial request, which might return an identifier to query. This pattern may be useful for that case:
return CompletableFuture.supplyAsync(initPollingReturningId(), executorService)
.thenCompose(id -> poll(executorService, delay, freq, performPolling(id)));
});
T - the result typeexecutorService - the executor service to use for pollinginitialDelay - the initial delay before starting to pollfrequency - the frequency to poll atpollingTask - the task used to poll, returning null when not yet completepublic static ThreadFactoryBuilder namedThreadFactory()
public static ThreadFactoryBuilder namedThreadFactory(String threadNamePrefix)
threadNamePrefix - the name which new thread names should be prefixed bypublic static Class<?> callerClass(int callStackDepth)
This takes an argument which is the number of stack levels to look back. This will be 2 to return the caller of this method, 3 to return the caller of the caller, and so on.
callStackDepth - the depth of the stack to look backCopyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.