CurveShiftFunctionFactory { public static DoublesCurve getShiftedCurve(final Curve<Double, Double> curve, final double shift) { if (curve instanceof ConstantDoublesCurve) { return CONSTANT.evaluate((ConstantDoublesCurve) curve, shift); } if (curve instanceof FunctionalDoublesCurve) { return FUNCTIONAL.evaluate((FunctionalDoublesCurve) curve, shift); } if (curve instanceof InterpolatedDoublesCurve) { return INTERPOLATED.evaluate((InterpolatedDoublesCurve) curve, shift); } if (curve instanceof SpreadDoublesCurve) { return SPREAD.evaluate((SpreadDoublesCurve) curve, shift); } throw new IllegalArgumentException("Do not have a curve shift function for curve " + curve.getClass()); }  static CurveShiftFunction<?> getFunction(final Class<?> clazz); static DoublesCurve getShiftedCurve(final Curve<Double, Double> curve, final double shift); static DoublesCurve getShiftedCurve(final Curve<Double, Double> curve, final double x, final double shift); static DoublesCurve getShiftedCurve(final Curve<Double, Double> curve, final double[] x, final double[] y); static DoublesCurve getShiftedCurve(final Curve<Double, Double> curve, final double shift, final String newName); static DoublesCurve getShiftedCurve(final Curve<Double, Double> curve, final double x, final double shift, final String newName); static DoublesCurve getShiftedCurve(final Curve<Double, Double> curve, final double[] x, final double[] y, final String newName); static final ConstantCurveShiftFunction CONSTANT; static final FunctionalCurveShiftFunction FUNCTIONAL; static final InterpolatedCurveShiftFunction INTERPOLATED; static final SpreadCurveShiftFunction SPREAD; }
@Test(expectedExceptions = IllegalArgumentException.class) public void testWrongCurveType1() { CurveShiftFunctionFactory.getShiftedCurve(DUMMY, 1); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testWrongCurveType2() { CurveShiftFunctionFactory.getShiftedCurve(DUMMY, 1, "M"); }
@Test public void testGetShiftedCurve1() { final double shift = 2; Curve<Double, Double> shifted = CurveShiftFunctionFactory.getShiftedCurve(CONSTANT, shift); Curve<Double, Double> expected = new ConstantCurveShiftFunction().evaluate(CONSTANT, shift); assertEquals(shifted.getClass(), expected.getClass()); assertEquals(shifted.getYValue(0.), expected.getYValue(0.)); shifted = CurveShiftFunctionFactory.getShiftedCurve(INTERPOLATED, shift); expected = new InterpolatedCurveShiftFunction().evaluate(INTERPOLATED, shift); assertEquals(shifted.getClass(), expected.getClass()); assertArrayEquals(shifted.getXData(), expected.getXData()); assertArrayEquals(shifted.getYData(), expected.getYData()); assertEquals(((InterpolatedDoublesCurve) shifted).getInterpolator(), ((InterpolatedDoublesCurve) expected).getInterpolator()); assertArrayEquals(shifted.getXData(), expected.getXData()); assertArrayEquals(shifted.getYData(), expected.getYData()); shifted = CurveShiftFunctionFactory.getShiftedCurve(FUNCTIONAL, shift); expected = new FunctionalCurveShiftFunction().evaluate(FUNCTIONAL, shift); assertEquals(shifted.getClass(), expected.getClass()); assertEquals(shifted.getYValue(0.), expected.getYValue(0.)); shifted = CurveShiftFunctionFactory.getShiftedCurve(SPREAD, shift); expected = new SpreadCurveShiftFunction().evaluate(SPREAD, shift); assertEquals(shifted.getClass(), expected.getClass()); assertEquals(shifted.getYValue(1.5), expected.getYValue(1.5)); final String newName = "F"; shifted = CurveShiftFunctionFactory.getShiftedCurve(CONSTANT, shift, newName); expected = new ConstantCurveShiftFunction().evaluate(CONSTANT, shift, newName); assertEquals(expected, shifted); shifted = CurveShiftFunctionFactory.getShiftedCurve(INTERPOLATED, shift, newName); expected = new InterpolatedCurveShiftFunction().evaluate(INTERPOLATED, shift, newName); assertEquals(expected, shifted); shifted = CurveShiftFunctionFactory.getShiftedCurve(FUNCTIONAL, shift, newName); expected = new FunctionalCurveShiftFunction().evaluate(FUNCTIONAL, shift, newName); assertEquals(shifted.getClass(), expected.getClass()); assertEquals(shifted.getYValue(0.), expected.getYValue(0.)); assertEquals(shifted.getName(), expected.getName()); shifted = CurveShiftFunctionFactory.getShiftedCurve(SPREAD, shift, newName); expected = new SpreadCurveShiftFunction().evaluate(SPREAD, shift, newName); assertEquals(shifted.getClass(), expected.getClass()); assertEquals(shifted.getYValue(1.5), expected.getYValue(1.5)); assertEquals(shifted.getName(), expected.getName()); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testWrongCurveType3() { CurveShiftFunctionFactory.getShiftedCurve(DUMMY, 1, 1); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testWrongCurveType4() { CurveShiftFunctionFactory.getShiftedCurve(DUMMY, 1, 1, "J"); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testUnsupportedShift1() { CurveShiftFunctionFactory.getShiftedCurve(CONSTANT, 1, 1); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testUnsupportedShift2() { CurveShiftFunctionFactory.getShiftedCurve(FUNCTIONAL, 1, 1); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testUnsupportedShift3() { CurveShiftFunctionFactory.getShiftedCurve(SPREAD, 1, 1); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testUnsupportedShift4() { CurveShiftFunctionFactory.getShiftedCurve(CONSTANT, 1, 1, "L"); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testUnsupportedShift5() { CurveShiftFunctionFactory.getShiftedCurve(FUNCTIONAL, 1, 1, "P"); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testUnsupportedShift6() { CurveShiftFunctionFactory.getShiftedCurve(SPREAD, 1, 1, "O"); }
@Test public void testGetShiftedCurve2() { final double x = 1; final double shift = 2; Curve<Double, Double> shifted = CurveShiftFunctionFactory.getShiftedCurve(INTERPOLATED, x, shift); Curve<Double, Double> expected = new InterpolatedCurveShiftFunction().evaluate(INTERPOLATED, x, shift); assertEquals(shifted.getClass(), expected.getClass()); assertArrayEquals(shifted.getXData(), expected.getXData()); assertArrayEquals(shifted.getYData(), expected.getYData()); assertEquals(((InterpolatedDoublesCurve) shifted).getInterpolator(), ((InterpolatedDoublesCurve) expected).getInterpolator()); assertArrayEquals(shifted.getXData(), expected.getXData()); assertArrayEquals(shifted.getYData(), expected.getYData()); final String newName = "H"; shifted = CurveShiftFunctionFactory.getShiftedCurve(INTERPOLATED, x, shift, newName); expected = new InterpolatedCurveShiftFunction().evaluate(INTERPOLATED, x, shift, newName); assertEquals(shifted, expected); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testWrongCurveType5() { CurveShiftFunctionFactory.getShiftedCurve(DUMMY, new double[] {1 }, new double[] {1 }); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testWrongCurveType6() { CurveShiftFunctionFactory.getShiftedCurve(DUMMY, new double[] {1 }, new double[] {1 }, "N"); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testUnsupportedShift7() { CurveShiftFunctionFactory.getShiftedCurve(CONSTANT, new double[] {1 }, new double[] {1 }); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testUnsupportedShift8() { CurveShiftFunctionFactory.getShiftedCurve(FUNCTIONAL, new double[] {1 }, new double[] {1 }); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testUnsupportedShift9() { CurveShiftFunctionFactory.getShiftedCurve(SPREAD, new double[] {1 }, new double[] {1 }); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testUnsupportedShift10() { CurveShiftFunctionFactory.getShiftedCurve(CONSTANT, new double[] {1 }, new double[] {1 }, "L"); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testUnsupportedShift11() { CurveShiftFunctionFactory.getShiftedCurve(FUNCTIONAL, new double[] {1 }, new double[] {1 }, "K"); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testUnsupportedShift12() { CurveShiftFunctionFactory.getShiftedCurve(SPREAD, new double[] {1 }, new double[] {1 }, "J"); }
@Test public void testGetShiftedCurve3() { final double[] x = new double[] {1 }; final double[] y = new double[] {2 }; Curve<Double, Double> shifted = CurveShiftFunctionFactory.getShiftedCurve(INTERPOLATED, x, y); Curve<Double, Double> expected = new InterpolatedCurveShiftFunction().evaluate(INTERPOLATED, x, y); assertEquals(shifted.getClass(), expected.getClass()); assertArrayEquals(shifted.getXData(), expected.getXData()); assertArrayEquals(shifted.getYData(), expected.getYData()); assertEquals(((InterpolatedDoublesCurve) shifted).getInterpolator(), ((InterpolatedDoublesCurve) expected).getInterpolator()); assertArrayEquals(shifted.getXData(), expected.getXData()); assertArrayEquals(shifted.getYData(), expected.getYData()); final String newName = "M"; shifted = CurveShiftFunctionFactory.getShiftedCurve(INTERPOLATED, x, y, newName); expected = new InterpolatedCurveShiftFunction().evaluate(INTERPOLATED, x, y, newName); assertEquals(shifted, expected); assertEquals(((InterpolatedDoublesCurve) shifted).getInterpolator(), ((InterpolatedDoublesCurve) expected).getInterpolator()); }