Skip to content

Commit

Permalink
GROOVY-6053: Try iterating or transforming Object into Collection in …
Browse files Browse the repository at this point in the history
…DefaultGroovyMethods like count(), sort() and other Collection methods.
  • Loading branch information
paulk-asert committed Mar 19, 2013
1 parent 8b3ea01 commit 5b95a1b
Show file tree
Hide file tree
Showing 2 changed files with 760 additions and 95 deletions.
76 changes: 64 additions & 12 deletions src/main/groovy/util/GroovyCollections.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2007 the original author or authors.
* Copyright 2003-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,7 +36,7 @@ public class GroovyCollections {
* @see #combinations(Collection)
*/
public static List combinations(Object[] collections) {
return combinations(Arrays.asList(collections));
return combinations((Iterable)Arrays.asList(collections));
}

/**
Expand Down Expand Up @@ -78,25 +78,42 @@ public static <T> Set<List<T>> subsequences(List<T> items) {
* @param collections the given collections
* @return a List of the combinations found
*/
@Deprecated
public static List combinations(Collection collections) {
return combinations((Iterable)collections);
}

/**
* Finds all combinations of items from the given Iterable aggregate of collections.
* So, <code>combinations([[true, false], [true, false]])</code>
* is <code>[[true, true], [false, true], [true, false], [false, false]]</code>
* and <code>combinations([['a', 'b'],[1, 2, 3]])</code>
* is <code>[['a', 1], ['b', 1], ['a', 2], ['b', 2], ['a', 3], ['b', 3]]</code>.
* If a non-collection item is given, it is treated as a singleton collection,
* i.e. <code>combinations([[1, 2], 'x'])</code> is <code>[[1, 'x'], [2, 'x']]</code>.
*
* @param collections the Iterable of given collections
* @return a List of the combinations found
* @since 2.2.0
*/
public static List combinations(Iterable collections) {
List collectedCombos = new ArrayList();
for (Iterator outer = collections.iterator(); outer.hasNext();) {
Collection items = DefaultTypeTransformation.asCollection(outer.next());
for (Object collection : collections) {
Iterable items = DefaultTypeTransformation.asCollection(collection);
if (collectedCombos.isEmpty()) {
for (Iterator iterator = items.iterator(); iterator.hasNext();) {
for (Object item : items) {
List l = new ArrayList();
l.add(iterator.next());
l.add(item);
collectedCombos.add(l);
}
} else {
List savedCombos = new ArrayList(collectedCombos);
List newCombos = new ArrayList();
for (Iterator inner = items.iterator(); inner.hasNext();) {
Object value = inner.next();
for (Iterator combos = savedCombos.iterator(); combos.hasNext();) {
List oldlist = new ArrayList((List) combos.next());
oldlist.add(value);
newCombos.add(oldlist);
for (Object value : items) {
for (Object savedCombo : savedCombos) {
List oldList = new ArrayList((List) savedCombo);
oldList.add(value);
newCombos.add(oldList);
}
}
collectedCombos = newCombos;
Expand Down Expand Up @@ -165,7 +182,19 @@ public static <T> T min(T[] items) {
* @param items a Collection
* @return the minimum value
*/
@Deprecated
public static <T> T min(Collection<T> items) {
return min((Iterable<T>)items);
}

/**
* Selects the minimum value found in an Iterable of items.
*
* @param items an Iterable
* @return the minimum value
* @since 2.2.0
*/
public static <T> T min(Iterable<T> items) {
T answer = null;
for (T value : items) {
if (value != null) {
Expand Down Expand Up @@ -194,7 +223,19 @@ public static <T> T max(T[] items) {
* @param items a Collection
* @return the maximum value
*/
@Deprecated
public static <T> T max(Collection<T> items) {
return max((Iterable<T>)items);
}

/**
* Selects the maximum value found in an Iterable.
*
* @param items a Collection
* @return the maximum value
* @since 2.2.0
*/
public static <T> T max(Iterable<T> items) {
T answer = null;
for (T value : items) {
if (value != null) {
Expand Down Expand Up @@ -226,4 +267,15 @@ public static Object sum(Collection items) {
return DefaultGroovyMethods.sum(items);
}

/**
* Sums all the given items.
*
* @param items an Iterable of items
* @return the sum of the item
* @since 2.2.0
*/
public static Object sum(Iterable items) {
return DefaultGroovyMethods.sum(items);
}

}

0 comments on commit 5b95a1b

Please sign in to comment.