/** * Includes the given Builder into this Builder. * * @param builder The Builder to include. * @param <T> Type of the Configuration to include. * @return This instance. */ public <T> Builder include(ConfigBuilder<T> builder) { builder.copyTo(this.properties); return this; }
static Properties extract(Properties source) { Properties result = new Properties(); for (val e : SHORTCUTS.entrySet()) { val builder = e.getKey().get(); boolean any = false; for (val s : e.getValue()) { val value = source.getProperty(s.key, null); if (value != null) { builder.withUnsafe(s.property, value); any = true; } } if (any) { builder.copyTo(result); } } return result; }
/** * Tests the copyTo() method. */ @Test public void testCopyTo() { final String namespace = "ns"; final int propertyCount = 10; val builder = new ConfigBuilder<TestConfig>(namespace, TestConfig::new); for (int i = 0; i < propertyCount; i++) { builder.with(Property.named(Integer.toString(i)), i); } Properties p2 = new Properties(); p2.setProperty(namespace + ".1", "a"); // Decoy - this should be overridden. builder.copyTo(p2); Assert.assertEquals("Unexpected number of properties copied.", propertyCount, p2.size()); for (int i = 0; i < propertyCount; i++) { val actualProp2 = p2.getProperty(namespace + "." + Integer.toString(i)); Assert.assertEquals("CopyTo did not set the correct values.", Integer.toString(i), actualProp2); } }