private <T> T tryGet(Property<T> property, Function<String, T> converter) { String fullKeyName = this.keyPrefix + property.getName(); // Get value from config. String value = this.properties.getProperty(fullKeyName, null); if (value == null) { // 2. Nothing in the configuration for this Property. if (property.hasDefaultValue()) { return property.getDefaultValue(); } else { throw new MissingPropertyException(fullKeyName); } } try { return converter.apply(value.trim()); } catch (IllegalArgumentException ex) { throw new InvalidPropertyValueException(fullKeyName, value, ex); } }
/** * Creates a new instance of the Property class with no default value. * * @param name The name of the property. * @param <T> The type of the property values. * @return A new instance of the Property class with no default value. */ public static <T> Property<T> named(String name) { return new Property<>(name, null); }
public static MetricsConfig getMetricsConfig() { val builder = MetricsConfig.builder(); for (Map.Entry<String, ConfigValue> e : CONFIG.entrySet()) { if (e.getKey().startsWith(METRIC_PATH)) { builder.with(Property.named(e.getKey().replaceFirst(METRIC_PATH, "")), e.getValue().unwrapped()); } } return builder.build(); } }
@SuppressWarnings("rawtypes") Property p = (Property) f.get(null); if (p.getDefaultValue() != null && p.getDefaultValue() instanceof Boolean) { configBuilder.with(p, nextValue.incrementAndGet() % 2 == 0); } else {
private String configProperty(String componentCode, Property<?> property) { return String.format("%s.%s", componentCode, property.getName()); }
/** * 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); } }
if (ipAddress == null || ipAddress.equals(LISTENING_IP_ADDRESS.getDefaultValue())) {
/** * Includes the given property and its value in the builder, without Property-Value type-enforcement. * * @param property The property to set. * @param value The value of the property. * @return This instance. */ public ConfigBuilder<T> withUnsafe(Property<?> property, Object value) { String key = String.format("%s.%s", this.namespace, property.getName()); this.properties.setProperty(key, value.toString()); return this; }
/** * Tests the with() method. */ @Test public void testWith() { final String namespace = "ns"; final int propertyCount = 10; val builder = new ConfigBuilder<TestConfig>(namespace, TestConfig::new); for (int i = 0; i < propertyCount; i++) { val result = builder.with(Property.named(Integer.toString(i)), i); Assert.assertEquals("with() did not return this instance.", builder, result); } TestConfig c = builder.build(); for (int i = 0; i < propertyCount; i++) { val p = Property.<Integer>named(Integer.toString(i)); val actual = c.getProperties().getInt(p); Assert.assertEquals("Unexpected value in result.", i, actual); } }
/** * Includes the given property and its value in the builder. * * @param property The property to set. * @param value The value of the property. This must be of the same type as accepted by the Property. * In case a `null` value is sent, the value of the property will be set to empty string. * @param <V> Type of the property. * @return This instance. */ public <V> ConfigBuilder<T> with(Property<V> property, V value) { String key = String.format("%s.%s", this.namespace, property.getName()); this.properties.setProperty(key, value == null ? "" : value.toString()); return this; }
/** * Creates a new instance of the Property class with the given default value. * * @param name The name of the property. * @param defaultValue The default value of the property. * @param <T> The type of the property values. * @return A new instance of the Property class with the given default value. */ public static <T> Property<T> named(String name, T defaultValue) { return new Property<>(name, defaultValue); }
val builder1 = new ConfigBuilder<TestConfig>(namespace, TestConfig::new); for (int i = 0; i < propertyCount; i++) { builder1.with(Property.named(Integer.toString(i)), i); builder2.with(Property.named(Integer.toString(i)), i * 10); TestConfig c2 = builder2.build(); for (int i = 0; i < propertyCount; i++) { val p = Property.<Integer>named(Integer.toString(i)); val actual1 = c1.getProperties().getInt(p); val actual2 = c2.getProperties().getInt(p);
private static void printUsage() { System.out.println("SelfTest arguments (set via System Properties):"); System.out.println("- Any property defined in TestConfig or other *Config classes in SegmentStore."); System.out.println(String.format("- %s: Load up configuration from this file.", TestConfig.CONFIG_FILE_PROPERTY_NAME)); System.out.println("- Shortcuts:"); Shortcuts.forEach(s -> System.out.println(String.format("\t-%s: %s", s.key, s.property.getName()))); System.out.println("At least one shortcut or a reference to a config file is required for the test."); }
int propertyId = getPropertyId(fullyQualifiedPropertyName); String propName = getPropertyName(propertyId); Property<T> property = Property.named(propName); Property<String> stringProperty = Property.named(propName); String expectedValue = props.getProperty(fullyQualifiedPropertyName); if (fullyQualifiedPropertyName.startsWith(componentCode)) {