CollectionUtils { @SuppressWarnings("unchecked") public static Object putByPath(Map<String, Object> map, String path, Object value) { if (path == null) { return map.put(null, value); } String[] names = StringUtils.split(path, "/"); int len = names.length - 1; for (int i = 0; i < len; i ++) { String name = names[i]; Object newMap = map.get(name); if (!(newMap instanceof Map)) { newMap = new HashMap<String, Object>(); map.put(name, newMap); } map = (Map<String, Object>) newMap; } return map.put(names[len], value); }  static Object getByPath(Object object, String path); @SuppressWarnings("unchecked") static Object putByPath(Map<String, Object> map, String path, Object value); @SuppressWarnings("unchecked") static void putAllRecursively(Map<String, Object> destination, Map<String, Object> source); static T set(List<T> list, int index, T value); @SuppressWarnings("unchecked") static T setByPath(List<Object> list, String path, T value); static Iterable<Object> recursiveIterable(final Object value);  }
@Test public void putByPath_path_null() { Map<String,Object> input = new HashMap<String,Object>(); Map<String,Object> expect = new HashMap<String,Object>() {{ put(null, "value"); }}; CollectionUtils.putByPath(input, null, "value"); assertEquals(expect, input); }
@Test public void putByPath_path_single_level() { Map<String,Object> input = new HashMap<String,Object>(); Map<String,Object> expect = new HashMap<String,Object>() {{ put("a", "value"); }}; CollectionUtils.putByPath(input, "a", "value"); assertEquals(expect, input); }
@Test public void putByPath_path_two_level() { Map<String,Object> input = new HashMap<String,Object>() {{ put("a", new HashMap<String,Object>()); }}; Map<String,Object> expect = new HashMap<String,Object>() {{ put("a", new HashMap<String,Object>() {{ put("b", "value"); }}); }}; CollectionUtils.putByPath(input, "a/b", "value"); assertEquals(expect, input); }
@Test public void putByPath_path_deeper_than_maps() { Map<String,Object> input = new HashMap<String,Object>(); Map<String,Object> expect = new HashMap<String,Object>() {{ put("a", new HashMap<String,Object>() {{ put("b", new HashMap<String,Object>() {{ put("c", "value"); }}); }}); }}; CollectionUtils.putByPath(input, "a/b/c", "value"); assertEquals(expect, input); }
@Test public void putByPath_path_replaces_value() { Map<String,Object> input = new HashMap<String,Object>() {{ put("a", "1"); }}; Map<String,Object> expect = new HashMap<String,Object>() {{ put("a", new HashMap<String,Object>() {{ put("b", new HashMap<String,Object>() {{ put("c", "value"); }}); }}); }}; CollectionUtils.putByPath(input, "a/b/c", "value"); assertEquals(expect, input); }
@Test public void putByPath_path_existing_not_overlapping() { Map<String,Object> input = new HashMap<String,Object>() {{ put("x", "1"); put("y", new HashMap<String,Object>() {{ put("z", "2"); }}); }}; Map<String,Object> expect = new HashMap<String,Object>() {{ put("x", "1"); put("y", new HashMap<String,Object>() {{ put("a", "value 2"); put("z", "2"); }}); put("a", new HashMap<String,Object>() {{ put("b", new HashMap<String,Object>() {{ put("c", "value 1"); }}); }}); }}; CollectionUtils.putByPath(input, "a/b/c", "value 1"); CollectionUtils.putByPath(input, "y/a", "value 2"); assertEquals(expect, input); }
@Test public void putByPath_return_value() { Map<String,Object> input = new HashMap<String,Object>() {{ put("a", new HashMap<String,Object>() {{ put("b", new HashMap<String,Object>() {{ put("c", "value 1"); }}); }}); }}; assertEquals("value 1", CollectionUtils.putByPath(input, "a/b/c", "value 2")); assertEquals(null, CollectionUtils.putByPath(input, "a/b/d", "value 3")); }