@Test public void testPropertyChange() throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); List<Value<Integer>> values = Lists.newArrayList(); Value<Integer> value = new Value<Integer>(); Map<String, Collection<String>> dimension = Maps.newHashMap(); dimension.put("d1", Lists.newArrayList("v1", "v2")); value.setDimensions(dimension); value.setValue(5); values.add(value); Value<Integer> value2 = new Value<Integer>(); Map<String, Collection<String>> dimension2 = Maps.newHashMap(); dimension2.put("d1", Lists.newArrayList("v3")); dimension2.put("d2", Lists.newArrayList("x1")); value2.setDimensions(dimension2); value2.setValue(10); values.add(value2); value = new Value<Integer>(); value.setValue(2); values.add(value); String json = mapper.writeValueAsString(values); System.out.println("serialized json: " + json); ConfigurationManager.getConfigInstance().setProperty("d1", "v1"); ConfigurationManager.getConfigInstance().setProperty("contextualProp", json); DynamicContextualProperty<Integer> prop = new DynamicContextualProperty<Integer>("contextualProp", 0); assertEquals(5, prop.getValue().intValue()); ConfigurationManager.getConfigInstance().setProperty("d1", "v2"); assertEquals(5, prop.getValue().intValue()); ConfigurationManager.getConfigInstance().setProperty("d1", "v3"); assertEquals(2, prop.getValue().intValue()); ConfigurationManager.getConfigInstance().setProperty("d2", "x1"); assertEquals(10, prop.getValue().intValue()); ConfigurationManager.getConfigInstance().setProperty("d1", "v1"); assertEquals(5, prop.getValue().intValue()); values.remove(0); json = mapper.writeValueAsString(values); ConfigurationManager.getConfigInstance().setProperty("contextualProp", json); assertEquals(2, prop.getValue().intValue()); ConfigurationManager.getConfigInstance().clearProperty("contextualProp"); assertEquals(0, prop.getValue().intValue()); }
@Override public T getValue() { if (values != null) { for (Value<T> v: values) { if (v.getDimensions() == null || v.getDimensions().isEmpty() || predicate.apply(v.getDimensions())) { return v.getValue(); } } } return defaultValue; }
DynamicContextualProperty extends PropertyWrapper<T> { @Override public T getValue() { if (values != null) { for (Value<T> v: values) { if (v.getDimensions() == null || v.getDimensions().isEmpty() || predicate.apply(v.getDimensions())) { return v.getValue(); } } } return defaultValue; } }
DynamicContextualProperty extends PropertyWrapper<T> { @Override public T getValue() { if (values != null) { for (Value<T> v: values) { if (v.getDimensions() == null || v.getDimensions().isEmpty() || predicate.apply(v.getDimensions())) { return v.getValue(); } } } return defaultValue; } @SuppressWarnings("unchecked")  DynamicContextualProperty(String propName, T defaultValue, Predicate<Map<String, Collection<String>>> predicate); @SuppressWarnings("unchecked")  DynamicContextualProperty(String propName, T defaultValue); }
DynamicContextualProperty extends PropertyWrapper<T> { @Override public T getValue() { if (values != null) { for (Value<T> v: values) { if (v.getDimensions() == null || v.getDimensions().isEmpty() || predicate.apply(v.getDimensions())) { return v.getValue(); } } } return defaultValue; } @SuppressWarnings("unchecked")  DynamicContextualProperty(String propName, T defaultValue, Predicate<Map<String, Collection<String>>> predicate); @SuppressWarnings("unchecked")  DynamicContextualProperty(String propName, T defaultValue); @Override T getValue(); }
DynamicContextualProperty extends PropertyWrapper<T> { @Override public T getValue() { if (values != null) { for (Value<T> v: values) { if (v.getDimensions() == null || v.getDimensions().isEmpty() || predicate.apply(v.getDimensions())) { return v.getValue(); } } } return defaultValue; } @SuppressWarnings("unchecked")  DynamicContextualProperty(String propName, T defaultValue, Predicate<Map<String, Collection<String>>> predicate); @SuppressWarnings("unchecked")  DynamicContextualProperty(String propName, T defaultValue); @Override T getValue();  }