@Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case -823812830: // values return ((DefaultScenarioArray<?>) bean).getValues(); } return super.propertyGet(bean, propertyName, quiet); }
/** * Obtains an instance from the specified list of values. * * @param <T> the type of the value * @param values the values, one value for each scenario * @return an instance with the specified values */ public static <T> ScenarioArray<T> of(List<T> values) { return DefaultScenarioArray.of(values); }
/** * Obtains an instance from the specified list of values. * * @param <T> the type of the value * @param values the values, one value for each scenario * @return an instance with the specified values */ public static <T> DefaultScenarioArray<T> of(List<T> values) { return new DefaultScenarioArray<>(values); }
public void create() { DefaultScenarioArray<Integer> test = DefaultScenarioArray.of(1, 2, 3); assertThat(test.getValues()).isEqualTo(ImmutableList.of(1, 2, 3)); assertThat(test.getScenarioCount()).isEqualTo(3); assertThat(test.get(0)).isEqualTo(1); assertThat(test.get(1)).isEqualTo(2); assertThat(test.get(2)).isEqualTo(3); assertThat(test.stream().collect(toList())).isEqualTo(ImmutableList.of(1, 2, 3)); }
public void notConvertible() { FxRateScenarioArray rates = FxRateScenarioArray.of(GBP, USD, DoubleArray.of(1.61, 1.62, 1.63)); ScenarioFxRateProvider fxProvider = new TestScenarioFxRateProvider(rates); List<String> values = ImmutableList.of("a", "b", "c"); DefaultScenarioArray<String> test = DefaultScenarioArray.of(values); ScenarioArray<?> convertedList = test.convertedTo(Currency.GBP, fxProvider); assertThat(convertedList).isEqualTo(test); }
@Override public ScenarioArray<?> convertedTo(Currency resultCurrency, ScenarioFxRateProvider fxRateProvider) { int scenarioCount = getScenarioCount(); if (fxRateProvider.getScenarioCount() != scenarioCount) { throw new IllegalArgumentException(Messages.format( "Expected {} FX rates but received {}", scenarioCount, fxRateProvider.getScenarioCount())); } ImmutableList<Object> converted = zipWithIndex(values.stream()) .map(tp -> convert(resultCurrency, fxRateProvider, tp.getFirst(), tp.getSecond())) .collect(toImmutableList()); return DefaultScenarioArray.of(converted); }
public void create_withFunction() { DefaultScenarioArray<Integer> test = DefaultScenarioArray.of(3, i -> (i + 1)); assertThat(test.getValues()).isEqualTo(ImmutableList.of(1, 2, 3)); assertThat(test.getScenarioCount()).isEqualTo(3); assertThat(test.get(0)).isEqualTo(1); assertThat(test.get(1)).isEqualTo(2); assertThat(test.get(2)).isEqualTo(3); assertThat(test.stream().collect(toList())).isEqualTo(ImmutableList.of(1, 2, 3)); }
public void noConversionNecessary() { FxRateScenarioArray rates = FxRateScenarioArray.of(GBP, USD, DoubleArray.of(1.61, 1.62, 1.63)); ScenarioFxRateProvider fxProvider = new TestScenarioFxRateProvider(rates); List<CurrencyAmount> values = ImmutableList.of( CurrencyAmount.of(Currency.GBP, 1), CurrencyAmount.of(Currency.GBP, 2), CurrencyAmount.of(Currency.GBP, 3)); DefaultScenarioArray<CurrencyAmount> test = DefaultScenarioArray.of(values); ScenarioArray<?> convertedList = test.convertedTo(Currency.GBP, fxProvider); ScenarioArray<CurrencyAmount> expectedList = DefaultScenarioArray.of(values); assertThat(convertedList).isEqualTo(expectedList); }
public void wrongNumberOfFxRates() { FxRateScenarioArray rates = FxRateScenarioArray.of(GBP, USD, DoubleArray.of(1.61, 1.62, 1.63)); ScenarioFxRateProvider fxProvider = new TestScenarioFxRateProvider(rates); List<CurrencyAmount> values = ImmutableList.of( CurrencyAmount.of(Currency.GBP, 1), CurrencyAmount.of(Currency.GBP, 2)); DefaultScenarioArray<CurrencyAmount> test = DefaultScenarioArray.of(values); assertThrows( () -> test.convertedTo(Currency.USD, fxProvider), IllegalArgumentException.class, "Expected 2 FX rates but received 3"); }
/** * Obtains an instance using a function to create the entries. * <p> * The function is passed the scenario index and returns the value for that index. * * @param <T> the type of the value * @param scenarioCount the number of scenarios * @param valueFunction the function used to obtain each value * @return an instance initialized using the function * @throws IllegalArgumentException is size is zero or less */ public static <T> ScenarioArray<T> of(int scenarioCount, IntFunction<T> valueFunction) { return DefaultScenarioArray.of(scenarioCount, valueFunction); }
@Override public DefaultScenarioArray<T> build() { return new DefaultScenarioArray<>( values); }
public void convertCurrencyAmount() { FxRateScenarioArray rates = FxRateScenarioArray.of(GBP, USD, DoubleArray.of(1.61, 1.62, 1.63)); ScenarioFxRateProvider fxProvider = new TestScenarioFxRateProvider(rates); List<CurrencyAmount> values = ImmutableList.of( CurrencyAmount.of(Currency.GBP, 1), CurrencyAmount.of(Currency.GBP, 2), CurrencyAmount.of(Currency.GBP, 3)); DefaultScenarioArray<CurrencyAmount> test = DefaultScenarioArray.of(values); ScenarioArray<?> convertedList = test.convertedTo(Currency.USD, fxProvider); List<CurrencyAmount> expectedValues = ImmutableList.of( CurrencyAmount.of(Currency.USD, 1 * 1.61), CurrencyAmount.of(Currency.USD, 2 * 1.62), CurrencyAmount.of(Currency.USD, 3 * 1.63)); DefaultScenarioArray<CurrencyAmount> expectedList = DefaultScenarioArray.of(expectedValues); assertThat(convertedList).isEqualTo(expectedList); }
/** * Obtains an instance from the specified array of values. * * @param <T> the type of the value * @param values the values, one value for each scenario * @return an instance with the specified values */ @SafeVarargs public static <T> ScenarioArray<T> of(T... values) { return DefaultScenarioArray.of(values); }
/** * Obtains an instance from the specified array of values. * * @param <T> the type of the value * @param values the values, one value for each scenario * @return an instance with the specified values */ @SafeVarargs public static <T> DefaultScenarioArray<T> of(T... values) { return new DefaultScenarioArray<>(ImmutableList.copyOf(values)); }
public void missingFxRates() { FxRateScenarioArray rates = FxRateScenarioArray.of(EUR, USD, DoubleArray.of(1.61, 1.62, 1.63)); ScenarioFxRateProvider fxProvider = new TestScenarioFxRateProvider(rates); List<CurrencyAmount> values = ImmutableList.of( CurrencyAmount.of(Currency.GBP, 1), CurrencyAmount.of(Currency.GBP, 2), CurrencyAmount.of(Currency.GBP, 3)); DefaultScenarioArray<CurrencyAmount> test = DefaultScenarioArray.of(values); assertThrows(() -> test.convertedTo(Currency.USD, fxProvider), IllegalArgumentException.class); }
public void test_of_array() { ScenarioArray<String> test = ScenarioArray.of("1", "2", "3"); DefaultScenarioArray<String> expected = DefaultScenarioArray.of("1", "2", "3"); assertEquals(test, expected); }
/** * Obtains an instance using a function to create the entries. * <p> * The function is passed the scenario index and returns the value for that index. * * @param <T> the type of the value * @param scenarioCount the number of scenarios * @param valueFunction the function used to obtain each value * @return an instance initialized using the function * @throws IllegalArgumentException is size is zero or less */ public static <T> DefaultScenarioArray<T> of(int scenarioCount, IntFunction<T> valueFunction) { ArgChecker.notNegativeOrZero(scenarioCount, "scenarioCount"); ImmutableList.Builder<T> builder = ImmutableList.builder(); for (int i = 0; i < scenarioCount; i++) { builder.add(valueFunction.apply(i)); } return new DefaultScenarioArray<>(builder.build()); }
public void test_of_list() { ScenarioArray<String> test = ScenarioArray.of(ImmutableList.of("1", "2", "3")); DefaultScenarioArray<String> expected = DefaultScenarioArray.of("1", "2", "3"); assertEquals(test, expected); }
public void test_of_function() { ScenarioArray<String> test = ScenarioArray.of(3, i -> Integer.toString(i + 1)); DefaultScenarioArray<String> expected = DefaultScenarioArray.of("1", "2", "3"); assertEquals(test, expected); }
@Override public ScenarioArray<?> convertedTo(Currency reportingCurrency, ScenarioFxRateProvider fxRateProvider) { if (value instanceof FxConvertible<?>) { FxConvertible<?> convertible = (FxConvertible<?>) value; if (fxRateProvider.getScenarioCount() != scenarioCount) { throw new IllegalArgumentException(Messages.format( "Expected {} FX rates but received {}", scenarioCount, fxRateProvider.getScenarioCount())); } ImmutableList<Object> converted = IntStream.range(0, scenarioCount) .mapToObj(i -> convertible.convertedTo(reportingCurrency, fxRateProvider.fxRateProvider(i))) .collect(toImmutableList()); return DefaultScenarioArray.of(converted); } return this; }