Skip to content

Commit

Permalink
Add MapStream method to NodalCurve, similar to CurrencyParameterSensi…
Browse files Browse the repository at this point in the history
…tivity (#1898)

* Add `values` stream to NodalCurve

* Test
  • Loading branch information
Will Nicholson authored and jodastephen committed Feb 28, 2019
1 parent 072bdea commit 62c0845
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package com.opengamma.strata.market.curve;

import com.opengamma.strata.collect.MapStream;
import com.opengamma.strata.collect.array.DoubleArray;
import com.opengamma.strata.market.param.ParameterMetadata;
import com.opengamma.strata.market.param.ParameterPerturbation;
Expand Down Expand Up @@ -91,6 +92,18 @@ public default ParameterMetadata getParameterMetadata(int parameterIndex) {
*/
public abstract NodalCurve withValues(DoubleArray xValues, DoubleArray yValues);

//-------------------------------------------------------------------------
/**
* Converts this instance to a stream of y-values, keyed by the x-values.
* <p>
* This returns a {@link MapStream} keyed by the x-values.
*
* @return a map stream containing the x-values and the y-values
*/
public default MapStream<Double, Double> values() {
return MapStream.zip(getXValues().stream().boxed(), getYValues().stream().boxed());
}

//-------------------------------------------------------------------------
@Override
public abstract NodalCurve withParameter(int parameterIndex, double newValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.testng.annotations.Test;

import com.google.common.collect.ImmutableMap;
import com.opengamma.strata.collect.array.DoubleArray;
import com.opengamma.strata.market.ValueType;
import com.opengamma.strata.market.param.ParameterMetadata;
Expand Down Expand Up @@ -90,6 +91,7 @@ public void test_values() {
assertThat(test.yValueParameterSensitivity(10.2421).getMarketDataName()).isEqualTo(CURVE_NAME);
assertThat(test.yValueParameterSensitivity(10.2421).getSensitivity()).isEqualTo(DoubleArray.of(1d));
assertThat(test.firstDerivative(10.2421)).isEqualTo(0d);
assertThat(test.values().toMap()).isEqualTo(ImmutableMap.of(XVALUE, YVALUE));
}

//-------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static com.opengamma.strata.collect.TestHelper.coverBeanEquals;
import static com.opengamma.strata.collect.TestHelper.coverImmutableBean;
import static java.time.temporal.ChronoUnit.MONTHS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.assertEquals;

import java.time.LocalDate;
Expand All @@ -17,6 +18,7 @@

import org.testng.annotations.Test;

import com.opengamma.strata.collect.MapStream;
import com.opengamma.strata.collect.array.DoubleArray;
import com.opengamma.strata.market.ShiftType;
import com.opengamma.strata.market.curve.interpolator.CurveInterpolator;
Expand Down Expand Up @@ -197,6 +199,7 @@ public void coverage() {
.of(CURVE2_NOFIX, VAL_DATE_2, LAST_FIX_MONTH_2, LAST_FIX_VALUE + 1.0d,
SEASONALITY_ADDITIVE_DEF);
coverBeanEquals(test, test2);
assertThat(test.values().toMap()).isEqualTo(MapStream.zip(TIMES.stream().boxed(), VALUES.stream().boxed()).toMap());
}

public void test_serialization() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.testng.annotations.Test;

import com.google.common.collect.ImmutableMap;
import com.opengamma.strata.basics.currency.Currency;
import com.opengamma.strata.collect.array.DoubleArray;
import com.opengamma.strata.market.ValueType;
Expand Down Expand Up @@ -249,6 +250,7 @@ public void coverage() {
.extrapolatorRight(CurveExtrapolators.LOG_LINEAR)
.build();
coverBeanEquals(test, test2);
assertThat(test.values().toMap()).isEqualTo(ImmutableMap.of(1d, 5d, 2d, 7d, 3d, 8d));
}

public void test_serialization() {
Expand Down

0 comments on commit 62c0845

Please sign in to comment.