/**
* Copyright (C) 2016 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.strata.collect.result;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import org.joda.beans.Bean;
import org.joda.beans.BeanBuilder;
import org.joda.beans.BeanDefinition;
import org.joda.beans.ImmutableBean;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaProperty;
import org.joda.beans.Property;
import org.joda.beans.PropertyDefinition;
import org.joda.beans.impl.direct.DirectFieldsBeanBuilder;
import org.joda.beans.impl.direct.DirectMetaBean;
import org.joda.beans.impl.direct.DirectMetaProperty;
import org.joda.beans.impl.direct.DirectMetaPropertyMap;
import com.google.common.collect.ImmutableList;
/**
* A value with associated failures.
* <p>
* 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.
* <p>
* 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 {@link List}
* or {@link Map}, which can be empty if the operation failed completely.
* <p>
* 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.
*
* @param <T> the type of the underlying success value, typically a collection type
*/
@BeanDefinition(builderScope = "private")
public final class ValueWithFailures<T>
implements ImmutableBean, Serializable {
/**
* The success value.
*/
@PropertyDefinition(validate = "notNull")
private final T value;
/**
* The failure items.
*/
@PropertyDefinition(validate = "notNull")
private final ImmutableList<FailureItem> failures;
//-------------------------------------------------------------------------
/**
* Creates an instance wrapping the success value and failures.
*
* @param <T> the type of the success value
* @param successValue the success value
* @param failures the failures
* @return an instance wrapping the value and failures
*/
public static <T> ValueWithFailures<T> of(T successValue, FailureItem... failures) {
return new ValueWithFailures<>(successValue, ImmutableList.copyOf(failures));
}
/**
* Creates an instance wrapping the success value and failures.
*
* @param <T> the type of the success value
* @param successValue the success value
* @param failures the failures
* @return an instance wrapping the value and failures
*/
public static <T> ValueWithFailures<T> of(T successValue, List<FailureItem> failures) {
return new ValueWithFailures<>(successValue, failures);
}
//-------------------------------------------------------------------------
/**
* Checks if there are any failures.
*
* @return true if there are any failures
*/
public boolean hasFailures() {
return !failures.isEmpty();
}
//------------------------- AUTOGENERATED START -------------------------
///CLOVER:OFF
/**
* The meta-bean for {@code ValueWithFailures}.
* @return the meta-bean, not null
*/
@SuppressWarnings("rawtypes")
public static ValueWithFailures.Meta meta() {
return ValueWithFailures.Meta.INSTANCE;
}
/**
* The meta-bean for {@code ValueWithFailures}.
* @param <R> the bean's generic type
* @param cls the bean's generic type
* @return the meta-bean, not null
*/
@SuppressWarnings("unchecked")
public static <R> ValueWithFailures.Meta<R> metaValueWithFailures(Class<R> cls) {
return ValueWithFailures.Meta.INSTANCE;
}
static {
JodaBeanUtils.registerMetaBean(ValueWithFailures.Meta.INSTANCE);
}
/**
* The serialization version id.
*/
private static final long serialVersionUID = 1L;
private ValueWithFailures(
T value,
List<FailureItem> failures) {
JodaBeanUtils.notNull(value, "value");
JodaBeanUtils.notNull(failures, "failures");
this.value = value;
this.failures = ImmutableList.copyOf(failures);
}
@SuppressWarnings("unchecked")
@Override
public ValueWithFailures.Meta<T> metaBean() {
return ValueWithFailures.Meta.INSTANCE;
}
@Override
public <R> Property<R> property(String propertyName) {
return metaBean().<R>metaProperty(propertyName).createProperty(this);
}
@Override
public Set<String> propertyNames() {
return metaBean().metaPropertyMap().keySet();
}
//-----------------------------------------------------------------------
/**
* Gets the success value.
* @return the value of the property, not null
*/
public T getValue() {
return value;
}
//-----------------------------------------------------------------------
/**
* Gets the failure items.
* @return the value of the property, not null
*/
public ImmutableList<FailureItem> getFailures() {
return failures;
}
//-----------------------------------------------------------------------
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj != null && obj.getClass() == this.getClass()) {
ValueWithFailures<?> other = (ValueWithFailures<?>) obj;
return JodaBeanUtils.equal(value, other.value) &&
JodaBeanUtils.equal(failures, other.failures);
}
return false;
}
@Override
public int hashCode() {
int hash = getClass().hashCode();
hash = hash * 31 + JodaBeanUtils.hashCode(value);
hash = hash * 31 + JodaBeanUtils.hashCode(failures);
return hash;
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder(96);
buf.append("ValueWithFailures{");
buf.append("value").append('=').append(value).append(',').append(' ');
buf.append("failures").append('=').append(JodaBeanUtils.toString(failures));
buf.append('}');
return buf.toString();
}
//-----------------------------------------------------------------------
/**
* The meta-bean for {@code ValueWithFailures}.
* @param <T> the type
*/
public static final class Meta<T> extends DirectMetaBean {
/**
* The singleton instance of the meta-bean.
*/
@SuppressWarnings("rawtypes")
static final Meta INSTANCE = new Meta();
/**
* The meta-property for the {@code value} property.
*/
@SuppressWarnings({"unchecked", "rawtypes" })
private final MetaProperty<T> value = (DirectMetaProperty) DirectMetaProperty.ofImmutable(
this, "value", ValueWithFailures.class, Object.class);
/**
* The meta-property for the {@code failures} property.
*/
@SuppressWarnings({"unchecked", "rawtypes" })
private final MetaProperty<ImmutableList<FailureItem>> failures = DirectMetaProperty.ofImmutable(
this, "failures", ValueWithFailures.class, (Class) ImmutableList.class);
/**
* The meta-properties.
*/
private final Map<String, MetaProperty<?>> metaPropertyMap$ = new DirectMetaPropertyMap(
this, null,
"value",
"failures");
/**
* Restricted constructor.
*/
private Meta() {
}
@Override
protected MetaProperty<?> metaPropertyGet(String propertyName) {
switch (propertyName.hashCode()) {
case 111972721: // value
return value;
case 675938345: // failures
return failures;
}
return super.metaPropertyGet(propertyName);
}
@Override
public BeanBuilder<? extends ValueWithFailures<T>> builder() {
return new ValueWithFailures.Builder<T>();
}
@SuppressWarnings({"unchecked", "rawtypes" })
@Override
public Class<? extends ValueWithFailures<T>> beanType() {
return (Class) ValueWithFailures.class;
}
@Override
public Map<String, MetaProperty<?>> metaPropertyMap() {
return metaPropertyMap$;
}
//-----------------------------------------------------------------------
/**
* The meta-property for the {@code value} property.
* @return the meta-property, not null
*/
public MetaProperty<T> value() {
return value;
}
/**
* The meta-property for the {@code failures} property.
* @return the meta-property, not null
*/
public MetaProperty<ImmutableList<FailureItem>> failures() {
return failures;
}
//-----------------------------------------------------------------------
@Override
protected Object propertyGet(Bean bean, String propertyName, boolean quiet) {
switch (propertyName.hashCode()) {
case 111972721: // value
return ((ValueWithFailures<?>) bean).getValue();
case 675938345: // failures
return ((ValueWithFailures<?>) bean).getFailures();
}
return super.propertyGet(bean, propertyName, quiet);
}
@Override
protected void propertySet(Bean bean, String propertyName, Object newValue, boolean quiet) {
metaProperty(propertyName);
if (quiet) {
return;
}
throw new UnsupportedOperationException("Property cannot be written: " + propertyName);
}
}
//-----------------------------------------------------------------------
/**
* The bean-builder for {@code ValueWithFailures}.
* @param <T> the type
*/
private static final class Builder<T> extends DirectFieldsBeanBuilder<ValueWithFailures<T>> {
private T value;
private List<FailureItem> failures = ImmutableList.of();
/**
* Restricted constructor.
*/
private Builder() {
}
//-----------------------------------------------------------------------
@Override
public Object get(String propertyName) {
switch (propertyName.hashCode()) {
case 111972721: // value
return value;
case 675938345: // failures
return failures;
default:
throw new NoSuchElementException("Unknown property: " + propertyName);
}
}
@SuppressWarnings("unchecked")
@Override
public Builder<T> set(String propertyName, Object newValue) {
switch (propertyName.hashCode()) {
case 111972721: // value
this.value = (T) newValue;
break;
case 675938345: // failures
this.failures = (List<FailureItem>) newValue;
break;
default:
throw new NoSuchElementException("Unknown property: " + propertyName);
}
return this;
}
@Override
public Builder<T> set(MetaProperty<?> property, Object value) {
super.set(property, value);
return this;
}
@Override
public Builder<T> setString(String propertyName, String value) {
setString(meta().metaProperty(propertyName), value);
return this;
}
@Override
public Builder<T> setString(MetaProperty<?> property, String value) {
super.setString(property, value);
return this;
}
@Override
public Builder<T> setAll(Map<String, ? extends Object> propertyValueMap) {
super.setAll(propertyValueMap);
return this;
}
@Override
public ValueWithFailures<T> build() {
return new ValueWithFailures<T>(
value,
failures);
}
//-----------------------------------------------------------------------
@Override
public String toString() {
StringBuilder buf = new StringBuilder(96);
buf.append("ValueWithFailures.Builder{");
buf.append("value").append('=').append(JodaBeanUtils.toString(value)).append(',').append(' ');
buf.append("failures").append('=').append(JodaBeanUtils.toString(failures));
buf.append('}');
return buf.toString();
}
}
///CLOVER:ON
//-------------------------- AUTOGENERATED END --------------------------
}