public class Optionals
extends java.lang.Object
Constructor and Description |
---|
Optionals() |
Modifier and Type | Method and Description |
---|---|
static <T,R> java.util.Optional<R> |
accumulatePresent(CollectionX<java.util.Optional<T>> optionals,
java.util.function.Function<? super T,R> mapper,
Monoid<R> reducer)
Accumulate the results only from those Optionals which have a value present, using the supplied mapping function to
convert the data from each Optional before reducing them using the supplied Monoid (a combining BiFunction/BinaryOperator and identity element that takes two
input values of the same type and returns the combined result)
Monoids . |
static <T,R> java.util.Optional<R> |
accumulatePresent(CollectionX<java.util.Optional<T>> optionals,
Reducer<R> reducer)
Accummulating operation using the supplied Reducer (@see com.aol.cyclops.Reducers).
|
static <T> java.util.Optional<T> |
accumulatePresent(Monoid<T> reducer,
CollectionX<java.util.Optional<T>> optionals)
Accumulate the results only from those Optionals which have a value present, using the
supplied Monoid (a combining BiFunction/BinaryOperator and identity element that takes two
input values of the same type and returns the combined result)
Monoids . |
static <T1,T2,R> java.util.Optional<R> |
combine(java.util.Optional<? extends T1> f,
java.util.Optional<? extends T2> v,
java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
Combine an Optional with the provided Optional using the supplied BiFunction
|
static <T1,T2,R> java.util.Optional<R> |
combine(java.util.Optional<? extends T1> f,
Value<? extends T2> v,
java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
Combine an Optional with the provided value using the supplied BiFunction
|
static <T> java.util.Optional<T> |
narrow(java.util.Optional<? extends T> optional)
Narrow covariant type parameter
|
static <T> java.util.Optional<ListX<T>> |
sequence(CollectionX<java.util.Optional<T>> opts)
Sequence operation, take a Collection of Optionals and turn it into a Optional with a Collection
By constrast with
sequencePresent(CollectionX) , if any Optionals are empty the result
is an empty Optional |
static <T> java.util.Optional<ReactiveSeq<T>> |
sequence(java.util.stream.Stream<java.util.Optional<T>> opts)
Sequence operation, take a Collection of Optionals and turn it into a Optional with a Collection
By constrast with
Optional#sequencePresent(CollectionX) if any Optional types are empty
the return type will be an empty Optional |
static <T> java.util.Optional<ListX<T>> |
sequencePresent(CollectionX<java.util.Optional<T>> opts)
Sequence operation, take a Collection of Optionals and turn it into a Optional with a Collection
Only successes are retained.
|
static <T1,T2,R> java.util.Optional<R> |
zip(java.util.Optional<? extends T1> f,
java.lang.Iterable<? extends T2> v,
java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
Combine an Optional with the provided Iterable (selecting one element if present) using the supplied BiFunction
|
static <T1,T2,R> java.util.Optional<R> |
zip(org.reactivestreams.Publisher<? extends T2> p,
java.util.Optional<? extends T1> f,
java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
Combine an Optional with the provided Publisher (selecting one element if present) using the supplied BiFunction
|
public static <T> java.util.Optional<ListX<T>> sequence(CollectionX<java.util.Optional<T>> opts)
sequencePresent(CollectionX)
, if any Optionals are empty the result
is an empty Optional
Optional<Integer> just = Optional.of(10);
Optional<Integer> none = Optional.empty();
Optional<ListX<Integer>> opts = Optionals.sequence(ListX.of(just, none, Optional.of(1)));
//Optional.empty();
maybes
- Maybes to Sequencepublic static <T> java.util.Optional<ListX<T>> sequencePresent(CollectionX<java.util.Optional<T>> opts)
sequence(CollectionX)
Optional#empty types are
tolerated and ignored.
Optional<Integer> just = Optional.of(10);
Optional<Integer> none = Optional.empty();
Optional<ListX<Integer>> maybes = Optionals.sequencePresent(ListX.of(just, none, Optional.of(1)));
//Optional.of(ListX.of(10, 1));
opts
- Optionals to Sequencepublic static <T> java.util.Optional<ReactiveSeq<T>> sequence(java.util.stream.Stream<java.util.Optional<T>> opts)
Optional#sequencePresent(CollectionX)
if any Optional types are empty
the return type will be an empty Optional
Optional<Integer> just = Optional.of(10);
Optional<Integer> none = Optional.empty();
Optional<ListX<Integer>> maybes = Optionals.sequence(ListX.of(just, none, Optional.of(1)));
//Optional.empty();
opts
- Maybes to Sequencepublic static <T,R> java.util.Optional<R> accumulatePresent(CollectionX<java.util.Optional<T>> optionals, Reducer<R> reducer)
Optional<Integer> just = Optional.of(10);
Optional<Integer> none = Optional.empty();
Optional<PSetX<Integer>> opts = Optional.accumulateJust(ListX.of(just, none, Optional.of(1)), Reducers.toPSetX());
//Optional.of(PSetX.of(10, 1)));
optionals
- Optionals to accumulatereducer
- Reducer to accumulate values withpublic static <T,R> java.util.Optional<R> accumulatePresent(CollectionX<java.util.Optional<T>> optionals, java.util.function.Function<? super T,R> mapper, Monoid<R> reducer)
Monoids
.
Optional<Integer> just = Optional.of(10);
Optional<Integer> none = Optional.empty();
Optional<String> opts = Optional.accumulateJust(ListX.of(just, none, Optional.of(1)), i -> "" + i,
Monoids.stringConcat);
//Optional.of("101")
optionals
- Optionals to accumulatemapper
- Mapping function to be applied to the result of each Optionalreducer
- Monoid to combine values from each Optionalpublic static <T> java.util.Optional<T> accumulatePresent(Monoid<T> reducer, CollectionX<java.util.Optional<T>> optionals)
Monoids
.
Optional<Integer> just = Optional.of(10);
Optional<Integer> none = Optional.empty();
Optional<String> opts = Optional.accumulateJust(Monoids.stringConcat,ListX.of(just, none, Optional.of(1)),
);
//Optional.of("101")
optionals
- Optionals to accumulatemapper
- Mapping function to be applied to the result of each Optionalreducer
- Monoid to combine values from each Optionalpublic static <T1,T2,R> java.util.Optional<R> combine(java.util.Optional<? extends T1> f, Value<? extends T2> v, java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
Optionals.combine(Optional.of(10),Maybe.just(20), this::add)
//Optional[30]
private int add(int a, int b) {
return a + b;
}
f
- Optional to combine with a valuev
- Value to combinefn
- Combining functionpublic static <T1,T2,R> java.util.Optional<R> combine(java.util.Optional<? extends T1> f, java.util.Optional<? extends T2> v, java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
Optionals.combine(Optional.of(10),Optional.of(20), this::add)
//Optional[30]
private int add(int a, int b) {
return a + b;
}
f
- Optional to combine with a valuev
- Optional to combinefn
- Combining functionpublic static <T1,T2,R> java.util.Optional<R> zip(java.util.Optional<? extends T1> f, java.lang.Iterable<? extends T2> v, java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
Optionals.zip(Optional.of(10),Arrays.asList(20), this::add)
//Optional[30]
private int add(int a, int b) {
return a + b;
}
f
- Optional to combine with first element in Iterable (if present)v
- Iterable to combinefn
- Combining functionpublic static <T1,T2,R> java.util.Optional<R> zip(org.reactivestreams.Publisher<? extends T2> p, java.util.Optional<? extends T1> f, java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
Optionals.zip(Flux.just(10),Optional.of(10), this::add)
//Optional[30]
private int add(int a, int b) {
return a + b;
}
p
- Publisher to combinef
- Optional to combine withfn
- Combining functionpublic static <T> java.util.Optional<T> narrow(java.util.Optional<? extends T> optional)
broad
- Optional with covariant type parameter