Class KiwiMaps

java.lang.Object
org.kiwiproject.collect.KiwiMaps

public final class KiwiMaps extends Object
Utility methods for working with Map instances
  • Method Details

    • isNullOrEmpty

      public static <K, V> boolean isNullOrEmpty(Map<K,V> map)
      Checks whether the specified map is null or empty.
      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      Parameters:
      map - the map
      Returns:
      true if map is null or empty; false otherwise
    • isNotNullOrEmpty

      public static <K, V> boolean isNotNullOrEmpty(Map<K,V> map)
      Checks whether the specified map is neither null nor empty.
      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      Parameters:
      map - the map
      Returns:
      true if map is neither null nor empty; false otherwise
    • newUnmodifiableHashMap

      public static <K, V> Map<K,V> newUnmodifiableHashMap(Object... items)
      Creates an unmodifiable HashMap instance containing key/value pairs as parsed in pairs from the items argument. The items argument contains keys and values in the form:

      key-1, value-1, key-2, value-2, ... , key-N, value-N

      Note that trying to cast the returned value to a HashMap will result in a ClassCastException because the actual type returned by Collections.unmodifiableMap(Map) is a private class that implements Map. Since it is generally it is best to use the interface type Map this should not present a problem in most use cases.

      Unlike the factory methods in Map, null keys and values are permitted.

      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      Parameters:
      items - the items containing keys and values, in pairs
      Returns:
      a new unmodifiable HashMap with data from items
      See Also:
      Implementation Note:
      Wraps result of newHashMap(Object...) with Collections.unmodifiableMap(Map)
    • newHashMap

      public static <K, V> Map<K,V> newHashMap(Object... items)
      Creates a mutable, HashMap instance containing key/value pairs as parsed in pairs from the items argument. The items argument contains keys and values in the form:

      key-1, value-1, key-2, value-2, ... , key-N, value-N

      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      Parameters:
      items - the items containing keys and values, in pairs
      Returns:
      a new HashMap with data from items
    • newUnmodifiableLinkedHashMap

      public static <K, V> Map<K,V> newUnmodifiableLinkedHashMap(Object... items)
      Creates an unmodifiable, LinkedHashMap instance containing key/value pairs as parsed in pairs from the items argument. The items argument contains keys and values in the form:

      key-1, value-1, key-2, value-2, ... , key-N, value-N

      Note that trying to cast the returned value to a LinkedHashMap will result in a ClassCastException because the actual type returned by Collections.unmodifiableMap(Map) is a private class that implements Map. Since it is generally it is best to use the interface type Map this should not present a problem in most use cases.

      Unlike the factory methods in Map, null keys and values are permitted.

      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      Parameters:
      items - the items containing keys and values, in pairs
      Returns:
      a new unmodifiable LinkedHashMap with data from items
      See Also:
      Implementation Note:
      Wraps result of newLinkedHashMap(Object...) with Collections.unmodifiableMap(Map)
    • newLinkedHashMap

      public static <K, V> Map<K,V> newLinkedHashMap(Object... items)
      Creates a mutable, LinkedHashMap instance containing key/value pairs as parsed in pairs from the items argument. The items argument contains keys and values in the form:

      key-1, value-1, key-2, value-2, ... , key-N, value-N

      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      Parameters:
      items - the items containing keys and values, in pairs
      Returns:
      a new LinkedHashMap with data from items
    • newUnmodifiableTreeMap

      public static <K extends Comparable<? super K>, V> SortedMap<K,V> newUnmodifiableTreeMap(Object... items)
      Creates an unmodifiable, TreeMap instance containing key/value pairs as parsed in pairs from the items argument. The items argument contains keys and values in the form:

      key-1, value-1, key-2, value-2, ... , key-N, value-N

      Note that trying to cast the returned value to a TreeMap will result in a ClassCastException because the actual type returned by Collections.unmodifiableMap(Map) is a private class that implements Map. Since it is generally it is best to use the interface type Map this should not present a problem in most use cases.

      Like the factory methods in Map, null keys and values are not permitted.

      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      Parameters:
      items - the items containing keys and values, in pairs
      Returns:
      a new unmodifiable TreeMap with data from items
      See Also:
      Implementation Note:
      Wraps result of newTreeMap(Object...) with Collections.unmodifiableMap(Map)
    • newTreeMap

      public static <K extends Comparable<? super K>, V> SortedMap<K,V> newTreeMap(Object... items)
      Creates a mutable, TreeMap instance containing key/value pairs as parsed in pairs from the items argument. The items argument contains keys and values in the form:

      key-1, value-1, key-2, value-2, ... , key-N, value-N

      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      Parameters:
      items - the items containing keys and values, in pairs
      Returns:
      a new TreeMap with data from items
    • newUnmodifiableConcurrentHashMap

      public static <K, V> Map<K,V> newUnmodifiableConcurrentHashMap(Object... items)
      Creates an unmodifiable, ConcurrentHashMap instance containing key/value pairs as parsed in pairs from the items argument. The items argument contains keys and values in the form:

      key-1, value-1, key-2, value-2, ... , key-N, value-N

      Note that, unlike newConcurrentHashMap(Object...), this method has a return type of Map. This is because Collections.unmodifiableMap(Map) returns Map and trying to cast the returned instance to a ConcurrentMap results in a ClassCastException. Since it is generally it is best to use the interface type Map this should not present a problem in most use cases.

      Like the factory methods in Map, null keys and values are not permitted.

      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      Parameters:
      items - the items containing keys and values, in pairs
      Returns:
      a new unmodifiable ConcurrentHashMap with data from items
      See Also:
      Implementation Note:
      Wraps result of newConcurrentHashMap(Object...) with Collections.unmodifiableMap(Map)
    • newConcurrentHashMap

      public static <K, V> ConcurrentMap<K,V> newConcurrentHashMap(Object... items)
      Creates a mutable, ConcurrentHashMap instance containing key/value pairs as parsed in pairs from the items argument. The items argument contains keys and values in the form:

      key-1, value-1, key-2, value-2, ... , key-N, value-N

      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      Parameters:
      items - the items containing keys and values, in pairs
      Returns:
      a new ConcurrentHashMap with data from items
    • keyExistsWithNullValue

      public static <K, V> boolean keyExistsWithNullValue(Map<K,V> map, K key)
      Returns true if and only if (1) map is not null or empty, (2) map contains the given key, and (3) the value associated with the given key is null.
      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      Parameters:
      map - the map
      key - the key to check
      Returns:
      true if and only if (1) map is not null or empty, (2) map contains the given key, and (3) the value associated with the given key is null
    • keyExistsWithNonNullValue

      public static <K, V> boolean keyExistsWithNonNullValue(Map<K,V> map, K key)
      Checks whether the given map contains a key whose value is not null.
      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      Parameters:
      map - the map
      key - the key to check
      Returns:
      true if and only if (1) map is not null or empty, (2) map contains the given key, and (3) the value associated with the given key is not null
    • getOrThrow

      public static <K, V> V getOrThrow(Map<K,V> map, K key)
      Returns the value to which the specified key is mapped.

      If the map is null or empty, or it does not contain the specified key, or the value associated with the key is null, a NoSuchElementException is thrown.

      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      Parameters:
      map - the map
      key - the key whose associated value is to be returned
      Returns:
      the value to which the specified key is mapped
      Throws:
      IllegalArgumentException - if either argument is null
      NoSuchElementException - if the map is null or empty, does not contain the specified key, or the value associated with the key is null
    • getOrThrow

      public static <K, V, E extends RuntimeException> V getOrThrow(Map<K,V> map, K key, Supplier<E> exceptionSupplier)
      Returns the value to which the specified key is mapped.

      If the map is null or empty, or it does not contain the specified key, or the value associated with the key is null, the exception provided by the given Supplier is thrown.

      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      E - the type of RuntimeException
      Parameters:
      map - the map
      key - the key whose associated value is to be returned
      exceptionSupplier - supplies a RuntimeException when there is no value to return
      Returns:
      the value to which the specified key is mapped
      Throws:
      IllegalArgumentException - if any of the arguments is null
      E - if the map is null or empty, does not contain the specified key, or the value associated with the key is null
    • getOrThrowChecked

      public static <K, V, E extends Exception> V getOrThrowChecked(Map<K,V> map, K key, Supplier<E> exceptionSupplier) throws E
      Returns the value to which the specified key is mapped.

      If the map is null or empty, or it does not contain the specified key, or the value associated with the key is null, the exception provided by the given Supplier is thrown.

      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      E - the type of Exception
      Parameters:
      map - the map
      key - the key whose associated value is to be returned
      exceptionSupplier - supplies an Exception when there is no value to return
      Returns:
      the value to which the specified key is mapped
      Throws:
      IllegalArgumentException - if any of the arguments is null
      E - if the map is null or empty, does not contain the specified key, or the value associated with the key is null