Package com.cedarsoftware.util
Class MapUtilities
java.lang.Object
com.cedarsoftware.util.MapUtilities
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 Summary
Modifier and TypeMethodDescriptioncloneMapOfMaps
(Map<T, Map<U, V>> original, boolean immutable) cloneMapOfSets
(Map<T, Set<V>> original, boolean immutable) Duplicate a map of Set to Class, possibly as unmodifiablestatic <T> T
Retrieves a value from a map by keygetOrThrow
(Map<?, ?> map, Object key, T throwable) Retrieves a value from a map by key, if value is not found by the given key throws a 'Throwable.'static boolean
Returns null safe isEmpty check for Mapstatic <K,
V> Map <K, V> Creates an immutable map with the specified key-value pairs, limited to 10 entries.static <K,
V> Map <K, V> mapOfEntries
(Map.Entry<K, V>... entries) Creates an immutable map from a series ofMap.Entry
objects.static <K,
V> String mapToString
(Map<K, V> map) Returns a string representation of the provided map.
-
Method Details
-
get
Retrieves a value from a map by key- Parameters:
map
- Map to retrieve item fromkey
- the key whose associated value is to be returneddef
- 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 TRetrieves 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 fromkey
- the key whose associated value is to be returnedthrowable
-- Returns:
- the value associated to the passed in key from the passed in map, otherwise throw the passed in exception.
- Throws:
T
-
isEmpty
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
Duplicate a map of Set to Class, possibly as unmodifiable- Parameters:
other
- map to duplicateunmodifiable
- will the result be unmodifiable- Returns:
- duplicated map
-
cloneMapOfSets
-
cloneMapOfMaps
-
mapToString
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 mapV
- 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
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 mapV
- 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 entriesNullPointerException
- if any key or value in the map isnull
-
mapOfEntries
Creates an immutable map from a series ofMap.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 mapV
- 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 isnull
-