Package com.terheyden.value
Class Value2<B,C>
java.lang.Object
com.terheyden.value.Value2<B,C>
A stack of two values.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <X,Y> Value2<X, Y> empty()Returns an emptyValue2instance.booleanIndicates whether some other object is "equal to" thisValue2.If a value is present, and the value matches the given predicate, returns anValue2describing the value, otherwise returns an emptyValue2.If a value is present, returns the result of applying the givenValue2-bearing mapping function to the value, otherwise returns an emptyValue2.<D> Value1<D>flatReduceAll(io.vavr.CheckedFunction2<? super B, ? super C, Value1<? extends D>> reducer) Reduce all values to a single stack value.get()If a value is present, returns the value, otherwise throwsNoSuchElementException.inthashCode()Returns the hash code of the value, if present, otherwise0(zero) if no value is present.ifAllPresent(CheckedConsumer2<? super B, ? super C> action) Perform the given action using all values, if present.ifEmpty(io.vavr.CheckedRunnable emptyAction) Performs the empty-based action if no value is present, otherwise does nothing.If a value is present, performs the given action with the value, otherwise does nothing.ifPresentOrElse(io.vavr.CheckedConsumer<? super C> action, io.vavr.CheckedRunnable emptyAction) If a value is present, performs the given action with the value, otherwise performs the given empty-based action.booleanisEmpty()True if any value is null.booleanTrue if all values are non-null.If a value is present, returns anValue2describing (as if by#ofNullable) the result of applying the given mapping function to the value, otherwise returns an emptyValue2.If a value is present, returns anValue2describing the value, otherwise returns anValue2produced by the supplying function.If a value is present, returns anValue2describing the value, otherwise returns anValue2produced by the supplying function.If a value is present, returns anValue2describing the value, otherwise returns anValue2produced by the supplying function.If the last added value is empty, use the given value function along with all other present values to create a new value.If a value is present, returns anValue2describing the value, otherwise returns anValue2produced by the supplying function.If a value is present, returns the value, otherwise returnsother.If a value is present, returns the value, otherwise returns the result produced by the supplying function.If a value is present, returns the value, otherwise throwsNoSuchElementException.orElseThrow(io.vavr.CheckedFunction0<? extends X> exceptionSupplier) If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.<D> Value1<D>Reduce all values to a single stack value.stream()If a value is present, returns a sequentialStreamcontaining only that value, otherwise returns an emptyStream.Converts the last value to a JavaOptional.toString()Returns a non-empty string representation of thisValue2suitable for debugging.
-
Method Details
-
empty
Returns an emptyValue2instance. No value is present for thisValue2.- Type Parameters:
T- The type of the non-existent value- Returns:
- an empty
Value2
-
get
If a value is present, returns the value, otherwise throwsNoSuchElementException.- Returns:
- the non-
nullvalue described by thisValue2 - Throws:
NoSuchElementException- if no value is present
-
isPresent
public boolean isPresent()True if all values are non-null. -
isEmpty
public boolean isEmpty()True if any value is null. -
ifPresent
If a value is present, performs the given action with the value, otherwise does nothing.- Parameters:
action- the action to be performed, if a value is present- Returns:
- this, for chaining
- Throws:
NullPointerException- if value is present and the given action isnull
-
ifPresentOrElse
public Value2<B,C> ifPresentOrElse(io.vavr.CheckedConsumer<? super C> action, io.vavr.CheckedRunnable emptyAction) If a value is present, performs the given action with the value, otherwise performs the given empty-based action.- Parameters:
action- the action to be performed, if a value is presentemptyAction- the empty-based action to be performed, if no value is present- Returns:
- this, for chaining
- Throws:
NullPointerException- if a value is present and the given action isnull, or no value is present and the given empty-based action isnull.- Since:
- 9
-
ifAllPresent
Perform the given action using all values, if present. If any value is empty, does nothing.- Returns:
- this, for chaining
-
ifEmpty
Performs the empty-based action if no value is present, otherwise does nothing.- Parameters:
action- the action to be performed, if a value is presentemptyAction- the empty-based action to be performed, if no value is present- Returns:
- this, for chaining
- Throws:
NullPointerException- if a value is present and the given action isnull, or no value is present and the given empty-based action isnull.- Since:
- 9
-
filter
If a value is present, and the value matches the given predicate, returns anValue2describing the value, otherwise returns an emptyValue2.- Parameters:
predicate- the predicate to apply to a value, if present- Returns:
- an
Value2describing the value of thisValue2, if a value is present and the value matches the given predicate, otherwise an emptyValue2 - Throws:
NullPointerException- if the predicate isnull
-
map
If a value is present, returns anValue2describing (as if by#ofNullable) the result of applying the given mapping function to the value, otherwise returns an emptyValue2.If the mapping function returns a
nullresult then this method returns an emptyValue2.- Type Parameters:
U- The type of the value returned from the mapping function- Parameters:
mapper- the mapping function to apply to a value, if present- Returns:
- an
Value2describing the result of applying a mapping function to the value of thisValue2, if a value is present, otherwise an emptyValue2 - Throws:
NullPointerException- if the mapping function isnull
-
flatMap
public <D> Value2<B,D> flatMap(io.vavr.CheckedFunction1<? super C, ? extends Optional<? extends D>> mapper) If a value is present, returns the result of applying the givenValue2-bearing mapping function to the value, otherwise returns an emptyValue2.This method is similar to
#map(Function), but the mapping function is one whose result is already anValue2, and if invoked,flatMapdoes not wrap it within an additionalValue2.- Type Parameters:
D- The type of value of theValue2returned by the mapping function- Parameters:
mapper- the mapping function to apply to a value, if present- Returns:
- the result of applying an
Value2-bearing mapping function to the value of thisValue2, if a value is present, otherwise an emptyValue2 - Throws:
NullPointerException- if the mapping function isnullor returns anullresult
-
reduceAll
Reduce all values to a single stack value. -
flatReduceAll
public <D> Value1<D> flatReduceAll(io.vavr.CheckedFunction2<? super B, ? super C, Value1<? extends D>> reducer) Reduce all values to a single stack value. -
or
If a value is present, returns anValue2describing the value, otherwise returns anValue2produced by the supplying function.- Parameters:
supplier- the supplying function that produces anValue2to be returned- Returns:
- returns an
Value2describing the value of thisValue2, if a value is present, otherwise anValue2produced by the supplying function. - Throws:
NullPointerException- if the supplying function isnullor produces anullresult- Since:
- 9
-
or
If the last added value is empty, use the given value function along with all other present values to create a new value. -
or
If a value is present, returns anValue2describing the value, otherwise returns anValue2produced by the supplying function.- Parameters:
supplier- the supplying function that produces anValue2to be returned- Returns:
- returns an
Value2describing the value of thisValue2, if a value is present, otherwise anValue2produced by the supplying function. - Throws:
NullPointerException- if the supplying function isnullor produces anullresult- Since:
- 9
-
or
If a value is present, returns anValue2describing the value, otherwise returns anValue2produced by the supplying function.- Parameters:
supplier- the supplying function that produces anValue2to be returned- Returns:
- returns an
Value2describing the value of thisValue2, if a value is present, otherwise anValue2produced by the supplying function. - Throws:
NullPointerException- if the supplying function isnullor produces anullresult- Since:
- 9
-
or
If a value is present, returns anValue2describing the value, otherwise returns anValue2produced by the supplying function.- Parameters:
supplier- the supplying function that produces anValue2to be returned- Returns:
- returns an
Value2describing the value of thisValue2, if a value is present, otherwise anValue2produced by the supplying function. - Throws:
NullPointerException- if the supplying function isnullor produces anullresult- Since:
- 9
-
stream
If a value is present, returns a sequentialStreamcontaining only that value, otherwise returns an emptyStream.- Returns:
- the optional value as a
Stream - Since:
- 9
-
orElse
If a value is present, returns the value, otherwise returnsother.- Parameters:
other- the value to be returned, if no value is present. May benull.- Returns:
- the value, if present, otherwise
other
-
orElseGet
If a value is present, returns the value, otherwise returns the result produced by the supplying function.- Parameters:
supplier- the supplying function that produces a value to be returned- Returns:
- the value, if present, otherwise the result produced by the supplying function
- Throws:
NullPointerException- if no value is present and the supplying function isnull
-
orElseThrow
If a value is present, returns the value, otherwise throwsNoSuchElementException.- Returns:
- the non-
nullvalue described by thisValue2 - Throws:
NoSuchElementException- if no value is present- Since:
- 10
-
orElseThrow
public <X extends Throwable> C orElseThrow(io.vavr.CheckedFunction0<? extends X> exceptionSupplier) throws X If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.- Type Parameters:
X- Type of the exception to be thrown- Parameters:
exceptionSupplier- the supplying function that produces an exception to be thrown- Returns:
- the value, if present
- Throws:
X- if no value is presentNullPointerException- if no value is present and the exception supplying function isnull
-
toOptional
Converts the last value to a JavaOptional. -
equals
Indicates whether some other object is "equal to" thisValue2. The other object is considered equal if:- it is also an
Value2and; - both instances have no value present or;
- the present values are "equal to" each other via
equals().
- it is also an
-
hashCode
public int hashCode()Returns the hash code of the value, if present, otherwise0(zero) if no value is present. -
toString
Returns a non-empty string representation of thisValue2suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions.
-