All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jooby.internal.whoops.Utils Maven / Gradle / Ivy

There is a newer version: 3.5.3
Show newest version
/**
 * 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;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy