Class MapUtilities


  • public class MapUtilities
    extends Object
    Usefule utilities for Maps
    Author:
    Kenneth Partlow
    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

    http://www.apache.org/licenses/LICENSE-2.0

    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 Detail

      • 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 ThrowableObject getOrThrow​(Map<?,​?> map,
                                                              Object key,
                                                              T throwable)
                                                       throws T extends Throwable
        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 extends Throwable
      • 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