SpotRateScaling implements StructureManipulator<Double>, ImmutableBean { @Override public Double execute(Double spotRate, ValueSpecification valueSpecification, FunctionExecutionContext executionContext) { CurrencyPair currencyPair = SimulationUtils.getCurrencyPair(valueSpecification); if (_currencyPairs.contains(currencyPair)) { return applyScaling(spotRate); } else if (_currencyPairs.contains(currencyPair.inverse())) { double inverseRate = 1 / spotRate; double scaledRate = applyScaling(inverseRate); return 1 / scaledRate; } else { throw new IllegalArgumentException("Currency pair " + currencyPair + " shouldn't match " + _currencyPairs); } } SpotRateScaling(Number scalingFactor,
                                Number minRate,
                                Number maxRate,
                                CurrencyPair currencyPair);   SpotRateScaling(Number scalingFactor, Set<CurrencyPair> currencyPairs); private  SpotRateScaling(
      Double scalingFactor,
      double minRate,
      double maxRate,
      Set<CurrencyPair> currencyPairs); @Override Double execute(Double spotRate, ValueSpecification valueSpecification, FunctionExecutionContext executionContext); @Override Class<Double> getExpectedType(); static SpotRateScaling.Meta meta(); static SpotRateScaling.Builder builder(); @Override SpotRateScaling.Meta metaBean(); @Override Property<R> property(String propertyName); @Override Set<String> propertyNames(); Double getScalingFactor(); double getMinRate(); double getMaxRate(); Set<CurrencyPair> getCurrencyPairs(); Builder toBuilder(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString();  }
@Test public void normalPair() { assertEquals(4.4, UP_10.execute(4.0, valueSpec("EUR/USD"), new FunctionExecutionContext()), DELTA); assertEquals(5.5, UP_10.execute(5.0, valueSpec("CHF/JPY"), new FunctionExecutionContext()), DELTA); assertEquals(3.2, DOWN_20.execute(4.0, valueSpec("EUR/USD"), new FunctionExecutionContext()), DELTA); assertEquals(4.0, DOWN_20.execute(5.0, valueSpec("CHF/JPY"), new FunctionExecutionContext()), DELTA); }
@Test public void inversePair() { assertEquals(0.22727272727272727, UP_10.execute(0.25, valueSpec("USD/EUR"), new FunctionExecutionContext()), DELTA); assertEquals(0.18181818181818182, UP_10.execute(0.2, valueSpec("JPY/CHF"), new FunctionExecutionContext()), DELTA); assertEquals(0.3125, DOWN_20.execute(0.25, valueSpec("USD/EUR"), new FunctionExecutionContext()), DELTA); assertEquals(0.25, DOWN_20.execute(0.2, valueSpec("JPY/CHF"), new FunctionExecutionContext()), DELTA); }
@Test public void boundedRate() { SpotRateScaling up = new SpotRateScaling(2d, 0, 6d, CurrencyPair.parse("EUR/USD")); assertEquals(4d, up.execute(2d, valueSpec("EUR/USD"), new FunctionExecutionContext()), DELTA); assertEquals(6d, up.execute(5d, valueSpec("EUR/USD"), new FunctionExecutionContext()), DELTA); SpotRateScaling down = new SpotRateScaling(0.5, 0.3, 6d, CurrencyPair.parse("EUR/USD")); assertEquals(1d, down.execute(2d, valueSpec("EUR/USD"), new FunctionExecutionContext()), DELTA); assertEquals(0.3, down.execute(0.5, valueSpec("EUR/USD"), new FunctionExecutionContext()), DELTA); }
@Test public void boundedInverseRate() { SpotRateScaling up = new SpotRateScaling(2d, 0, 6d, CurrencyPair.parse("EUR/USD")); assertEquals(1d, up.execute(2d, valueSpec("USD/EUR"), new FunctionExecutionContext()), DELTA); assertEquals(0.166666666, up.execute(0.2, valueSpec("USD/EUR"), new FunctionExecutionContext()), DELTA); SpotRateScaling down = new SpotRateScaling(0.5, 0.2, 6d, CurrencyPair.parse("EUR/USD")); assertEquals(0.5, down.execute(0.25, valueSpec("USD/EUR"), new FunctionExecutionContext()), DELTA); assertEquals(5, down.execute(4d, valueSpec("USD/EUR"), new FunctionExecutionContext()), DELTA); }
@Test(expectedExceptions = IllegalArgumentException.class) public void unexpectedTargetType() { ValueProperties properties = ValueProperties.with(ValuePropertyNames.FUNCTION, "foo").get(); ComputationTargetSpecification targetSpec = new ComputationTargetSpecification(ComputationTargetType.CURRENCY, Currency.GBP.getUniqueId()); ValueSpecification valueSpec = new ValueSpecification("SpotRate", targetSpec, properties); UP_10.execute(2d, valueSpec, new FunctionExecutionContext()); }
@Test(expectedExceptions = IllegalArgumentException.class) public void unexpectedCurrencyPair() { UP_10.execute(2d, valueSpec("GBP/USD"), new FunctionExecutionContext()); }