Package javascalautils
Class Success<T>
- java.lang.Object
-
- javascalautils.Success<T>
-
- Type Parameters:
T
- The type of the value represented by this instance
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable<T>
,Try<T>
public final class Success<T> extends java.lang.Object implements Try<T>, java.io.Serializable
Represents the successful implementation ofTry
.
Acts as a carrier for the result of a successful computation.
For examples on usage refer to the documentation forTry
.- Since:
- 1.0
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Try<java.lang.Throwable>
failed()
Returns aFailure
with anUnsupportedOperationException
.Try<T>
filter(java.util.function.Predicate<T> predicate)
Applies the predicate to the value of this instance, if it matches this is returned else aFailure
.<R> Try<R>
flatMap(ThrowableFunction1<T,Try<R>> function)
Applies the value to the function and returns theTry
generated by the function.T
get()
Returns the value held by this instance.T
getOrElse(java.util.function.Supplier<T> supplier)
Returns the value provided in the constructor.boolean
isSuccess()
Always returnstrue
.java.util.Iterator<T>
iterator()
Returns an iterator containing the single value of this instance.<R> Try<R>
map(ThrowableFunction1<T,R> function)
Applies the value to the function and returns theTry
representing the mapped value.Try<T>
orElse(java.util.function.Supplier<Try<T>> supplier)
Always returns this.Try<T>
recover(ThrowableFunction1<java.lang.Throwable,T> function)
Always returns this .Try<T>
recoverWith(ThrowableFunction1<java.lang.Throwable,Try<T>> function)
Always returns this .java.util.stream.Stream<T>
stream()
Returns a stream of size one containing the value of this instance.java.lang.String
toString()
Returns a String representation of the instance.
-
-
-
Constructor Detail
-
Success
public Success(T value)
Creates a 'successful' instance with the provided value.
Null values are allowed.- Parameters:
value
- The value represented by this instance- Since:
- 1.0
-
-
Method Detail
-
isSuccess
public boolean isSuccess()
Always returnstrue
.
-
getOrElse
public T getOrElse(java.util.function.Supplier<T> supplier)
Returns the value provided in the constructor.
I.e. the provided supplier is never use.
-
orElse
public Try<T> orElse(java.util.function.Supplier<Try<T>> supplier)
Always returns this.
I.e. the provided supplier is never use.
-
get
public T get()
Returns the value held by this instance.
Never throws an exception as this is a success
-
failed
public Try<java.lang.Throwable> failed()
Returns aFailure
with anUnsupportedOperationException
.
Since this is a success there is no failure to map to a success i.e. this is a illegal operation.
-
map
public <R> Try<R> map(ThrowableFunction1<T,R> function)
Applies the value to the function and returns theTry
representing the mapped value.
-
flatMap
public <R> Try<R> flatMap(ThrowableFunction1<T,Try<R>> function)
Applies the value to the function and returns theTry
generated by the function.
-
filter
public Try<T> filter(java.util.function.Predicate<T> predicate)
Applies the predicate to the value of this instance, if it matches this is returned else aFailure
.
-
recover
public Try<T> recover(ThrowableFunction1<java.lang.Throwable,T> function)
Always returns this .
As per definition this is a success and will not need to be recovered.
-
recoverWith
public Try<T> recoverWith(ThrowableFunction1<java.lang.Throwable,Try<T>> function)
Always returns this .
As per definition this is a success and will not need to be recovered.- Specified by:
recoverWith
in interfaceTry<T>
- Parameters:
function
- The function to apply in case of aFailure
- Returns:
- The recovered Try
- Since:
- 1.4
-
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
-
-