/**
* Copyright (C) 2015 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.sesame.marketdata.scenarios;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import org.joda.beans.Bean;
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.opengamma.analytics.ShiftType;
/**
* Perturbation that applies a shift to an FX rate.
* <p>
* Shifts can be absolute or relative. An absolute shift adds the shift amount to the rate. Relative shifts
* are defined in terms of how much to increase or decrease the rate by. e.g. a 10% shift multiplies the rate
* by 1.1, a -20% shift multiplies the rate by 0.8. So for relative shifts the shifted
* rate is {@code (rate x (1 + shiftAmount))}.
* <p>
* Inverse rates are handled correctly if the
* {@link CurrencyPairMatchDetails match details} indicate the matching rate was the inverse of the rate for which
* the filter was defined.
*/
@BeanDefinition
public final class FxRateShift implements Perturbation, ImmutableBean {
/** The amount of the shift. */
@PropertyDefinition
private final double _shiftAmount;
/** The type of shift. */
@PropertyDefinition(validate = "notNull")
private final ShiftType _shiftType;
/**
* Creates an absolute shift. The shift amount is added to the underlying FX rate.
*
* @param shiftAmount the amount added to the underlying FX rate
* @return a shift that adds the {@code shiftAmount} to the underlying FX rate
*/
public static FxRateShift absolute(double shiftAmount) {
return new FxRateShift(shiftAmount, ShiftType.ABSOLUTE);
}
/**
* Creates a relative shift that scales the underlying FX rate.
* <p>
* Relative shifts are defined in terms of how much to increase or decrease the rate by. e.g. a 10% shift (0.1)
* multiplies the rate by 1.1, a -20% shift (-0.2) multiplies the rate by 0.8. So for relative shifts the shifted
* rate is {@code (rate x (1 + shiftAmount))}.
*
* @param shiftAmount the amount to scale the rate by
* @return a shift that scales the underlying FX rate by the {@code shiftAmount}
*/
public static FxRateShift relative(double shiftAmount) {
return new FxRateShift(shiftAmount, ShiftType.RELATIVE);
}
@Override
public Object apply(Object marketData, MatchDetails matchDetails) {
double rate = (double) marketData;
CurrencyPairMatchDetails pairMatchDetails = (CurrencyPairMatchDetails) matchDetails;
if (pairMatchDetails.isInverse()) {
return 1 / _shiftType.applyShift(1 / rate, _shiftAmount);
} else {
return _shiftType.applyShift(rate, _shiftAmount);
}
}
@Override
public Class<Double> getMarketDataType() {
return Double.class;
}
@Override
public Class<? extends MatchDetails> getMatchDetailsType() {
return CurrencyPairMatchDetails.class;
}
@Override
public PerturbationTarget getTargetType() {
return PerturbationTarget.INPUT;
}
//------------------------- AUTOGENERATED START -------------------------
///CLOVER:OFF
/**
* The meta-bean for {@code FxRateShift}.
* @return the meta-bean, not null
*/
public static FxRateShift.Meta meta() {
return FxRateShift.Meta.INSTANCE;
}
static {
JodaBeanUtils.registerMetaBean(FxRateShift.Meta.INSTANCE);
}
/**
* Returns a builder used to create an instance of the bean.
* @return the builder, not null
*/
public static FxRateShift.Builder builder() {
return new FxRateShift.Builder();
}
private FxRateShift(
double shiftAmount,
ShiftType shiftType) {
JodaBeanUtils.notNull(shiftType, "shiftType");
this._shiftAmount = shiftAmount;
this._shiftType = shiftType;
}
@Override
public FxRateShift.Meta metaBean() {
return FxRateShift.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 amount of the shift.
* @return the value of the property
*/
public double getShiftAmount() {
return _shiftAmount;
}
//-----------------------------------------------------------------------
/**
* Gets the type of shift.
* @return the value of the property, not null
*/
public ShiftType getShiftType() {
return _shiftType;
}
//-----------------------------------------------------------------------
/**
* Returns a builder that allows this bean to be mutated.
* @return the mutable builder, not null
*/
public Builder toBuilder() {
return new Builder(this);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj != null && obj.getClass() == this.getClass()) {
FxRateShift other = (FxRateShift) obj;
return JodaBeanUtils.equal(getShiftAmount(), other.getShiftAmount()) &&
JodaBeanUtils.equal(getShiftType(), other.getShiftType());
}
return false;
}
@Override
public int hashCode() {
int hash = getClass().hashCode();
hash = hash * 31 + JodaBeanUtils.hashCode(getShiftAmount());
hash = hash * 31 + JodaBeanUtils.hashCode(getShiftType());
return hash;
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder(96);
buf.append("FxRateShift{");
buf.append("shiftAmount").append('=').append(getShiftAmount()).append(',').append(' ');
buf.append("shiftType").append('=').append(JodaBeanUtils.toString(getShiftType()));
buf.append('}');
return buf.toString();
}
//-----------------------------------------------------------------------
/**
* The meta-bean for {@code FxRateShift}.
*/
public static final class Meta extends DirectMetaBean {
/**
* The singleton instance of the meta-bean.
*/
static final Meta INSTANCE = new Meta();
/**
* The meta-property for the {@code shiftAmount} property.
*/
private final MetaProperty<Double> _shiftAmount = DirectMetaProperty.ofImmutable(
this, "shiftAmount", FxRateShift.class, Double.TYPE);
/**
* The meta-property for the {@code shiftType} property.
*/
private final MetaProperty<ShiftType> _shiftType = DirectMetaProperty.ofImmutable(
this, "shiftType", FxRateShift.class, ShiftType.class);
/**
* The meta-properties.
*/
private final Map<String, MetaProperty<?>> _metaPropertyMap$ = new DirectMetaPropertyMap(
this, null,
"shiftAmount",
"shiftType");
/**
* Restricted constructor.
*/
private Meta() {
}
@Override
protected MetaProperty<?> metaPropertyGet(String propertyName) {
switch (propertyName.hashCode()) {
case -1043480710: // shiftAmount
return _shiftAmount;
case 893345500: // shiftType
return _shiftType;
}
return super.metaPropertyGet(propertyName);
}
@Override
public FxRateShift.Builder builder() {
return new FxRateShift.Builder();
}
@Override
public Class<? extends FxRateShift> beanType() {
return FxRateShift.class;
}
@Override
public Map<String, MetaProperty<?>> metaPropertyMap() {
return _metaPropertyMap$;
}
//-----------------------------------------------------------------------
/**
* The meta-property for the {@code shiftAmount} property.
* @return the meta-property, not null
*/
public MetaProperty<Double> shiftAmount() {
return _shiftAmount;
}
/**
* The meta-property for the {@code shiftType} property.
* @return the meta-property, not null
*/
public MetaProperty<ShiftType> shiftType() {
return _shiftType;
}
//-----------------------------------------------------------------------
@Override
protected Object propertyGet(Bean bean, String propertyName, boolean quiet) {
switch (propertyName.hashCode()) {
case -1043480710: // shiftAmount
return ((FxRateShift) bean).getShiftAmount();
case 893345500: // shiftType
return ((FxRateShift) bean).getShiftType();
}
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 FxRateShift}.
*/
public static final class Builder extends DirectFieldsBeanBuilder<FxRateShift> {
private double _shiftAmount;
private ShiftType _shiftType;
/**
* Restricted constructor.
*/
private Builder() {
}
/**
* Restricted copy constructor.
* @param beanToCopy the bean to copy from, not null
*/
private Builder(FxRateShift beanToCopy) {
this._shiftAmount = beanToCopy.getShiftAmount();
this._shiftType = beanToCopy.getShiftType();
}
//-----------------------------------------------------------------------
@Override
public Object get(String propertyName) {
switch (propertyName.hashCode()) {
case -1043480710: // shiftAmount
return _shiftAmount;
case 893345500: // shiftType
return _shiftType;
default:
throw new NoSuchElementException("Unknown property: " + propertyName);
}
}
@Override
public Builder set(String propertyName, Object newValue) {
switch (propertyName.hashCode()) {
case -1043480710: // shiftAmount
this._shiftAmount = (Double) newValue;
break;
case 893345500: // shiftType
this._shiftType = (ShiftType) newValue;
break;
default:
throw new NoSuchElementException("Unknown property: " + propertyName);
}
return this;
}
@Override
public Builder set(MetaProperty<?> property, Object value) {
super.set(property, value);
return this;
}
@Override
public Builder setString(String propertyName, String value) {
setString(meta().metaProperty(propertyName), value);
return this;
}
@Override
public Builder setString(MetaProperty<?> property, String value) {
super.setString(property, value);
return this;
}
@Override
public Builder setAll(Map<String, ? extends Object> propertyValueMap) {
super.setAll(propertyValueMap);
return this;
}
@Override
public FxRateShift build() {
return new FxRateShift(
_shiftAmount,
_shiftType);
}
//-----------------------------------------------------------------------
/**
* Sets the {@code shiftAmount} property in the builder.
* @param shiftAmount the new value
* @return this, for chaining, not null
*/
public Builder shiftAmount(double shiftAmount) {
this._shiftAmount = shiftAmount;
return this;
}
/**
* Sets the {@code shiftType} property in the builder.
* @param shiftType the new value, not null
* @return this, for chaining, not null
*/
public Builder shiftType(ShiftType shiftType) {
JodaBeanUtils.notNull(shiftType, "shiftType");
this._shiftType = shiftType;
return this;
}
//-----------------------------------------------------------------------
@Override
public String toString() {
StringBuilder buf = new StringBuilder(96);
buf.append("FxRateShift.Builder{");
buf.append("shiftAmount").append('=').append(JodaBeanUtils.toString(_shiftAmount)).append(',').append(' ');
buf.append("shiftType").append('=').append(JodaBeanUtils.toString(_shiftType));
buf.append('}');
return buf.toString();
}
}
///CLOVER:ON
//-------------------------- AUTOGENERATED END --------------------------
}