ValuePropertiesUtils { public static ValueProperties.Builder addAllOptional(final ValueProperties originalProperties, final ValueProperties propertiesToAdd) { ArgumentChecker.notNull(originalProperties, "original properties"); if (propertiesToAdd == null || propertiesToAdd.isEmpty()) { return originalProperties.copy(); } final ValueProperties.Builder newProperties = originalProperties.copy(); for (final String propertyName : propertiesToAdd.getProperties()) { final Set<String> propertyValue = propertiesToAdd.getValues(propertyName); if (originalProperties.getValues(propertyName) != null && !propertyValue.equals(originalProperties.getValues(propertyName))) { throw new IllegalStateException("Property " + propertyName + " already present, but value " + propertyValue + " not equal to " + originalProperties.getValues(propertyName)); } if (propertyValue == null || propertyValue.isEmpty()) { newProperties.withAny(propertyName).withOptional(propertyName); } else { newProperties.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 testAddAllOptional() { final ValueProperties properties = ValuePropertiesUtils.addAllOptional(CURVE_PROPERTIES, PAY_CURVE_PROPERTIES).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) .with(ValuePropertyNames.PAY_CURVE_CALCULATION_CONFIG, "PayCurveCalculationConfigName").withOptional(ValuePropertyNames.PAY_CURVE_CALCULATION_CONFIG) .get(); assertEquals(expectedProperties, properties); }
@Test(expectedExceptions = IllegalStateException.class) public void testAddAllOptionalDifferentValue() { final ValueProperties payCurveProperties = ValueProperties.builder() .with(ValuePropertyNames.CURVE, "PayCurveName") .with(ValuePropertyNames.PAY_CURVE_CALCULATION_CONFIG, "PayCurveCalculationConfigName") .get(); ValuePropertiesUtils.addAllOptional(CURVE_PROPERTIES, payCurveProperties).get(); }
@Test public void testAddAllOptionalSameProperty() { final ValueProperties payCurveProperties = ValueProperties.builder() .with(ValuePropertyNames.CURVE, "CurveName") .with(ValuePropertyNames.PAY_CURVE_CALCULATION_CONFIG, "PayCurveCalculationConfigName") .get(); final ValueProperties properties = ValuePropertiesUtils.addAllOptional(CURVE_PROPERTIES, payCurveProperties).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") .with(ValuePropertyNames.PAY_CURVE_CALCULATION_CONFIG, "PayCurveCalculationConfigName").withOptional(ValuePropertyNames.PAY_CURVE_CALCULATION_CONFIG) .get(); assertEquals(expectedProperties, properties); }