io.jooby.internal.whoops.Utils Maven / Gradle / Ivy
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.internal.whoops;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Utils {
public static Map multimap(Map> input) {
if (input.isEmpty()) {
return Collections.emptyMap();
}
Map map = new LinkedHashMap<>();
for (Map.Entry> e : input.entrySet()) {
if (e.getValue().size() == 1) {
map.put(e.getKey(), e.getValue().get(0));
} else {
map.put(e.getKey(), e.getValue());
}
}
return map;
}
public static Map mapOf(Object... values) {
Map map = new LinkedHashMap<>();
for (int i = 0; i < values.length; i += 2) {
Object value = values[i + 1];
if (!isEmpty(value)) {
map.put(values[i].toString(), value);
}
}
return map;
}
public static boolean isEmpty(Object value) {
if (value instanceof Map) {
return ((Map) value).isEmpty();
}
if (value instanceof Collection) {
return ((Collection) value).isEmpty();
}
return value == null;
}
}