Java Code Examples for org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation#asCollection()

The following examples show how to use org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation#asCollection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroovyCollections.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * 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>.
 * If an empty collection is found within the given collections, the result will be an empty list.
 *
 * @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 (Object collection : collections) {
        Iterable items = DefaultTypeTransformation.asCollection(collection);
        if (collectedCombos.isEmpty()) {
            for (Object item : items) {
                List l = new ArrayList();
                l.add(item);
                collectedCombos.add(l);
            }
        } else {
            List savedCombos = new ArrayList(collectedCombos);
            List newCombos = new ArrayList();
            for (Object value : items) {
                for (Object savedCombo : savedCombos) {
                    List oldList = new ArrayList((List) savedCombo);
                    oldList.add(value);
                    newCombos.add(oldList);
                }
            }
            collectedCombos = newCombos;
        }

        if (collectedCombos.isEmpty()) 
            break;
    }
    return collectedCombos;
}
 
Example 2
Source File: InvokerTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that the given object can be converted into a collection and iterator
 * of the given size
 */
protected void assertAsCollection(Object collectionObject, int count) {
    Collection collection = DefaultTypeTransformation.asCollection(collectionObject);
    assertTrue("Collection is not null", collection != null);
    assertEquals("Collection size", count, collection.size());

    assertIterator("collections iterator", collection.iterator(), count);
    assertIterator("InvokerHelper.asIterator", InvokerHelper.asIterator(collectionObject), count);
    assertIterator("InvokerHelper.asIterator(InvokerHelper.asCollection)", InvokerHelper.asIterator(collection), count);
    assertIterator("InvokerHelper.asIterator(InvokerHelper.asIterator)", InvokerHelper.asIterator(InvokerHelper.asIterator(collectionObject)), count);
}
 
Example 3
Source File: InvokeMethodTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public Integer mockCallWithOneCollectionParam(Object collection) {
    Collection coll = DefaultTypeTransformation.asCollection(collection);
    return Integer.valueOf(coll.size());
}