T - the type of the underlying success value, typically a collection typepublic final class ValueWithFailures<T> extends Object implements org.joda.beans.ImmutableBean, Serializable
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
or Map, 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.
| Modifier and Type | Class and Description |
|---|---|
static class |
ValueWithFailures.Meta<T>
The meta-bean for
ValueWithFailures. |
| Modifier and Type | Method and 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 of
ValueWithFailure 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 of
ValueWithFailure into a single instance,
using a set to collect the values. |
static <T> BinaryOperator<ValueWithFailures<T>> |
combiningValues(BinaryOperator<T> combiner)
Returns a
BinaryOperator that combines ValueWithFailures 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.
|
static ValueWithFailures.Meta |
meta()
The meta-bean for
ValueWithFailures. |
ValueWithFailures.Meta<T> |
metaBean() |
static <R> ValueWithFailures.Meta<R> |
metaValueWithFailures(Class<R> cls)
The meta-bean for
ValueWithFailures. |
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 successValue,
FailureItem... failures)
Creates an instance wrapping the success value and failures.
|
static <T> ValueWithFailures<T> |
of(T successValue,
List<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> Collector<ValueWithFailures<? extends T>,?,ValueWithFailures<List<T>>> |
toCombinedValuesAsList()
Returns a collector that creates a combined
ValueWithFailure 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 combined
ValueWithFailure 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 combined
ValueWithFailure
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(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.
|
<R> ValueWithFailures<R> |
withValue(ValueWithFailures<R> valueWithFailures)
Returns a new instance with the specified value, combining the failures.
|
public static <T> ValueWithFailures<T> of(T successValue, FailureItem... failures)
T - the type of the success valuesuccessValue - the success valuefailures - the failurespublic static <T> ValueWithFailures<T> of(T successValue, List<FailureItem> failures)
T - the type of the success valuesuccessValue - the success valuefailures - the failurespublic static <T> ValueWithFailures<T> of(T successValue, Collection<FailureItem> failures)
T - the type of the success valuesuccessValue - the success valuefailures - the failurespublic static <T> ValueWithFailures<T> of(T emptyValue, Supplier<T> 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.
T - the type of the valueemptyValue - the empty valuesupplier - supplier of the result valuepublic static <T> BinaryOperator<ValueWithFailures<T>> combiningValues(BinaryOperator<T> combiner)
BinaryOperator that combines ValueWithFailures 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));
T - the type of the valuescombiner - the combiner of the valuespublic static <T> Collector<ValueWithFailures<T>,?,ValueWithFailures<T>> toValueWithFailures(T identityValue, BinaryOperator<T> operator)
ValueWithFailure
from a stream of separate instances.
The Collector returned performs a reduction of its ValueWithFailures input elements under a
specified BinaryOperator using the provided identity.
This collects a Stream<ValueWithFailures<T>> to a ValueWithFailures<T>.
T - the type of the success value in the ValueWithFailuresidentityValue - the identity valueoperator - the operator used for the reduction.Collectorpublic static <T> Collector<ValueWithFailures<? extends T>,?,ValueWithFailures<List<T>>> toCombinedValuesAsList()
ValueWithFailure from a stream
of separate instances, combining into an immutable list.
This collects a Stream<ValueWithFailures<T>> to a ValueWithFailures<List<T>>.
T - the type of the success value in the ValueWithFailuresCollectorpublic static <T> Collector<ValueWithFailures<? extends T>,?,ValueWithFailures<Set<T>>> toCombinedValuesAsSet()
ValueWithFailure from a stream
of separate instances, combining into an immutable set.
This collects a Stream<ValueWithFailures<T>> to a ValueWithFailures<Set<T>>.
T - the type of the success value in the ValueWithFailuresCollectorpublic static <T> ValueWithFailures<List<T>> combineValuesAsList(Iterable<? extends ValueWithFailures<? extends T>> items)
ValueWithFailure into a single instance,
using a list to collect the values.
This converts Iterable<ValueWithFailures<T>> to ValueWithFailures<List<T>>.
T - the type of the success value in the ValueWithFailuresitems - the items to combinepublic static <T> ValueWithFailures<Set<T>> combineValuesAsSet(Iterable<? extends ValueWithFailures<? extends T>> items)
ValueWithFailure into a single instance,
using a set to collect the values.
This converts Iterable<ValueWithFailures<T>> to ValueWithFailures<Set<T>>.
T - the type of the success value in the ValueWithFailuresitems - the items to combinepublic boolean hasFailures()
public <R> ValueWithFailures<R> map(Function<? super T,? extends R> function)
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.
R - the type of the value in the returned resultfunction - the function to transform the value withpublic <R> ValueWithFailures<R> flatMap(Function<? super T,ValueWithFailures<R>> function)
This operation allows post-processing of a result value.
This is similar to map(Function) but the function returns a ValueWithFailures.
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.
R - the type of the value in the returned resultfunction - the function to transform the value withpublic <U,R> ValueWithFailures<R> combinedWith(ValueWithFailures<U> other, BiFunction<T,U,R> combiner)
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.
U - the type of the value in the other instanceR - the type of the value in the returned resultother - the other instancecombiner - the function that combines the two valuespublic <R> ValueWithFailures<R> withValue(R value)
This can be useful as an inline alternative to map(Function).
R - the type of the value in the returned resultvalue - the new valuepublic <R> ValueWithFailures<R> withValue(R value, List<FailureItem> additionalFailures)
This can be useful as an inline alternative to flatMap(Function).
R - the type of the value in the returned resultvalue - the new valueadditionalFailures - the additional failurespublic <R> ValueWithFailures<R> withValue(ValueWithFailures<R> valueWithFailures)
This can be useful as an inline alternative to flatMap(Function).
R - the type of the value in the returned resultvalueWithFailures - the new value with failurespublic ValueWithFailures<T> withAdditionalFailures(List<FailureItem> additionalFailures)
additionalFailures - the additional failurespublic static ValueWithFailures.Meta meta()
ValueWithFailures.public static <R> ValueWithFailures.Meta<R> metaValueWithFailures(Class<R> cls)
ValueWithFailures.R - the bean's generic typecls - the bean's generic typepublic ValueWithFailures.Meta<T> metaBean()
metaBean in interface org.joda.beans.Beanpublic T getValue()
public ImmutableList<FailureItem> getFailures()
Copyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.