Search in sources :

Example 1 with SpreadMap

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

the class InvokerHelper method spreadMap.

public static SpreadMap spreadMap(Object value) {
    if (value instanceof Map) {
        Object[] values = new Object[((Map) value).keySet().size() * 2];
        int index = 0;
        for (Object key : ((Map) value).keySet()) {
            values[index++] = key;
            values[index++] = ((Map) value).get(key);
        }
        return new SpreadMap(values);
    }
    throw new SpreadMapEvaluatingException("Cannot spread the map " + typeName(value) + ", value " + value);
}
Also used : SpreadMap(groovy.lang.SpreadMap) SpreadMapEvaluatingException(groovy.lang.SpreadMapEvaluatingException) GroovyObject(groovy.lang.GroovyObject) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SpreadMap(groovy.lang.SpreadMap)

Example 2 with SpreadMap

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

the class InvokerHelper method createMap.

public static Map createMap(Object[] values) {
    Map answer = new LinkedHashMap(values.length / 2);
    int i = 0;
    while (i < values.length - 1) {
        if ((values[i] instanceof SpreadMap) && (values[i + 1] instanceof Map)) {
            Map smap = (Map) values[i + 1];
            for (Object key : smap.keySet()) {
                answer.put(key, smap.get(key));
            }
            i += 2;
        } else {
            answer.put(values[i++], values[i++]);
        }
    }
    return answer;
}
Also used : SpreadMap(groovy.lang.SpreadMap) GroovyObject(groovy.lang.GroovyObject) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SpreadMap(groovy.lang.SpreadMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

GroovyObject (groovy.lang.GroovyObject)2 SpreadMap (groovy.lang.SpreadMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 SpreadMapEvaluatingException (groovy.lang.SpreadMapEvaluatingException)1