T
- The type contained on the Optional withinpublic class OptionalTValue<T> extends java.lang.Object implements OptionalT<T>, TransformerValue<T>, MonadicValue<T>, java.util.function.Supplier<T>, ConvertableFunctor<T>, Filterable<T>, ApplicativeFunctor<T>, Matchable.ValueAndOptionalMatcher<T>
ApplicativeFunctor.Applicatives, ApplicativeFunctor.ApplyFunctions<T>
Value.ValueImpl<T>
Convertable.SupplierToConvertable<T>
Modifier and Type | Method and Description |
---|---|
<U> OptionalTValue<U> |
cast(java.lang.Class<? extends U> type)
Cast all elements in a stream to a given type, possibly throwing a
ClassCastException . |
<T2,R> OptionalTValue<R> |
combine(Value<? extends T2> app,
java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Lazily combine this ApplicativeFunctor with the supplied value via the supplied BiFunction
Example
|
<R> OptionalTValue<R> |
empty() |
static <T> OptionalTValue<T> |
emptyOptional() |
boolean |
equals(java.lang.Object o) |
OptionalTValue<T> |
filter(java.util.function.Predicate<? super T> test)
Filter the wrapped Optional
|
OptionalTValue<T> |
filterNot(java.util.function.Predicate<? super T> fn)
Remove any elements for which the predicate holds (inverse operation to filter)
e.g.
|
<B> OptionalTValue<B> |
flatMap(java.util.function.Function<? super T,? extends MonadicValue<? extends B>> f)
A flattening transformation operation (@see
Optional.flatMap(Function) |
<B> OptionalTValue<B> |
flatMapT(java.util.function.Function<? super T,OptionalTValue<? extends B>> f)
Flat Map the wrapped Optional
|
static <A> OptionalTValue<A> |
fromAnyM(AnyMValue<A> anyM)
|
static <A,V extends MonadicValue<java.util.Optional<A>>> |
fromValue(V monadicValue) |
T |
get() |
int |
hashCode() |
boolean |
isPresent() |
boolean |
isValuePresent() |
java.util.Iterator<T> |
iterator() |
static <U,R> java.util.function.Function<OptionalTValue<U>,OptionalTValue<R>> |
lift(java.util.function.Function<U,R> fn)
Lift a function into one that accepts and returns an OptionalT
This allows multiple monad types to add functionality to existing functions and methods
e.g.
|
static <U1,U2,R> java.util.function.BiFunction<OptionalTValue<U1>,OptionalTValue<U2>,OptionalTValue<R>> |
lift2(java.util.function.BiFunction<? super U1,? super U2,? extends R> fn)
Lift a BiFunction into one that accepts and returns OptionalTs
This allows multiple monad types to add functionality to existing functions and methods
e.g.
|
<B> OptionalTValue<B> |
map(java.util.function.Function<? super T,? extends B> f)
Map the wrapped Optional
|
OptionalTValue<T> |
notNull()
Filter elements retaining only values which are not null
|
static <A> OptionalTValue<A> |
of(AnyMValue<java.util.Optional<A>> monads)
Construct an OptionalT from an AnyM that wraps a monad containing Optionals
|
static <A> OptionalTValue<A> |
of(java.util.Optional<A> maybe) |
<U> OptionalTValue<U> |
ofType(java.lang.Class<? extends U> type)
Keep only those elements in a stream that are of a given type.
|
<R> OptionalTValue<R> |
patternMatch(java.util.function.Function<Matchable.CheckValue1<T,R>,Matchable.CheckValue1<T,R>> case1,
java.util.function.Supplier<? extends R> otherwise)
Transform the elements of this Stream with a Pattern Matching case and default value
|
OptionalTValue<T> |
peek(java.util.function.Consumer<? super T> peek)
Peek at the current value of the Optional
|
ReactiveSeq<T> |
stream() |
void |
subscribe(org.reactivestreams.Subscriber<? super T> s) |
boolean |
test(T t) |
java.lang.String |
toString() |
<R> OptionalTValue<R> |
trampoline(java.util.function.Function<? super T,? extends Trampoline<? extends R>> mapper)
Performs a map operation that can call a recursive method without running out of stack space
|
<R> OptionalTValue<R> |
unit(R value) |
AnyMValue<java.util.Optional<T>> |
unwrap() |
Maybe<T> |
value() |
<T2,R> OptionalTValue<R> |
zip(java.util.function.BiFunction<? super T,? super T2,? extends R> fn,
org.reactivestreams.Publisher<? extends T2> app)
Zip (combine) this Zippable with the supplied Publisher, using the supplied combining function
|
<T2,R> OptionalTValue<R> |
zip(java.lang.Iterable<? extends T2> app,
java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Zip (combine) this Zippable with the supplied Iterable using the supplied combining function
|
<U> OptionalTValue<org.jooq.lambda.tuple.Tuple2<T,U>> |
zip(java.lang.Iterable<? extends U> other)
Zip (combine) this Zippable with the supplied Iterable combining both into a Tuple2
|
<U> OptionalTValue<org.jooq.lambda.tuple.Tuple2<T,U>> |
zip(org.jooq.lambda.Seq<? extends U> other)
Zip (combine) this Zippable with the supplied Seq combining both into a Tuple2
|
<U,R> OptionalTValue<R> |
zip(org.jooq.lambda.Seq<? extends U> other,
java.util.function.BiFunction<? super T,? super U,? extends R> zipper)
Zip (combine) this Zippable with the supplied Seq, using the supplied combining function
|
<U> OptionalTValue<org.jooq.lambda.tuple.Tuple2<T,U>> |
zip(java.util.stream.Stream<? extends U> other)
Zip (combine) this Zippable with the supplied Stream combining both into a Tuple2
|
<U,R> OptionalTValue<R> |
zip(java.util.stream.Stream<? extends U> other,
java.util.function.BiFunction<? super T,? super U,? extends R> zipper)
Zip (combine) this Zippable with the supplied Stream, using the supplied combining function
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
bind, emptyList, fromAnyM, fromAnyMSeq, fromAnyMValue, fromFuture, fromIterable, fromIterablOptionalue, fromOptional, fromPublisher, fromStream, of
anyM, coflatMap, combineEager, flatMapIterable, flatMapPublisher, forEach2, forEach2, forEach3, forEach3, forEach4, forEach4, nest
applyFunctions
collect, fold, fold, generate, iterate, mapReduce, mkString, newSubscriber, of, toDequeX, toEvalAlways, toEvalLater, toEvalNow, toFeatureToggle, toFutureStream, toFutureStream, toIor, toLazyImmutable, toList, toListX, toMaybe, toMutable, toPBagX, toPOrderedSetX, toPQueueX, toPSetX, toPStackX, toPVectorX, toQueueX, toSetX, toSimpleReact, toSimpleReact, toSortedSetX, toTry, toTry, toTry, toXor, toXor, unapply
endsWith, endsWithIterable, findAny, findFirst, firstValue, foldable, foldRight, foldRight, foldRight, foldRightMapToType, get, groupBy, join, join, join, mapReduce, print, print, printErr, printOut, reduce, reduce, reduce, reduce, reduce, reduce, reduce, schedule, scheduleFixedDelay, scheduleFixedRate, single, single, singleOptional, startsWith, startsWithIterable, toConcurrentLazyCollection, toConcurrentLazyStreamable, toLazyCollection, validate, xMatch
collect, fromSupplier, orElse, orElseGet, orElseThrow, toAtomicReference, toCompletableFuture, toCompletableFutureAsync, toCompletableFutureAsync, toFutureW, toFutureWAsync, toFutureWAsync, toOptional, toOptionalAtomicReference, toStream, visit
toOptional, visit
matches
public Maybe<T> value()
value
in interface TransformerValue<T>
public boolean isValuePresent()
isValuePresent
in interface TransformerValue<T>
public OptionalTValue<T> peek(java.util.function.Consumer<? super T> peek)
OptionalT.of(AnyM.fromStream(Optional.of(10))
.peek(System.out::println);
//prints 10
public OptionalTValue<T> filter(java.util.function.Predicate<? super T> test)
OptionalT.of(AnyM.fromStream(Optional.of(10))
.filter(t->t!=10);
//OptionalT<AnyMValue<Stream<Optional.empty>>>
filter
in interface OptionalT<T>
filter
in interface TransformerValue<T>
filter
in interface Filterable<T>
filter
in interface MonadicValue<T>
test
- Predicate to filter the wrapped Optionalpublic <B> OptionalTValue<B> map(java.util.function.Function<? super T,? extends B> f)
OptionalT.of(AnyM.fromStream(Optional.of(10))
.map(t->t=t+1);
//OptionalT<AnyMValue<Stream<Optional[11]>>>
map
in interface OptionalT<T>
map
in interface TransformerValue<T>
map
in interface ConvertableFunctor<T>
map
in interface Functor<T>
map
in interface MonadicValue<T>
f
- Mapping function for the wrapped Optionalpublic <T2,R> OptionalTValue<R> combine(Value<? extends T2> app, java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Combiner
Maybe<Integer> some = Maybe.just(10);
just.combine(Eval.now(20), this::add);
//Some[30]
Maybe<Integer> none = Maybe.none();
none.combine(Eval.now(20), this::add);
//None
public <T2,R> OptionalTValue<R> zip(java.lang.Iterable<? extends T2> app, java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Zippable
public <T2,R> OptionalTValue<R> zip(java.util.function.BiFunction<? super T,? super T2,? extends R> fn, org.reactivestreams.Publisher<? extends T2> app)
Zippable
public <U,R> OptionalTValue<R> zip(org.jooq.lambda.Seq<? extends U> other, java.util.function.BiFunction<? super T,? super U,? extends R> zipper)
Zippable
public <U,R> OptionalTValue<R> zip(java.util.stream.Stream<? extends U> other, java.util.function.BiFunction<? super T,? super U,? extends R> zipper)
Zippable
public <U> OptionalTValue<org.jooq.lambda.tuple.Tuple2<T,U>> zip(java.util.stream.Stream<? extends U> other)
Zippable
public <U> OptionalTValue<org.jooq.lambda.tuple.Tuple2<T,U>> zip(org.jooq.lambda.Seq<? extends U> other)
Zippable
public <U> OptionalTValue<org.jooq.lambda.tuple.Tuple2<T,U>> zip(java.lang.Iterable<? extends U> other)
Zippable
public <B> OptionalTValue<B> flatMapT(java.util.function.Function<? super T,OptionalTValue<? extends B>> f)
OptionalT.of(AnyM.fromStream(Optional.of(10))
.flatMap(t->Optional.empty();
//OptionalT<AnyMValue<Stream<Optional.empty>>>
f
- FlatMap functionpublic <B> OptionalTValue<B> flatMap(java.util.function.Function<? super T,? extends MonadicValue<? extends B>> f)
MonadicValue
Optional.flatMap(Function)
Eval.now(1).map(i->i+2).flatMap(i->Eval.later(()->i*3);
//Eval[9]
public static <U,R> java.util.function.Function<OptionalTValue<U>,OptionalTValue<R>> lift(java.util.function.Function<U,R> fn)
Function<Integer,Integer> add2 = i -> i+2;
Function<OptionalT<Integer>, OptionalT<Integer>> optTAdd2 = OptionalT.lift(add2);
Stream<Integer> withNulls = Stream.of(1,2,null);
AnyMValue<Integer> stream = AnyM.ofMonad(withNulls);
AnyMValue<Optional<Integer>> streamOpt = stream.map(Optional::ofNullable);
List<Integer> results = optTAdd2.apply(OptionalT.of(streamOpt))
.unwrap()
.<Stream<Optional<Integer>>>unwrap()
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
//Arrays.asList(3,4);
public static <U1,U2,R> java.util.function.BiFunction<OptionalTValue<U1>,OptionalTValue<U2>,OptionalTValue<R>> lift2(java.util.function.BiFunction<? super U1,? super U2,? extends R> fn)
BiFunction<Integer,Integer,Integer> add = (a,b) -> a+b;
BiFunction<OptionalT<Integer>,OptionalT<Integer>, OptionalT<Integer>> optTAdd2 = OptionalT.lift2(add);
Stream<Integer> withNulls = Stream.of(1,2,null);
AnyMValue<Integer> stream = AnyM.ofMonad(withNulls);
AnyMValue<Optional<Integer>> streamOpt = stream.map(Optional::ofNullable);
CompletableFuture<Optional<Integer>> two = CompletableFuture.supplyAsync(()->Optional.of(2));
AnyMValue<Optional<Integer>> future= AnyM.ofMonad(two);
List<Integer> results = optTAdd2.apply(OptionalT.of(streamOpt),OptionalT.of(future))
.unwrap()
.<Stream<Optional<Integer>>>unwrap()
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
//Arrays.asList(3,4);
public static <A> OptionalTValue<A> fromAnyM(AnyMValue<A> anyM)
anyM
- AnyM that doesn't contain a monad wrapping an Optionalpublic static <A> OptionalTValue<A> of(AnyMValue<java.util.Optional<A>> monads)
monads
- AnyM that contains a monad wrapping an Optionalpublic static <A> OptionalTValue<A> of(java.util.Optional<A> maybe)
public static <A,V extends MonadicValue<java.util.Optional<A>>> OptionalTValue<A> fromValue(V monadicValue)
public java.lang.String toString()
toString
in class java.lang.Object
public T get()
get
in interface Convertable<T>
get
in interface java.util.function.Supplier<T>
public boolean isPresent()
isPresent
in interface TransformerValue<T>
isPresent
in interface Convertable<T>
public ReactiveSeq<T> stream()
public java.util.Iterator<T> iterator()
iterator
in interface Matchable.MatchableOptional<T>
iterator
in interface Matchable.ValueAndOptionalMatcher<T>
iterator
in interface Convertable<T>
iterator
in interface Value<T>
iterator
in interface java.lang.Iterable<T>
public void subscribe(org.reactivestreams.Subscriber<? super T> s)
public boolean test(T t)
public <R> OptionalTValue<R> unit(R value)
public <R> OptionalTValue<R> empty()
public static <T> OptionalTValue<T> emptyOptional()
emptyOptional
in interface OptionalT<T>
public <U> OptionalTValue<U> cast(java.lang.Class<? extends U> type)
Functor
ClassCastException
.
// ClassCastException ReactiveSeq.of(1, "a", 2, "b", 3).cast(Integer.class)public <R> OptionalTValue<R> trampoline(java.util.function.Function<? super T,? extends Trampoline<? extends R>> mapper)
Functor
ReactiveSeq.of(10,20,30,40)
.trampoline(i-> fibonacci(i))
.forEach(System.out::println);
Trampoline<Long> fibonacci(int i){
return fibonacci(i,1,0);
}
Trampoline<Long> fibonacci(int n, long a, long b) {
return n == 0 ? Trampoline.done(b) : Trampoline.more( ()->fibonacci(n-1, a+b, a));
}
55
6765
832040
102334155
ReactiveSeq.of(10_000,200_000,3_000_000,40_000_000)
.trampoline(i-> fibonacci(i))
.forEach(System.out::println);
completes successfully
trampoline
in interface OptionalT<T>
trampoline
in interface TransformerValue<T>
trampoline
in interface Functor<T>
mapper
- TCO Transformation functionpublic <R> OptionalTValue<R> patternMatch(java.util.function.Function<Matchable.CheckValue1<T,R>,Matchable.CheckValue1<T,R>> case1, java.util.function.Supplier<? extends R> otherwise)
Functor
List<String> result = CollectionX.of(1,2,3,4)
.patternMatch(
c->c.valuesWhere(i->"even", (Integer i)->i%2==0 )
)
// CollectionX["odd","even","odd","even"]
patternMatch
in interface OptionalT<T>
patternMatch
in interface TransformerValue<T>
patternMatch
in interface Functor<T>
case1
- Function to generate a case (or chain of cases as a single case)otherwise
- Value if supplied case doesn't matchpublic <U> OptionalTValue<U> ofType(java.lang.Class<? extends U> type)
Filterable
// (1, 2, 3) ReactiveSeq.of(1, "a", 2, "b",3).ofType(Integer.class)
public OptionalTValue<T> filterNot(java.util.function.Predicate<? super T> fn)
Filterable
of(1,2,3).filter(i->i>2);
//[1,2]
public OptionalTValue<T> notNull()
Filterable
of(1,2,null,4).nonNull();
//[1,2,4]
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object