/**
* Copyright (C) 2015 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.strata.basics.value;
import java.io.Serializable;
import java.util.Set;
import org.joda.beans.BeanDefinition;
import org.joda.beans.ImmutableBean;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaBean;
import org.joda.beans.Property;
import org.joda.beans.PropertyDefinition;
import org.joda.beans.impl.light.LightMetaBean;
import com.opengamma.strata.collect.array.DoubleArray;
/**
* A value and its derivatives.
* <p>
* This defines a standard way to return a value and its derivatives to certain inputs.
* It is in particular used as a return object for Algorithmic Differentiation versions of some functions.
*/
@BeanDefinition(style = "light")
public final class ValueDerivatives
implements ImmutableBean, Serializable {
/**
* The value of the variable.
*/
@PropertyDefinition
private final double value;
/**
* The derivatives of the variable with respect to some inputs.
*/
@PropertyDefinition(validate = "notNull")
private final DoubleArray derivatives;
//-------------------------------------------------------------------------
/**
* Obtains an instance from a value and array of derivatives.
*
* @param value the value
* @param derivatives the derivatives of the value
* @return the object
*/
public static ValueDerivatives of(double value, DoubleArray derivatives) {
return new ValueDerivatives(value, derivatives);
}
//-------------------------------------------------------------------------
/**
* Gets the derivative of the variable with respect to an input.
*
* @param index the zero-based derivative to obtain
* @return the derivative
* @throws IndexOutOfBoundsException if the index is invalid
*/
public double getDerivative(int index) {
return derivatives.get(index);
}
//------------------------- AUTOGENERATED START -------------------------
///CLOVER:OFF
/**
* The meta-bean for {@code ValueDerivatives}.
*/
private static MetaBean META_BEAN = LightMetaBean.of(ValueDerivatives.class);
/**
* The meta-bean for {@code ValueDerivatives}.
* @return the meta-bean, not null
*/
public static MetaBean meta() {
return META_BEAN;
}
static {
JodaBeanUtils.registerMetaBean(META_BEAN);
}
/**
* The serialization version id.
*/
private static final long serialVersionUID = 1L;
private ValueDerivatives(
double value,
DoubleArray derivatives) {
JodaBeanUtils.notNull(derivatives, "derivatives");
this.value = value;
this.derivatives = derivatives;
}
@Override
public MetaBean metaBean() {
return META_BEAN;
}
@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 value of the variable.
* @return the value of the property
*/
public double getValue() {
return value;
}
//-----------------------------------------------------------------------
/**
* Gets the derivatives of the variable with respect to some inputs.
* @return the value of the property, not null
*/
public DoubleArray getDerivatives() {
return derivatives;
}
//-----------------------------------------------------------------------
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj != null && obj.getClass() == this.getClass()) {
ValueDerivatives other = (ValueDerivatives) obj;
return JodaBeanUtils.equal(value, other.value) &&
JodaBeanUtils.equal(derivatives, other.derivatives);
}
return false;
}
@Override
public int hashCode() {
int hash = getClass().hashCode();
hash = hash * 31 + JodaBeanUtils.hashCode(value);
hash = hash * 31 + JodaBeanUtils.hashCode(derivatives);
return hash;
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder(96);
buf.append("ValueDerivatives{");
buf.append("value").append('=').append(value).append(',').append(' ');
buf.append("derivatives").append('=').append(JodaBeanUtils.toString(derivatives));
buf.append('}');
return buf.toString();
}
///CLOVER:ON
//-------------------------- AUTOGENERATED END --------------------------
}