CollectionUtils { public static Object getByPath(Object object, String path) { if (path == null) { return object instanceof Map ? ((Map<?, ?>) object).get(null) : null; } for (String key; path != null;) { if (object == null) { return null; } int slashAt = path.indexOf('/'); if (slashAt > -1) { key = path.substring(0, slashAt); path = path.substring(slashAt + 1); } else { key = path; path = null; } if (object instanceof Map) { object = ((Map<?, ?>) object).get(key); } else if (object instanceof List) { Integer index = ObjectUtils.to(Integer.class, key); if (index != null) { List<?> list = (List<?>) object; int listSize = list.size(); if (index < 0) { index += listSize; } if (index >= 0 && index < listSize) { object = list.get(index); continue; } } return null; } else { Method getter = TypeDefinition.getInstance(object.getClass()).getAllGetters().get(key); if (getter == null) { return null; } else { try { return getter.invoke(object); } catch (IllegalAccessException error) { throw new IllegalStateException(error); } catch (InvocationTargetException error) { throw Throwables.propagate(error.getCause()); } } } } return object; }  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 getByPath_path_null_list() { List<Integer> input = Arrays.asList(1,2,3); assertEquals(null, CollectionUtils.getByPath(input, null)); }
@Test public void getByPath_path_null_map_with_key() { Map<String,Integer> input = new HashMap<String,Integer>() {{ put("a", 1); put(null, 2); }}; assertEquals(2, CollectionUtils.getByPath(input, null)); }
@Test public void getByPath_path_null_map_without_key() { Map<String,Integer> input = new HashMap<String,Integer>() {{ put("a", 1); put("b", 2); }}; assertEquals(null, CollectionUtils.getByPath(input, null)); }
@Ignore @Test public void getByPath_path_empty() { assertEquals(Arrays.asList(1,2,3), CollectionUtils.getByPath(Arrays.asList(1,2,3), "")); }
@Test public void getByPath_path_list_first() { List<String> input = Arrays.asList("a", "b", "c"); assertEquals("a", CollectionUtils.getByPath(input, "0")); }
@Test public void getByPath_path_list_oorange() { List<String> input = Arrays.asList("a", "b", "c"); assertEquals(null, CollectionUtils.getByPath(input, "3")); }
@Test public void getByPath_path_list_negative() { List<String> input = Arrays.asList("a", "b", "c"); assertEquals("c", CollectionUtils.getByPath(input, "-1")); }
@Test public void getByPath_path_list_oorange_negative() { List<String> input = Arrays.asList("a", "b", "c"); assertEquals(null, CollectionUtils.getByPath(input, "-4")); }
@Test public void getByPath_path_map() { Map<String,String> input = ObjectUtils.to(Map.class, Arrays.asList("a", "x", "b", "y", "c", "z")); assertEquals("x", CollectionUtils.getByPath(input, "a")); }
@Test public void getByPath_path_map_unknown_key() { Map<String,String> input = ObjectUtils.to(Map.class, Arrays.asList("a", "x", "b", "y", "c", "z")); assertEquals(null, CollectionUtils.getByPath(input, "d")); }
@Test public void getByPath_path_map_tree() { Map<String,Object> input = ObjectUtils.to(Map.class, Arrays.asList("a", "x", "b", Arrays.asList(10,20,30), "c", "z")); assertEquals(20, CollectionUtils.getByPath(input, "b/1")); }
@Test public void getByPath_path_extra_paths() { Map<String,String> input = ObjectUtils.to(Map.class, Arrays.asList("a", "x", "b", "y", "c", "z")); assertEquals(null, CollectionUtils.getByPath(input, "a/1")); }