/** * Copyright (C) 2014 - present by OpenGamma Inc. and the OpenGamma group of companies * * Please see distribution for license. */ package com.opengamma.integration.marketdata.manipulator.dsl; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedHashSet; 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.google.common.collect.ImmutableSet; import com.opengamma.engine.function.FunctionExecutionContext; import com.opengamma.engine.marketdata.manipulator.function.StructureManipulator; import com.opengamma.engine.value.ValueSpecification; import com.opengamma.financial.currency.CurrencyPair; import com.opengamma.util.ArgumentChecker; /** * Shifts a spot rate. * If the shift type is {@link ScenarioShiftType#ABSOLUTE absolute} the shift amount is added to the rate. * If the shift type is {@link ScenarioShiftType#RELATIVE relative} the rate is multiplied by (1 + shift amount). This * means a shift of +10% scales a value by 1.1 and a shift of -20% scales a value by 0.8. */ @BeanDefinition public final class SpotRateShift implements StructureManipulator<Double>, ImmutableBean { /** How the shift amount should be applied. */ @PropertyDefinition(validate = "notNull") private final ScenarioShiftType _shiftType; @PropertyDefinition private final double _shiftAmount; @PropertyDefinition(validate = "notNull") private final double _minRate; @PropertyDefinition(validate = "notNull") private final double _maxRate; @PropertyDefinition(validate = "notNull") private final Set<CurrencyPair> _currencyPairs; /* package */ SpotRateShift(ScenarioShiftType shiftType, Number shiftAmount, Set<CurrencyPair> currencyPairs) { _shiftType = ArgumentChecker.notNull(shiftType, "shiftType"); _shiftAmount = ArgumentChecker.notNull(shiftAmount, "shiftAmount").doubleValue(); _minRate = 0; _maxRate = Double.MAX_VALUE; _currencyPairs = ImmutableSet.copyOf(ArgumentChecker.notEmpty(currencyPairs, "currencyPairs")); } /* package */ SpotRateShift(ScenarioShiftType shiftType, Number shiftAmount, Number minRate, Number maxRate, CurrencyPair currencyPair) { _currencyPairs = ImmutableSet.of(ArgumentChecker.notNull(currencyPair, "currencyPair")); _shiftAmount = ArgumentChecker.notNull(shiftAmount, "shiftAmount").doubleValue(); _minRate = ArgumentChecker.notNull(minRate, "minRate").doubleValue(); _maxRate = ArgumentChecker.notNull(maxRate, "minRate").doubleValue(); _shiftType = ArgumentChecker.notNull(shiftType, "shiftType"); if (_minRate > _maxRate) { throw new IllegalArgumentException("Minimum rate must be less than or equal to maximum rate"); } if (_minRate < 0 || _maxRate < 0) { throw new IllegalArgumentException("Minimum and maximum rate must be greater than zero"); } } @Override public Double execute(Double spotRate, ValueSpecification valueSpecification, FunctionExecutionContext executionContext) { CurrencyPair currencyPair = SimulationUtils.getCurrencyPair(valueSpecification); if (_currencyPairs.contains(currencyPair)) { return applyShift(spotRate); } else if (_currencyPairs.contains(currencyPair.inverse())) { double inverseRate = 1 / spotRate; double shiftedRate = applyShift(inverseRate); return 1 / shiftedRate; } else { throw new IllegalArgumentException("Currency pair " + currencyPair + " shouldn't match " + _currencyPairs); } } private double applyShift(double rate) { double shiftedRate; switch (_shiftType) { case ABSOLUTE: shiftedRate = rate + _shiftAmount; break; case RELATIVE: shiftedRate = rate * (1 + _shiftAmount); break; default: throw new IllegalArgumentException("Unexpected shift type " + _shiftType); } if (shiftedRate < _minRate) { return _minRate; } else if (shiftedRate > _maxRate) { return _maxRate; } else { return shiftedRate; } } @Override public Class<Double> getExpectedType() { return Double.class; } //------------------------- AUTOGENERATED START ------------------------- ///CLOVER:OFF /** * The meta-bean for {@code SpotRateShift}. * @return the meta-bean, not null */ public static SpotRateShift.Meta meta() { return SpotRateShift.Meta.INSTANCE; } static { JodaBeanUtils.registerMetaBean(SpotRateShift.Meta.INSTANCE); } /** * Returns a builder used to create an instance of the bean. * @return the builder, not null */ public static SpotRateShift.Builder builder() { return new SpotRateShift.Builder(); } private SpotRateShift( ScenarioShiftType shiftType, double shiftAmount, double minRate, double maxRate, Set<CurrencyPair> currencyPairs) { JodaBeanUtils.notNull(shiftType, "shiftType"); JodaBeanUtils.notNull(minRate, "minRate"); JodaBeanUtils.notNull(maxRate, "maxRate"); JodaBeanUtils.notNull(currencyPairs, "currencyPairs"); this._shiftType = shiftType; this._shiftAmount = shiftAmount; this._minRate = minRate; this._maxRate = maxRate; this._currencyPairs = ImmutableSet.copyOf(currencyPairs); } @Override public SpotRateShift.Meta metaBean() { return SpotRateShift.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 how the shift amount should be applied. * @return the value of the property, not null */ public ScenarioShiftType getShiftType() { return _shiftType; } //----------------------------------------------------------------------- /** * Gets the shiftAmount. * @return the value of the property */ public double getShiftAmount() { return _shiftAmount; } //----------------------------------------------------------------------- /** * Gets the minRate. * @return the value of the property, not null */ public double getMinRate() { return _minRate; } //----------------------------------------------------------------------- /** * Gets the maxRate. * @return the value of the property, not null */ public double getMaxRate() { return _maxRate; } //----------------------------------------------------------------------- /** * Gets the currencyPairs. * @return the value of the property, not null */ public Set<CurrencyPair> getCurrencyPairs() { return _currencyPairs; } //----------------------------------------------------------------------- /** * 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()) { SpotRateShift other = (SpotRateShift) obj; return JodaBeanUtils.equal(getShiftType(), other.getShiftType()) && JodaBeanUtils.equal(getShiftAmount(), other.getShiftAmount()) && JodaBeanUtils.equal(getMinRate(), other.getMinRate()) && JodaBeanUtils.equal(getMaxRate(), other.getMaxRate()) && JodaBeanUtils.equal(getCurrencyPairs(), other.getCurrencyPairs()); } return false; } @Override public int hashCode() { int hash = getClass().hashCode(); hash = hash * 31 + JodaBeanUtils.hashCode(getShiftType()); hash = hash * 31 + JodaBeanUtils.hashCode(getShiftAmount()); hash = hash * 31 + JodaBeanUtils.hashCode(getMinRate()); hash = hash * 31 + JodaBeanUtils.hashCode(getMaxRate()); hash = hash * 31 + JodaBeanUtils.hashCode(getCurrencyPairs()); return hash; } @Override public String toString() { StringBuilder buf = new StringBuilder(192); buf.append("SpotRateShift{"); buf.append("shiftType").append('=').append(getShiftType()).append(',').append(' '); buf.append("shiftAmount").append('=').append(getShiftAmount()).append(',').append(' '); buf.append("minRate").append('=').append(getMinRate()).append(',').append(' '); buf.append("maxRate").append('=').append(getMaxRate()).append(',').append(' '); buf.append("currencyPairs").append('=').append(JodaBeanUtils.toString(getCurrencyPairs())); buf.append('}'); return buf.toString(); } //----------------------------------------------------------------------- /** * The meta-bean for {@code SpotRateShift}. */ 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 shiftType} property. */ private final MetaProperty<ScenarioShiftType> _shiftType = DirectMetaProperty.ofImmutable( this, "shiftType", SpotRateShift.class, ScenarioShiftType.class); /** * The meta-property for the {@code shiftAmount} property. */ private final MetaProperty<Double> _shiftAmount = DirectMetaProperty.ofImmutable( this, "shiftAmount", SpotRateShift.class, Double.TYPE); /** * The meta-property for the {@code minRate} property. */ private final MetaProperty<Double> _minRate = DirectMetaProperty.ofImmutable( this, "minRate", SpotRateShift.class, Double.TYPE); /** * The meta-property for the {@code maxRate} property. */ private final MetaProperty<Double> _maxRate = DirectMetaProperty.ofImmutable( this, "maxRate", SpotRateShift.class, Double.TYPE); /** * The meta-property for the {@code currencyPairs} property. */ @SuppressWarnings({"unchecked", "rawtypes" }) private final MetaProperty<Set<CurrencyPair>> _currencyPairs = DirectMetaProperty.ofImmutable( this, "currencyPairs", SpotRateShift.class, (Class) Set.class); /** * The meta-properties. */ private final Map<String, MetaProperty<?>> _metaPropertyMap$ = new DirectMetaPropertyMap( this, null, "shiftType", "shiftAmount", "minRate", "maxRate", "currencyPairs"); /** * Restricted constructor. */ private Meta() { } @Override protected MetaProperty<?> metaPropertyGet(String propertyName) { switch (propertyName.hashCode()) { case 893345500: // shiftType return _shiftType; case -1043480710: // shiftAmount return _shiftAmount; case 1063841362: // minRate return _minRate; case 844043364: // maxRate return _maxRate; case 1094810440: // currencyPairs return _currencyPairs; } return super.metaPropertyGet(propertyName); } @Override public SpotRateShift.Builder builder() { return new SpotRateShift.Builder(); } @Override public Class<? extends SpotRateShift> beanType() { return SpotRateShift.class; } @Override public Map<String, MetaProperty<?>> metaPropertyMap() { return _metaPropertyMap$; } //----------------------------------------------------------------------- /** * The meta-property for the {@code shiftType} property. * @return the meta-property, not null */ public MetaProperty<ScenarioShiftType> shiftType() { return _shiftType; } /** * 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 minRate} property. * @return the meta-property, not null */ public MetaProperty<Double> minRate() { return _minRate; } /** * The meta-property for the {@code maxRate} property. * @return the meta-property, not null */ public MetaProperty<Double> maxRate() { return _maxRate; } /** * The meta-property for the {@code currencyPairs} property. * @return the meta-property, not null */ public MetaProperty<Set<CurrencyPair>> currencyPairs() { return _currencyPairs; } //----------------------------------------------------------------------- @Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case 893345500: // shiftType return ((SpotRateShift) bean).getShiftType(); case -1043480710: // shiftAmount return ((SpotRateShift) bean).getShiftAmount(); case 1063841362: // minRate return ((SpotRateShift) bean).getMinRate(); case 844043364: // maxRate return ((SpotRateShift) bean).getMaxRate(); case 1094810440: // currencyPairs return ((SpotRateShift) bean).getCurrencyPairs(); } 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 SpotRateShift}. */ public static final class Builder extends DirectFieldsBeanBuilder<SpotRateShift> { private ScenarioShiftType _shiftType; private double _shiftAmount; private double _minRate; private double _maxRate; private Set<CurrencyPair> _currencyPairs = new HashSet<CurrencyPair>(); /** * Restricted constructor. */ private Builder() { } /** * Restricted copy constructor. * @param beanToCopy the bean to copy from, not null */ private Builder(SpotRateShift beanToCopy) { this._shiftType = beanToCopy.getShiftType(); this._shiftAmount = beanToCopy.getShiftAmount(); this._minRate = beanToCopy.getMinRate(); this._maxRate = beanToCopy.getMaxRate(); this._currencyPairs = new HashSet<CurrencyPair>(beanToCopy.getCurrencyPairs()); } //----------------------------------------------------------------------- @Override public Object get(String propertyName) { switch (propertyName.hashCode()) { case 893345500: // shiftType return _shiftType; case -1043480710: // shiftAmount return _shiftAmount; case 1063841362: // minRate return _minRate; case 844043364: // maxRate return _maxRate; case 1094810440: // currencyPairs return _currencyPairs; default: throw new NoSuchElementException("Unknown property: " + propertyName); } } @SuppressWarnings("unchecked") @Override public Builder set(String propertyName, Object newValue) { switch (propertyName.hashCode()) { case 893345500: // shiftType this._shiftType = (ScenarioShiftType) newValue; break; case -1043480710: // shiftAmount this._shiftAmount = (Double) newValue; break; case 1063841362: // minRate this._minRate = (Double) newValue; break; case 844043364: // maxRate this._maxRate = (Double) newValue; break; case 1094810440: // currencyPairs this._currencyPairs = (Set<CurrencyPair>) 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 SpotRateShift build() { return new SpotRateShift( _shiftType, _shiftAmount, _minRate, _maxRate, _currencyPairs); } //----------------------------------------------------------------------- /** * Sets the {@code shiftType} property in the builder. * @param shiftType the new value, not null * @return this, for chaining, not null */ public Builder shiftType(ScenarioShiftType shiftType) { JodaBeanUtils.notNull(shiftType, "shiftType"); this._shiftType = shiftType; return this; } /** * 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 minRate} property in the builder. * @param minRate the new value, not null * @return this, for chaining, not null */ public Builder minRate(double minRate) { JodaBeanUtils.notNull(minRate, "minRate"); this._minRate = minRate; return this; } /** * Sets the {@code maxRate} property in the builder. * @param maxRate the new value, not null * @return this, for chaining, not null */ public Builder maxRate(double maxRate) { JodaBeanUtils.notNull(maxRate, "maxRate"); this._maxRate = maxRate; return this; } /** * Sets the {@code currencyPairs} property in the builder. * @param currencyPairs the new value, not null * @return this, for chaining, not null */ public Builder currencyPairs(Set<CurrencyPair> currencyPairs) { JodaBeanUtils.notNull(currencyPairs, "currencyPairs"); this._currencyPairs = currencyPairs; return this; } /** * Sets the {@code currencyPairs} property in the builder * from an array of objects. * @param currencyPairs the new value, not null * @return this, for chaining, not null */ public Builder currencyPairs(CurrencyPair... currencyPairs) { return currencyPairs(new LinkedHashSet<CurrencyPair>(Arrays.asList(currencyPairs))); } //----------------------------------------------------------------------- @Override public String toString() { StringBuilder buf = new StringBuilder(192); buf.append("SpotRateShift.Builder{"); buf.append("shiftType").append('=').append(JodaBeanUtils.toString(_shiftType)).append(',').append(' '); buf.append("shiftAmount").append('=').append(JodaBeanUtils.toString(_shiftAmount)).append(',').append(' '); buf.append("minRate").append('=').append(JodaBeanUtils.toString(_minRate)).append(',').append(' '); buf.append("maxRate").append('=').append(JodaBeanUtils.toString(_maxRate)).append(',').append(' '); buf.append("currencyPairs").append('=').append(JodaBeanUtils.toString(_currencyPairs)); buf.append('}'); return buf.toString(); } } ///CLOVER:ON //-------------------------- AUTOGENERATED END -------------------------- }