Package javascalautils
Class LeftProjection<L,R>
- java.lang.Object
-
- javascalautils.LeftProjection<L,R>
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable<L>
public class LeftProjection<L,R> extends java.lang.Object implements java.lang.Iterable<L>, java.io.SerializableThis is a left-biased wrapper for an instance ofEither.- Since:
- 1.1
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Option<T>asOption()booleanexists(java.util.function.Predicate<L> predicate)Option<Either<L,R>>filter(java.util.function.Predicate<L> predicate)<T> Either<T,R>flatMap(java.util.function.Function<L,Left<T,R>> function)booleanforAll(java.util.function.Predicate<L> predicate)Tget()Returns the value of the Right/Left side in case the projection matches the side, else throwNoSuchElementException.LgetOrElse(java.util.function.Supplier<L> supplier)Returns the value if this is aLeftelse the value provided by the supplier.java.util.Iterator<T>iterator()Returns the iterator of the Right/Left side in case the projection matches the side.<T> Either<T,R>map(java.util.function.Function<L,T> function)TorNull()Returns the value of this projection if it is of the correct type elsenull.java.util.stream.Stream<T>stream()Returns the stream of the Right/Left side in case the projection matches the side.
-
-
-
Method Detail
-
exists
public boolean exists(java.util.function.Predicate<L> predicate)
- Parameters:
predicate- The predicate to apply- Returns:
- If this is a
Leftand the predicate matches the value - Since:
- 1.1
-
getOrElse
public L getOrElse(java.util.function.Supplier<L> supplier)
Returns the value if this is aLeftelse the value provided by the supplier.- Parameters:
supplier- The supplier- Returns:
- The value
- Since:
- 1.1
-
filter
public Option<Either<L,R>> filter(java.util.function.Predicate<L> predicate)
Returns aSomewrapping theEitherif it's aLeftand the value of theLeftmatches the predicate, elseNone.- Parameters:
predicate- The predicate to apply- Returns:
- The resulting Option of the filter operation
- Since:
- 1.1
-
forAll
public boolean forAll(java.util.function.Predicate<L> predicate)
- Parameters:
predicate- The predicate to apply- Returns:
- If it is a match
- Since:
- 1.1
-
map
public <T> Either<T,R> map(java.util.function.Function<L,T> function)
If this projection contains aLeftthen a newLeftis returned containing the value from the originalRightmapped via the provided function, else the contained Either is returned as is.- Type Parameters:
T- The type to return as the newLeft- Parameters:
function- The function- Returns:
- Mapped Either
- Since:
- 1.1
-
flatMap
public <T> Either<T,R> flatMap(java.util.function.Function<L,Left<T,R>> function)
If this projection contains aLeftthen a newLeftis returned containing the value from the originalRightmapped via the provided function, else the contained Either is returned as is.- Type Parameters:
T- The type to return as the newLeft- Parameters:
function- The function- Returns:
- Mapped Either
- Since:
- 1.2
-
asOption
public Option<T> asOption()
Returns the value of the Right/Left inSomeside in case the projection matches the side, elseNone.
That is:
If this is aRightProjectionwith aRight
or
If this is aLeftProjectionwith aLeft.
Then then returnSomecontaining the value of theLeft/Right.
Any other case, returnNone.- Returns:
- The Option
- Since:
- 1.1
-
get
public T get()
Returns the value of the Right/Left side in case the projection matches the side, else throwNoSuchElementException.
That is:
If this is aRightProjectionwith aRight
or
If this is aLeftProjectionwith aLeft.
Then return the value of theLeft/Right.
Any other case, throwNoSuchElementException.- Returns:
- The value
- Since:
- 1.1
-
iterator
public java.util.Iterator<T> iterator()
Returns the iterator of the Right/Left side in case the projection matches the side.
That is:
If this is aRightProjectionwith aRight
or
If this is aLeftProjectionwith aLeft.
Then return an iterator with the value for theLeft/Right.
Else return an empty iterator- Specified by:
iteratorin interfacejava.lang.Iterable<T>- Returns:
- The iterator
- Since:
- 1.1
-
stream
public java.util.stream.Stream<T> stream()
Returns the stream of the Right/Left side in case the projection matches the side.
That is:
If this is aRightProjectionwith aRight
or
If this is aLeftProjectionwith aLeft.
Then return a stream with the value for theLeft/Right.
Else return an empty stream- Returns:
- The stream
- Since:
- 1.1
-
orNull
public T orNull()
Returns the value of this projection if it is of the correct type elsenull.
That is:
If this is aRightProjectionwith aRight
or
If this is aLeftProjectionwith aLeft.
Then return the value of theLeft/Right.
Any other case, returnnull.
In an essence this is a specialized version ofgetOrElse(Supplier)where the supplier returnsnull.
getOrElse(() -> null)
- Returns:
- The value or
null - Since:
- 1.1
-
-