/** * Flatten a Set. This Set and any nested arrays or * collections have their contents (recursively) added to the new Set. * <pre class="groovyTestCase">assert [1,2,3,4,5] as Set == ([1,[2,3],[[4]],[],5] as Set).flatten()</pre> * * @param self a Set to flatten * @return a flattened Set * @since 2.4.0 */ public static Set<?> flatten(Set<?> self) { return (Set<?>) flatten((Collection<?>) self); }
/** * Flatten a List. This List and any nested arrays or * collections have their contents (recursively) added to the new List. * <pre class="groovyTestCase">assert [1,2,3,4,5] == [1,[2,3],[[4]],[],5].flatten()</pre> * * @param self a List to flatten * @return a flattened List * @since 2.4.0 */ public static List<?> flatten(List<?> self) { return (List<?>) flatten((Collection<?>) self); }
/** * Flatten a SortedSet. This SortedSet and any nested arrays or * collections have their contents (recursively) added to the new SortedSet. * <pre class="groovyTestCase"> * Set nested = [[0,1],[2],3,[4],5] * SortedSet sorted = new TreeSet({ a, b -> (a instanceof List ? a[0] : a) <=> (b instanceof List ? b[0] : b) } as Comparator) * sorted.addAll(nested) * assert [0,1,2,3,4,5] as SortedSet == sorted.flatten() * </pre> * * @param self a SortedSet to flatten * @return a flattened SortedSet * @since 2.4.0 */ public static SortedSet<?> flatten(SortedSet<?> self) { return (SortedSet<?>) flatten((Collection<?>) self); }
private static Collection flatten(Iterable elements, Collection addTo) { for (Object element : elements) { if (element instanceof Collection) { flatten((Collection) element, addTo); } else if (element != null && element.getClass().isArray()) { flatten(DefaultTypeTransformation.arrayAsCollection(element), addTo); } else { // found a leaf addTo.add(element); } } return addTo; }
/** * Flatten an array. This array and any nested arrays or * collections have their contents (recursively) added to the new collection. * * @param self a char Array to flatten * @return a flattened Collection * @since 1.6.0 */ public static Collection flatten(char[] self) { return flatten(toList(self), new ArrayList()); }
/** * Flatten an array. This array and any nested arrays or * collections have their contents (recursively) added to the new collection. * * @param self a short Array to flatten * @return a flattened Collection * @since 1.6.0 */ public static Collection flatten(short[] self) { return flatten(toList(self), new ArrayList()); }
/** * Flatten an array. This array and any nested arrays or * collections have their contents (recursively) added to the new collection. * * @param self an Array to flatten * @return a flattened Collection * @since 1.6.0 */ public static Collection flatten(Object[] self) { return flatten(toList(self), new ArrayList()); }
/** * Flatten an array. This array and any nested arrays or * collections have their contents (recursively) added to the new collection. * * @param self a boolean Array to flatten * @return a flattened Collection * @since 1.6.0 */ public static Collection flatten(boolean[] self) { return flatten(toList(self), new ArrayList()); }
/** * Flatten an array. This array and any nested arrays or * collections have their contents (recursively) added to the new collection. * * @param self an int Array to flatten * @return a flattened Collection * @since 1.6.0 */ public static Collection flatten(int[] self) { return flatten(toList(self), new ArrayList()); }
/** * Flatten an array. This array and any nested arrays or * collections have their contents (recursively) added to the new collection. * * @param self a double Array to flatten * @return a flattened Collection * @since 1.6.0 */ public static Collection flatten(double[] self) { return flatten(toList(self), new ArrayList()); }
/** * Flatten an array. This array and any nested arrays or * collections have their contents (recursively) added to the new collection. * * @param self a byte Array to flatten * @return a flattened Collection * @since 1.6.0 */ public static Collection flatten(byte[] self) { return flatten(toList(self), new ArrayList()); }
/** * @deprecated Use the Iterable version of flatten instead * @see #flatten(Iterable, Closure) * @since 1.6.0 */ @Deprecated public static <T> Collection<T> flatten(Collection<T> self, Closure<? extends T> flattenUsing) { return flatten(self, createSimilarCollection(self), flattenUsing); }
/** * Flatten a Collection. This Collection and any nested arrays or * collections have their contents (recursively) added to the new collection. * <pre class="groovyTestCase">assert [1,2,3,4,5] == [1,[2,3],[[4]],[],5].flatten()</pre> * * @param self a Collection to flatten * @return a flattened Collection * @since 1.6.0 */ public static Collection<?> flatten(Collection<?> self) { return flatten(self, createSimilarCollection(self)); }
/** * Flatten an Iterable. This Iterable and any nested arrays or * collections have their contents (recursively) added to the new collection. * <pre class="groovyTestCase">assert [1,2,3,4,5] == [1,[2,3],[[4]],[],5].flatten()</pre> * * @param self an Iterable to flatten * @return a flattened Collection * @since 1.6.0 */ public static Collection<?> flatten(Iterable<?> self) { return flatten(self, createSimilarCollection(self)); }
/** * Flatten an array. This array and any nested arrays or * collections have their contents (recursively) added to the new collection. * * @param self a long Array to flatten * @return a flattened Collection * @since 1.6.0 */ public static Collection flatten(long[] self) { return flatten(toList(self), new ArrayList()); }
/** * Flatten an array. This array and any nested arrays or * collections have their contents (recursively) added to the new collection. * * @param self a float Array to flatten * @return a flattened Collection * @since 1.6.0 */ public static Collection flatten(float[] self) { return flatten(toList(self), new ArrayList()); }
/** * Flatten an Iterable. This Iterable and any nested arrays or * collections have their contents (recursively) added to the new collection. * For any non-Array, non-Collection object which represents some sort * of collective type, the supplied closure should yield the contained items; * otherwise, the closure should just return any element which corresponds to a leaf. * * @param self an Iterable * @param flattenUsing a closure to determine how to flatten non-Array, non-Collection elements * @return a flattened Collection * @since 1.6.0 */ public static <T> Collection<T> flatten(Iterable<T> self, Closure<? extends T> flattenUsing) { return flatten(self, createSimilarCollection(self), flattenUsing); }
private static <T> Collection<T> flatten(Iterable elements, Collection<T> addTo, Closure<? extends T> flattenUsing) { for (Object element : elements) { if (element instanceof Collection) { flatten((Collection) element, addTo, flattenUsing); } else if (element != null && element.getClass().isArray()) { flatten(DefaultTypeTransformation.arrayAsCollection(element), addTo, flattenUsing); } else { T flattened = flattenUsing.call(new Object[]{element}); boolean returnedSelf = flattened == element; if (!returnedSelf && flattened instanceof Collection) { List<?> list = toList((Iterable<?>) flattened); if (list.size() == 1 && list.get(0) == element) { returnedSelf = true; } } if (flattened instanceof Collection && !returnedSelf) { flatten((Collection) flattened, addTo, flattenUsing); } else { addTo.add(flattened); } } } return addTo; }
/** * Flatten an array. This array and any nested arrays or * collections have their contents (recursively) added to the new collection. * * @param self an int Array to flatten * @return a flattened Collection * @since 1.6.0 */ public static Collection flatten(int[] self) { return flatten(toList(self), new ArrayList()); }
/** * Flatten an array. This array and any nested arrays or * collections have their contents (recursively) added to the new collection. * * @param self a double Array to flatten * @return a flattened Collection * @since 1.6.0 */ public static Collection flatten(double[] self) { return flatten(toList(self), new ArrayList()); }