Class ValueWithFailures<T>
- java.lang.Object
-
- com.opengamma.strata.collect.result.ValueWithFailures<T>
-
- Type Parameters:
T
- the type of the underlying success value, typically a collection type
- All Implemented Interfaces:
Serializable
,org.joda.beans.Bean
,org.joda.beans.ImmutableBean
public final class ValueWithFailures<T> extends Object implements org.joda.beans.ImmutableBean, Serializable
A value with associated failures.This captures a common use case where an operation can tolerate some failure. This is often referred to as partial success or partial failure. The class stores the value, of any object type, and a list of failures that may be empty.
The success value must be able to handle the case where everything fails. In most cases, the success value will be a collection type, such as
List
orMap
, which can be empty if the operation failed completely.The classic example is loading rows from a file, when some rows are valid and some are invalid. The partial result would contain the successful rows, with the list of failures containing an entry for each row that failed to parse.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ValueWithFailures.Meta<T>
The meta-bean forValueWithFailures
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <U,R>
ValueWithFailures<R>combinedWith(ValueWithFailures<U> other, BiFunction<T,U,R> combiner)
Combines this instance with another.static <T> ValueWithFailures<List<T>>
combineValuesAsList(Iterable<? extends ValueWithFailures<? extends T>> items)
Combines separate instances ofValueWithFailure
into a single instance, using a list to collect the values.static <T> ValueWithFailures<Set<T>>
combineValuesAsSet(Iterable<? extends ValueWithFailures<? extends T>> items)
Combines separate instances ofValueWithFailure
into a single instance, using a set to collect the values.static <T> BinaryOperator<ValueWithFailures<T>>
combiningValues(BinaryOperator<T> combiner)
Returns aBinaryOperator
that combinesValueWithFailures
objects using the provided combiner function.boolean
equals(Object obj)
<R> ValueWithFailures<R>
flatMap(Function<? super T,ValueWithFailures<R>> function)
Processes the value by applying a function that returns another result.ImmutableList<FailureItem>
getFailures()
Gets the failure items.T
getValue()
Gets the success value.boolean
hasFailures()
Checks if there are any failures.int
hashCode()
<R> ValueWithFailures<R>
map(Function<? super T,? extends R> function)
Processes the value by applying a function that alters the value.ValueWithFailures<T>
mapFailures(Function<FailureItem,FailureItem> function)
Processes the value by applying a function that alters the failures.static ValueWithFailures.Meta
meta()
The meta-bean forValueWithFailures
.ValueWithFailures.Meta<T>
metaBean()
static <R> ValueWithFailures.Meta<R>
metaValueWithFailures(Class<R> cls)
The meta-bean forValueWithFailures
.static <T> ValueWithFailures<T>
of(T successValue, FailureItem... failures)
Creates an instance wrapping the success value and failures.static <T> ValueWithFailures<T>
of(T successValue, Collection<FailureItem> failures)
Creates an instance wrapping the success value and failures.static <T> ValueWithFailures<T>
of(T emptyValue, Supplier<T> supplier)
Creates an instance using a supplier.static <T> ValueWithFailures<T>
of(T successValue, List<FailureItem> failures)
Creates an instance wrapping the success value and failures.static <T> Collector<Result<? extends T>,?,ValueWithFailures<List<T>>>
toCombinedResultsAsList()
Returns a collector that can be used to create a combinedValueWithFailure
from a stream ofResult
instances.static <T> Collector<ValueWithFailures<? extends T>,?,ValueWithFailures<List<T>>>
toCombinedValuesAsList()
Returns a collector that creates a combinedValueWithFailure
from a stream of separate instances, combining into an immutable list.static <T> Collector<ValueWithFailures<? extends T>,?,ValueWithFailures<Set<T>>>
toCombinedValuesAsSet()
Returns a collector that creates a combinedValueWithFailure
from a stream of separate instances, combining into an immutable set.String
toString()
static <T> Collector<ValueWithFailures<T>,?,ValueWithFailures<T>>
toValueWithFailures(T identityValue, BinaryOperator<T> operator)
Returns a collector that can be used to create a combinedValueWithFailure
from a stream of separate instances.ValueWithFailures<T>
withAdditionalFailures(List<FailureItem> additionalFailures)
Returns a new instance with the specified failures, retaining the current value.<R> ValueWithFailures<R>
withValue(ValueWithFailures<R> valueWithFailures)
Returns a new instance with the specified value, combining the failures.<R> ValueWithFailures<R>
withValue(R value)
Returns a new instance with the specified value, retaining the current failures.<R> ValueWithFailures<R>
withValue(R value, List<FailureItem> additionalFailures)
Returns a new instance with the specified value, combining the failures.
-
-
-
Method Detail
-
of
public static <T> ValueWithFailures<T> of(T successValue, FailureItem... failures)
Creates an instance wrapping the success value and failures.- Type Parameters:
T
- the type of the success value- Parameters:
successValue
- the success valuefailures
- the failures- Returns:
- an instance wrapping the value and failures
-
of
public static <T> ValueWithFailures<T> of(T successValue, List<FailureItem> failures)
Creates an instance wrapping the success value and failures.- Type Parameters:
T
- the type of the success value- Parameters:
successValue
- the success valuefailures
- the failures- Returns:
- an instance wrapping the value and failures
-
of
public static <T> ValueWithFailures<T> of(T successValue, Collection<FailureItem> failures)
Creates an instance wrapping the success value and failures.- Type Parameters:
T
- the type of the success value- Parameters:
successValue
- the success valuefailures
- the failures- Returns:
- an instance wrapping the value and failures
-
of
public static <T> ValueWithFailures<T> of(T emptyValue, Supplier<T> supplier)
Creates an instance using a supplier.If the supplier succeeds normally, the supplied value will be returned. If the supplier fails, the empty value will be returned along with a failure.
This recognizes and handles
FailureItemProvider
exceptions. If the exception type is not recognized, the failure item will have a reason ofERROR
.- Type Parameters:
T
- the type of the value- Parameters:
emptyValue
- the empty valuesupplier
- supplier of the result value- Returns:
- an instance containing the supplied value, or a failure if an exception is thrown
-
combiningValues
public static <T> BinaryOperator<ValueWithFailures<T>> combiningValues(BinaryOperator<T> combiner)
Returns aBinaryOperator
that combinesValueWithFailures
objects using the provided combiner function.This would be used as follows (with a static import):
stream.reduce(combiningValues(Guavate::concatToList)); stream.reduce(baseValueWithFailures, combiningValues(Guavate::concatToList));
This replaces code of the form:
stream.reduce((vwf1, vwf2) -> vwf1.combinedWith(vwf2, Guavate::concatToList)); stream.reduce(baseValueWithFailures, (vwf1, vwf2) -> vwf1.combinedWith(vwf2, Guavate::concatToList));
- Type Parameters:
T
- the type of the values- Parameters:
combiner
- the combiner of the values- Returns:
- the combining binary operator
-
toValueWithFailures
public static <T> Collector<ValueWithFailures<T>,?,ValueWithFailures<T>> toValueWithFailures(T identityValue, BinaryOperator<T> operator)
Returns a collector that can be used to create a combinedValueWithFailure
from a stream of separate instances.The
Collector
returned performs a reduction of itsValueWithFailures
input elements under a specifiedBinaryOperator
using the provided identity.This collects a
Stream<ValueWithFailures<T>>
to aValueWithFailures<T>
.- Type Parameters:
T
- the type of the success value in theValueWithFailures
- Parameters:
identityValue
- the identity valueoperator
- the operator used for the reduction.- Returns:
- a
Collector
-
toCombinedValuesAsList
public static <T> Collector<ValueWithFailures<? extends T>,?,ValueWithFailures<List<T>>> toCombinedValuesAsList()
Returns a collector that creates a combinedValueWithFailure
from a stream of separate instances, combining into an immutable list.This collects a
Stream<ValueWithFailures<T>>
to aValueWithFailures<List<T>>
.- Type Parameters:
T
- the type of the success value in theValueWithFailures
- Returns:
- a
Collector
-
toCombinedValuesAsSet
public static <T> Collector<ValueWithFailures<? extends T>,?,ValueWithFailures<Set<T>>> toCombinedValuesAsSet()
Returns a collector that creates a combinedValueWithFailure
from a stream of separate instances, combining into an immutable set.This collects a
Stream<ValueWithFailures<T>>
to aValueWithFailures<Set<T>>
.- Type Parameters:
T
- the type of the success value in theValueWithFailures
- Returns:
- a
Collector
-
toCombinedResultsAsList
public static <T> Collector<Result<? extends T>,?,ValueWithFailures<List<T>>> toCombinedResultsAsList()
Returns a collector that can be used to create a combinedValueWithFailure
from a stream ofResult
instances.This collects a
Stream<Result<T>>
to aValueWithFailures<List<T>>
.- Type Parameters:
T
- the type of the success value in theValueWithFailures
- Returns:
- a
Collector
-
combineValuesAsList
public static <T> ValueWithFailures<List<T>> combineValuesAsList(Iterable<? extends ValueWithFailures<? extends T>> items)
Combines separate instances ofValueWithFailure
into a single instance, using a list to collect the values.This converts
Iterable<ValueWithFailures<T>>
toValueWithFailures<List<T>>
.- Type Parameters:
T
- the type of the success value in theValueWithFailures
- Parameters:
items
- the items to combine- Returns:
- a new instance with a list of success values
-
combineValuesAsSet
public static <T> ValueWithFailures<Set<T>> combineValuesAsSet(Iterable<? extends ValueWithFailures<? extends T>> items)
Combines separate instances ofValueWithFailure
into a single instance, using a set to collect the values.This converts
Iterable<ValueWithFailures<T>>
toValueWithFailures<Set<T>>
.- Type Parameters:
T
- the type of the success value in theValueWithFailures
- Parameters:
items
- the items to combine- Returns:
- a new instance with a set of success values
-
hasFailures
public boolean hasFailures()
Checks if there are any failures.- Returns:
- true if there are any failures
-
map
public <R> ValueWithFailures<R> map(Function<? super T,? extends R> function)
Processes the value by applying a function that alters the value.This operation allows post-processing of a result value. The specified function represents a conversion to be performed on the value.
It is strongly advised to ensure that the function cannot throw an exception. Exceptions from the function are not caught.
- Type Parameters:
R
- the type of the value in the returned result- Parameters:
function
- the function to transform the value with- Returns:
- the transformed instance of value and failures
-
mapFailures
public ValueWithFailures<T> mapFailures(Function<FailureItem,FailureItem> function)
Processes the value by applying a function that alters the failures.This operation allows post-processing of a result failures. The specified function represents a conversion to be performed on the failures.
It is strongly advised to ensure that the function cannot throw an exception. Exceptions from the function are not caught.
- Parameters:
function
- the function to transform the failures with- Returns:
- the transformed instance of value and failures
-
flatMap
public <R> ValueWithFailures<R> flatMap(Function<? super T,ValueWithFailures<R>> function)
Processes the value by applying a function that returns another result.This operation allows post-processing of a result value. This is similar to
map(Function)
but the function returns aValueWithFailures
. The result of this method consists of the transformed value, and the combined list of failures.It is strongly advised to ensure that the function cannot throw an exception. Exceptions from the function are not caught.
- Type Parameters:
R
- the type of the value in the returned result- Parameters:
function
- the function to transform the value with- Returns:
- the transformed instance of value and failures
-
combinedWith
public <U,R> ValueWithFailures<R> combinedWith(ValueWithFailures<U> other, BiFunction<T,U,R> combiner)
Combines this instance with another.If both instances contain lists of the same type, the combining function will often be
Guavate::concatToList
.It is strongly advised to ensure that the function cannot throw an exception. Exceptions from the function are not caught.
- Type Parameters:
U
- the type of the value in the other instanceR
- the type of the value in the returned result- Parameters:
other
- the other instancecombiner
- the function that combines the two values- Returns:
- the combined instance of value and failures
-
withValue
public <R> ValueWithFailures<R> withValue(R value)
Returns a new instance with the specified value, retaining the current failures.This can be useful as an inline alternative to
map(Function)
.- Type Parameters:
R
- the type of the value in the returned result- Parameters:
value
- the new value- Returns:
- the combined instance of value and failures
-
withValue
public <R> ValueWithFailures<R> withValue(R value, List<FailureItem> additionalFailures)
Returns a new instance with the specified value, combining the failures.This can be useful as an inline alternative to
flatMap(Function)
.- Type Parameters:
R
- the type of the value in the returned result- Parameters:
value
- the new valueadditionalFailures
- the additional failures- Returns:
- the combined instance of value and failures
-
withValue
public <R> ValueWithFailures<R> withValue(ValueWithFailures<R> valueWithFailures)
Returns a new instance with the specified value, combining the failures.This can be useful as an inline alternative to
flatMap(Function)
.- Type Parameters:
R
- the type of the value in the returned result- Parameters:
valueWithFailures
- the new value with failures- Returns:
- the combined instance of value and failures
-
withAdditionalFailures
public ValueWithFailures<T> withAdditionalFailures(List<FailureItem> additionalFailures)
Returns a new instance with the specified failures, retaining the current value.- Parameters:
additionalFailures
- the additional failures- Returns:
- the combined instance of value and failures
-
meta
public static ValueWithFailures.Meta meta()
The meta-bean forValueWithFailures
.- Returns:
- the meta-bean, not null
-
metaValueWithFailures
public static <R> ValueWithFailures.Meta<R> metaValueWithFailures(Class<R> cls)
The meta-bean forValueWithFailures
.- Type Parameters:
R
- the bean's generic type- Parameters:
cls
- the bean's generic type- Returns:
- the meta-bean, not null
-
metaBean
public ValueWithFailures.Meta<T> metaBean()
- Specified by:
metaBean
in interfaceorg.joda.beans.Bean
-
getValue
public T getValue()
Gets the success value.- Returns:
- the value of the property, not null
-
getFailures
public ImmutableList<FailureItem> getFailures()
Gets the failure items.- Returns:
- the value of the property, not null
-
-