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.Serializable
This 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()
boolean
exists(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)
boolean
forAll(java.util.function.Predicate<L> predicate)
T
get()
Returns the value of the Right/Left side in case the projection matches the side, else throwNoSuchElementException
.L
getOrElse(java.util.function.Supplier<L> supplier)
Returns the value if this is aLeft
else 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)
T
orNull()
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
Left
and the predicate matches the value - Since:
- 1.1
-
getOrElse
public L getOrElse(java.util.function.Supplier<L> supplier)
Returns the value if this is aLeft
else 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 aSome
wrapping theEither
if it's aLeft
and the value of theLeft
matches 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 aLeft
then a newLeft
is returned containing the value from the originalRight
mapped 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 aLeft
then a newLeft
is returned containing the value from the originalRight
mapped 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 inSome
side in case the projection matches the side, elseNone
.
That is:
If this is aRightProjection
with aRight
or
If this is aLeftProjection
with aLeft
.
Then then returnSome
containing 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 aRightProjection
with aRight
or
If this is aLeftProjection
with 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 aRightProjection
with aRight
or
If this is aLeftProjection
with aLeft
.
Then return an iterator with the value for theLeft
/Right
.
Else return an empty iterator- Specified by:
iterator
in 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 aRightProjection
with aRight
or
If this is aLeftProjection
with 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 aRightProjection
with aRight
or
If this is aLeftProjection
with 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
-
-