ValuePropertiesUtils { public static ValueProperties.Builder addOptional(final ValueProperties originalProperties, final String propertyName, final String propertyValue) { ArgumentChecker.notNull(originalProperties, "original properties"); ArgumentChecker.notNull(propertyName, "property name"); ArgumentChecker.notNull(propertyValue, "property value"); final Set<String> originalPropertyValues = originalProperties.getValues(propertyName); if (originalPropertyValues != null && !originalPropertyValues.equals(Collections.singleton(propertyValue))) { throw new IllegalStateException("Property " + propertyName + " already present, but value " + propertyValue + " not equal to " + originalProperties.getValues(propertyName)); } final ValueProperties.Builder newProperties = originalProperties.copy() .with(propertyName, propertyValue) .withOptional(propertyName); return newProperties; }  static ValueProperties.Builder addOptional(final ValueProperties originalProperties, final String propertyName, final String propertyValue); static ValueProperties.Builder addAllOptional(final ValueProperties originalProperties, final ValueProperties propertiesToAdd); static void withAllOptional(final ValueProperties.Builder properties, final ValueProperties propertiesToAdd); static ValueProperties.Builder removeAll(final ValueProperties originalProperties, final String... propertiesToRemove); static ValueProperties.Builder getAllOptional(final ValueProperties originalProperties);  }
@Test public void testAddOptional() { final ValueProperties properties = ValuePropertiesUtils.addOptional(CURVE_PROPERTIES, ValuePropertyNames.PAY_CURVE, "PayCurveName").get(); assertFalse(CURVE_PROPERTIES == properties); final ValueProperties expectedProperties = ValueProperties.builder() .with(ValuePropertyNames.CURVE, "CurveName") .with(ValuePropertyNames.CURVE_CALCULATION_CONFIG, "CurveCalculationConfigName") .with(ValuePropertyNames.CURVE_CALCULATION_METHOD, "CurveCalculationMethodName") .with(ValuePropertyNames.PAY_CURVE, "PayCurveName").withOptional(ValuePropertyNames.PAY_CURVE) .get(); assertEquals(expectedProperties, properties); }
@Test(expectedExceptions = IllegalStateException.class) public void testAddOptionalDifferentValue() { ValuePropertiesUtils.addOptional(CURVE_PROPERTIES, ValuePropertyNames.CURVE, "OtherCurveName").get(); }
@Test public void testAddOptionalSameProperty() { final ValueProperties properties = ValuePropertiesUtils.addOptional(CURVE_PROPERTIES, ValuePropertyNames.CURVE, "CurveName").get(); assertFalse(CURVE_PROPERTIES == properties); final ValueProperties expectedProperties = ValueProperties.builder() .with(ValuePropertyNames.CURVE, "CurveName").withOptional(ValuePropertyNames.CURVE) .with(ValuePropertyNames.CURVE_CALCULATION_CONFIG, "CurveCalculationConfigName") .with(ValuePropertyNames.CURVE_CALCULATION_METHOD, "CurveCalculationMethodName") .get(); assertEquals(expectedProperties, properties); }