Search in sources :

Example 51 with GString

use of groovy.lang.GString 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)

Example 52 with GString

use of groovy.lang.GString in project groovy by apache.

the class StringGroovyMethods method denormalize.

/**
     * Return a CharSequence with lines (separated by LF, CR/LF, or CR)
     * terminated by the platform specific line separator.
     *
     * @param self a CharSequence object
     * @return the denormalized toString() of this CharSequence
     * @see #denormalize(String)
     * @since 1.8.2
     */
public static String denormalize(final CharSequence self) {
    // TODO: Put this lineSeparator property somewhere everyone can use it.
    if (lineSeparator == null) {
        final StringWriter sw = new StringWriter(2);
        try {
            // We use BufferedWriter rather than System.getProperty because
            // it has the security manager rigamarole to deal with the possible exception.
            final BufferedWriter bw = new BufferedWriter(sw);
            bw.newLine();
            bw.flush();
            lineSeparator = sw.toString();
        } catch (IOException ioe) {
            // This shouldn't happen, but this is the same default used by
            // BufferedWriter on a security exception.
            lineSeparator = "\n";
        }
    }
    final int len = self.length();
    if (len < 1) {
        return self.toString();
    }
    final StringBuilder sb = new StringBuilder((110 * len) / 100);
    int i = 0;
    // GROOVY-7873: GString calls toString() on each invocation of CharSequence methods such
    // as charAt which is very expensive for large GStrings.
    CharSequence cs = (self instanceof GString) ? self.toString() : self;
    while (i < len) {
        final char ch = cs.charAt(i++);
        switch(ch) {
            case '\r':
                sb.append(lineSeparator);
                // Eat the following LF if any.
                if ((i < len) && (cs.charAt(i) == '\n')) {
                    ++i;
                }
                break;
            case '\n':
                sb.append(lineSeparator);
                break;
            default:
                sb.append(ch);
                break;
        }
    }
    return sb.toString();
}
Also used : StringWriter(java.io.StringWriter) IOException(java.io.IOException) GString(groovy.lang.GString) BufferedWriter(java.io.BufferedWriter)

Aggregations

GString (groovy.lang.GString)52 CachedClass (org.codehaus.groovy.reflection.CachedClass)5 Closure (groovy.lang.Closure)3 IOException (java.io.IOException)3 Map (java.util.Map)3 GroovyObject (groovy.lang.GroovyObject)2 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 NullObject (org.codehaus.groovy.runtime.NullObject)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 StringWriter (java.io.StringWriter)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 SortedSet (java.util.SortedSet)1 CachedMethod (org.codehaus.groovy.reflection.CachedMethod)1 CachedSAMClass (org.codehaus.groovy.reflection.stdclasses.CachedSAMClass)1