ConfigFactory { public static Config fromMap(Map<String, ? extends Object> m) { return from(toConfigMap(m)); } private  ConfigFactory(); static Config fromMap(Map<String, ? extends Object> m); static Config fromProperties(Properties m); static Config from(ConfigMap m); static ReplaceableConfigMap replaceable(ConfigMap m); static ConfigMap chain(ConfigMap ... m); static ConfigMap chain(Iterable<ConfigMap> m); @SuppressWarnings("unchecked") static T bind(Config config, Class<? extends T> clazz); @SuppressWarnings("unchecked") static T bind(Config config, Class<? extends T> clazz, BindConfig bc); static Config fromBean(Class<?> c, Object o); static String prettyPrint(Config c); static ConfigMap toConfigMap(Map<?, ? extends Object> m); static ConfigMap toConfigMap(Class<?> clazz, Object target); static String validatePath(String path); static boolean isKey(String s); static boolean isSubPath(String parentPath, String childPath); static String joinPath(List<String> elements); static List<String> splitPath(String path); static String parentPath(String path); static Set<String> filterParentPaths(Iterator<String> paths);  }
@Test public void test() { Map<String, Object> o = newLinkedHashMap(); o.put("a.b.c", "123"); o.put("a.b", "12"); o.put("a", "Hello"); Config c = ConfigFactory.fromMap(o); Property<String> p = c.getString("a.b.c"); Property<String> pa = c.atPath("a").getString("b.c"); Property<String> pab = c.atPath("a.b").getString("c"); Property<String> none = c.atPath("none").getString("c"); assertEquals("123", p.get()); assertEquals("123", pa.get()); assertEquals("123", pab.get()); o.put("a.b.c", "321"); assertEquals("321", p.get()); assertEquals("321", pa.get()); assertEquals("321", pab.get()); assertEquals(321, c.getInteger("a.b.c").get().intValue()); List<String> keys = Lists.newArrayList(c.getKeys()); assertEquals(asList("a"), keys); assertEquals(asList("b"), Lists.newArrayList(c.atPath("a").getKeys())); assertFalse(none.isPresent()); }
@Test public void testChained() throws Exception { Map<String, Object> o = newLinkedHashMap(); o.put("host", "localhost"); o.put("port", 2); Config c = ConfigFactory.fromMap(o); final AtomicInteger i = new AtomicInteger(); Supplier<Optional<? extends Integer>> sup = new Supplier<Optional<? extends Integer>>() { @Override public Optional<? extends Integer> get() { if (i.get() == 0) return Optional.absent(); return Optional.of(i.get()); } }; c.getInteger("port").or(sup).or(5).addListener(new FutureCallback<Integer>() { @Override public void onSuccess(Integer result) { out.println("default port success: " + result); } @Override public void onFailure(Throwable t) { out.println("default port Error: " + t); } }); o.put("port", "werwe"); c.reload(); i.set(12); c.reload(); }