Search in sources :

Example 1 with MapEntry

use of groovy.util.MapEntry in project groovy by apache.

the class DefaultGroovyMethods method addEntry.

private static <K, V> void addEntry(Map<K, V> result, Object newEntry) {
    if (newEntry instanceof Map) {
        leftShift(result, (Map) newEntry);
    } else if (newEntry instanceof List) {
        List list = (List) newEntry;
        // def (key, value) == list
        Object key = list.isEmpty() ? null : list.get(0);
        Object value = list.size() <= 1 ? null : list.get(1);
        leftShift(result, new MapEntry(key, value));
    } else if (newEntry.getClass().isArray()) {
        Object[] array = (Object[]) newEntry;
        // def (key, value) == array.toList()
        Object key = array.length == 0 ? null : array[0];
        Object value = array.length <= 1 ? null : array[1];
        leftShift(result, new MapEntry(key, value));
    } else {
        // TODO: enforce stricter behavior?
        // given Map.Entry is an interface, we get a proxy which gives us lots
        // of flexibility but sometimes the error messages might be unexpected
        leftShift(result, asType(newEntry, Map.Entry.class));
    }
}
Also used : MapEntry(groovy.util.MapEntry) MapEntry(groovy.util.MapEntry)

Aggregations

MapEntry (groovy.util.MapEntry)1