/** * Collapses a collection of individual objects, arrays, and collections into a single list of objects. * * @param o The collection of objects to collapse. * @return A new linked-list of objects. */ public static List<Object> collapse(Object...o) { return collapse(new LinkedList<>(), o); }
/** * Collapses a collection of individual objects, arrays, and collections into a single list of objects. * * @param o The collection of objects to collapse. * @return A new linked-list of objects. */ public static List<Object> collapse(Object...o) { return collapse(new LinkedList<>(), o); }
/** * Collapses a collection of individual objects, arrays, and collections into a single list of objects. * * @param o The collection of objects to collapse. * @return A new linked-list of objects. */ public static List<Object> collapse(Object...o) { return collapse(new LinkedList<>(), o); }
/** * Same as {@link #collapse(Object...)} but allows you to supply your own list to append to. * * @param l The list to append to. * @param o The collection of objects to collapse. * @return The same list passed in. */ @SuppressWarnings("unchecked") public static List<Object> collapse(List<Object> l, Object...o) { for (Object o2 : o) { if (o2 != null) { if (o2.getClass().isArray()) { for (int i = 0; i < Array.getLength(o2); i++) collapse(l, Array.get(o2, i)); } else if (o2 instanceof Collection) { for (Object o3 : (Collection<Object>)o2) collapse(l, o3); } else { l.add(o2); } } } return l; }
/** * Same as {@link #collapse(Object...)} but allows you to supply your own list to append to. * * @param l The list to append to. * @param o The collection of objects to collapse. * @return The same list passed in. */ @SuppressWarnings("unchecked") public static List<Object> collapse(List<Object> l, Object...o) { for (Object o2 : o) { if (o2 != null) { if (o2.getClass().isArray()) { for (int i = 0; i < Array.getLength(o2); i++) collapse(l, Array.get(o2, i)); } else if (o2 instanceof Collection) { for (Object o3 : (Collection<Object>)o2) collapse(l, o3); } else { l.add(o2); } } } return l; }
/** * Same as {@link #collapse(Object...)} but allows you to supply your own list to append to. * * @param l The list to append to. * @param o The collection of objects to collapse. * @return The same list passed in. */ @SuppressWarnings("unchecked") public static List<Object> collapse(List<Object> l, Object...o) { for (Object o2 : o) { if (o2 != null) { if (o2.getClass().isArray()) { for (int i = 0; i < Array.getLength(o2); i++) collapse(l, Array.get(o2, i)); } else if (o2 instanceof Collection) { for (Object o3 : (Collection<Object>)o2) collapse(l, o3); } else { l.add(o2); } } } return l; }
@Test public void testCollapse() throws Exception { assertObjectEquals("[]", collapse()); assertObjectEquals("[1,2,3]", collapse(1,2,3)); assertObjectEquals("[1,2,3,4,5,6,7,8,9,10,11,12]", collapse(1, 2, null, new int[]{3,4}, new int[][]{null,{},{5,6}}, null, new ObjectList("[null,7,8,[9,10,[11,12,null]]]"))); }