Package javascalautils
Class Some<T>
- java.lang.Object
-
- javascalautils.Some<T>
-
- Type Parameters:
T
- The type of the value represented by this instance
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable<T>
,Option<T>
public final class Some<T> extends java.lang.Object implements Option<T>, java.io.Serializable
Represents anOption
holding a value.
The instance ofSome
is guaranteed to keep a non-null value object.
Null values are not allowed as it implies an instance ofNone
.
One can considerSome
as a collection of size 1.
Either use the implementation directly:
Option<String> option = new Some<>("Peter is Great!");
or use the factory/apply method from Option:
Option<String> option = Option.apply("Peter is Great!");
For examples on usage refer to the documentation forOption
.- Since:
- 1.0
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from interface javascalautils.Option
DEFAULT_NONE
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(java.lang.Object other)
boolean
exists(java.util.function.Predicate<T> p)
Returnstrue
if the predicate matches the value.<R> Option<R>
flatMap(ThrowableFunction1<T,Option<R>> function)
Returns an Option consisting of the result of applying the given function to the current value.T
get()
Always returns the value for this instance.T
getOrElse(java.util.function.Supplier<T> s)
Always returns the value for this instance.int
hashCode()
Returns the hashCode based on the value of this instance.java.util.Iterator<T>
iterator()
Returns an iterator containing the single value of this instance.<R> Option<R>
map(ThrowableFunction1<T,R> f)
Returns anOption
containing the value of applying the given function to the current value.Option<T>
orElse(java.util.function.Supplier<Option<T>> s)
Always returns this.java.util.stream.Stream<T>
stream()
Returns a stream of size one containing the value of this instance.<R> Either<T,R>
toLeft(java.util.function.Supplier<R> right)
Returns aLeft
containing the value of this instance.<L> Either<L,T>
toRight(java.util.function.Supplier<L> left)
Returns aRight
containing the value of this instance.java.lang.String
toString()
Returns a String representation of the instance.
-
-
-
Constructor Detail
-
Some
public Some(T value)
Creates an instance for the provided value.
Null objects are not allowed and will render an exception.- Parameters:
value
- The value represented by this Some- Since:
- 1.0
-
-
Method Detail
-
exists
public boolean exists(java.util.function.Predicate<T> p)
Returnstrue
if the predicate matches the value.
-
get
public T get()
Always returns the value for this instance.
Guaranteed to return a non-null value.
-
getOrElse
public T getOrElse(java.util.function.Supplier<T> s)
Always returns the value for this instance.
-
map
public <R> Option<R> map(ThrowableFunction1<T,R> f)
Returns anOption
containing the value of applying the given function to the current value.
-
flatMap
public <R> Option<R> flatMap(ThrowableFunction1<T,Option<R>> function)
Returns an Option consisting of the result of applying the given function to the current value.
-
toLeft
public <R> Either<T,R> toLeft(java.util.function.Supplier<R> right)
Returns aLeft
containing the value of this instance.
-
toRight
public <L> Either<L,T> toRight(java.util.function.Supplier<L> left)
Returns aRight
containing the value of this instance.
-
equals
public boolean equals(java.lang.Object other)
Returnstrue
if the other object isSome
containing a value that equals the value of thisSome
, elsefalse
.- Overrides:
equals
in classjava.lang.Object
- Since:
- 1.0
-
hashCode
public int hashCode()
Returns the hashCode based on the value of this instance.- Overrides:
hashCode
in classjava.lang.Object
- Since:
- 1.0
-
toString
public java.lang.String toString()
Returns a String representation of the instance.- Overrides:
toString
in classjava.lang.Object
- Since:
- 1.0
-
iterator
public final java.util.Iterator<T> iterator()
Returns an iterator containing the single value of this instance.- Specified by:
iterator
in interfacejava.lang.Iterable<T>
- Returns:
- An iterator containing the single value
- Since:
- 1.0
-
stream
public final java.util.stream.Stream<T> stream()
Returns a stream of size one containing the value of this instance.- Returns:
- A stream containing the single value
- Since:
- 1.0
-
-