Search in sources :

Example 1 with IteratorClosureAdapter

use of org.codehaus.groovy.runtime.IteratorClosureAdapter in project groovy-core by groovy.

the class ObjectRange method step.

/**
     * {@inheritDoc}
     */
public List step(int step) {
    IteratorClosureAdapter adapter = new IteratorClosureAdapter(this);
    step(step, adapter);
    return adapter.asList();
}
Also used : IteratorClosureAdapter(org.codehaus.groovy.runtime.IteratorClosureAdapter)

Example 2 with IteratorClosureAdapter

use of org.codehaus.groovy.runtime.IteratorClosureAdapter in project groovy by apache.

the class DefaultTypeTransformation method asCollection.

public static Collection asCollection(Object value) {
    if (value == null) {
        return Collections.EMPTY_LIST;
    } else if (value instanceof Collection) {
        return (Collection) value;
    } else if (value instanceof Map) {
        Map map = (Map) value;
        return map.entrySet();
    } else if (value.getClass().isArray()) {
        return arrayAsCollection(value);
    } else if (value instanceof MethodClosure) {
        MethodClosure method = (MethodClosure) value;
        IteratorClosureAdapter adapter = new IteratorClosureAdapter(method.getDelegate());
        method.call(adapter);
        return adapter.asList();
    } else if (value instanceof String || value instanceof GString) {
        return StringGroovyMethods.toList((CharSequence) value);
    } else if (value instanceof File) {
        try {
            return ResourceGroovyMethods.readLines((File) value);
        } catch (IOException e) {
            throw new GroovyRuntimeException("Error reading file: " + value, e);
        }
    } else if (value instanceof Class && ((Class) value).isEnum()) {
        Object[] values = (Object[]) InvokerHelper.invokeMethod(value, "values", EMPTY_OBJECT_ARRAY);
        return Arrays.asList(values);
    } else {
        // let's assume it's a collection of 1
        return Collections.singletonList(value);
    }
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) Collection(java.util.Collection) CachedSAMClass(org.codehaus.groovy.reflection.stdclasses.CachedSAMClass) NullObject(org.codehaus.groovy.runtime.NullObject) IteratorClosureAdapter(org.codehaus.groovy.runtime.IteratorClosureAdapter) GString(groovy.lang.GString) IOException(java.io.IOException) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) GString(groovy.lang.GString) Map(java.util.Map) File(java.io.File)

Aggregations

IteratorClosureAdapter (org.codehaus.groovy.runtime.IteratorClosureAdapter)2 GString (groovy.lang.GString)1 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)1 File (java.io.File)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 Map (java.util.Map)1 CachedSAMClass (org.codehaus.groovy.reflection.stdclasses.CachedSAMClass)1 MethodClosure (org.codehaus.groovy.runtime.MethodClosure)1 NullObject (org.codehaus.groovy.runtime.NullObject)1