Class MapUtilities

java.lang.Object
com.cedarsoftware.util.MapUtilities

public class MapUtilities extends Object
Usefule utilities for Maps
Author:
Ken Partlow ([email protected]), John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

License

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  • Method Details

    • get

      public static <T> T get(Map<?,T> map, Object key, T def)
      Retrieves a value from a map by key
      Parameters:
      map - Map to retrieve item from
      key - the key whose associated value is to be returned
      def - value to return if item was not found.
      Returns:
      Returns a string value that was found at the location key. If the item is null then the def value is sent back. If the item is not the expected type, an exception is thrown.
    • getOrThrow

      public static <T extends Throwable> Object getOrThrow(Map<?,?> map, Object key, T throwable) throws T
      Retrieves a value from a map by key, if value is not found by the given key throws a 'Throwable.' This version allows the value associated to the key to be null, and it still works. In other words, if the passed in key is within the map, this method will return whatever is associated to the key, including null.
      Type Parameters:
      T - Throwable passed in to be thrown *if* the passed in key is not within the passed in map.
      Parameters:
      map - Map to retrieve item from
      key - the key whose associated value is to be returned
      throwable -
      Returns:
      the value associated to the passed in key from the passed in map, otherwise throw the passed in exception.
      Throws:
      T
    • isEmpty

      public static boolean isEmpty(Map map)
      Returns null safe isEmpty check for Map
      Parameters:
      map - Map to check, can be null
      Returns:
      Returns true if map is empty or null
    • dupe

      public static <T> Map<Class<?>,Set<T>> dupe(Map<Class<?>,Set<T>> other, boolean unmodifiable)
      Duplicate a map of Set to Class, possibly as unmodifiable
      Parameters:
      other - map to duplicate
      unmodifiable - will the result be unmodifiable
      Returns:
      duplicated map
    • cloneMapOfSets

      public static <T, V> Map<T,Set<V>> cloneMapOfSets(Map<T,Set<V>> original, boolean immutable)
    • cloneMapOfMaps

      public static <T, U, V> Map<T,Map<U,V>> cloneMapOfMaps(Map<T,Map<U,V>> original, boolean immutable)
    • mapToString

      public static <K, V> String mapToString(Map<K,V> map)
      Returns a string representation of the provided map.

      The string representation consists of a list of key-value mappings in the order returned by the map's entrySet iterator, enclosed in braces ("{}"). Adjacent mappings are separated by the characters ", " (comma and space). Each key-value mapping is rendered as the key followed by an equals sign ("=") followed by the associated value.

      Type Parameters:
      K - the type of keys in the map
      V - the type of values in the map
      Parameters:
      map - the map to represent as a string
      Returns:
      a string representation of the provided map
    • mapOf

      @SafeVarargs public static <K, V> Map<K,V> mapOf(Object... keyValues)
      Creates an immutable map with the specified key-value pairs, limited to 10 entries.

      If more than 10 key-value pairs are provided, an IllegalArgumentException is thrown.

      Type Parameters:
      K - the type of keys in the map
      V - the type of values in the map
      Parameters:
      keyValues - an even number of key-value pairs
      Returns:
      an immutable map containing the specified key-value pairs
      Throws:
      IllegalArgumentException - if the number of arguments is odd or exceeds 10 entries
      NullPointerException - if any key or value in the map is null
    • mapOfEntries

      @SafeVarargs public static <K, V> Map<K,V> mapOfEntries(Map.Entry<K,V>... entries)
      Creates an immutable map from a series of Map.Entry objects.

      This method is intended for use with larger maps where more than 10 entries are needed.

      Type Parameters:
      K - the type of keys in the map
      V - the type of values in the map
      Parameters:
      entries - the entries to be included in the map
      Returns:
      an immutable map containing the specified entries
      Throws:
      NullPointerException - if any entry, key, or value is null