org.omnaest.utils.structure.map
Class MapUtils

java.lang.Object
  extended by org.omnaest.utils.structure.map.MapUtils

public class MapUtils
extends Object

Helper class for Map operations.

Author:
Omnaest

Nested Class Summary
static interface MapUtils.MapElementMergeOperation<K,V>
          A MapUtils.MapElementMergeOperation defines a MapUtils.MapElementMergeOperation.merge(Object, Object, Map) operation to merge Map elements into a merged Map instance.
static interface MapUtils.MapEntryToElementConverter<TO,K,V>
          Converted to transform a given Map.Entry of a Map to a single List element
 
Constructor Summary
MapUtils()
           
 
Method Summary
static
<KEY_FROM,VALUE_FROM,KEY_TO,VALUE_TO>
Map<KEY_TO,VALUE_TO>
adapter(Map<KEY_FROM,VALUE_FROM> sourceMap, ElementBidirectionalConverter<KEY_FROM,KEY_TO> elementBidirectionalConverterKey, ElementBidirectionalConverter<VALUE_FROM,VALUE_TO> elementBidirectionalConverterValue)
          Returns a MapToMapAdapter for the given source Map
static
<KEY,VALUE_FROM,VALUE_TO>
SortedMap<KEY,VALUE_TO>
adapter(SortedMap<KEY,VALUE_FROM> sourceMap, ElementBidirectionalConverter<VALUE_FROM,VALUE_TO> elementBidirectionalConverterValue)
          Returns a SortedMapToSortedMapAdapter for the given source SortedMap
static MapBuilder builder()
          Returns a new MapBuilder instance.
static
<K,V> Map<K,V>
composite(List<Map<K,V>> mapList)
          Returns a new MapComposite instance for the given Maps
static
<K,V> Map<K,V>
composite(Map<K,V>... maps)
          Returns a new MapComposite instance for the given Maps
static
<KeyFrom,KeyTo,ValueFrom,ValueTo>
Map<KeyTo,ValueTo>
convertMap(Map<? extends KeyFrom,? extends ValueFrom> map, ElementConverter<KeyFrom,KeyTo> keyElementConverter, ElementConverter<ValueFrom,ValueTo> valueElementConverter)
          Returns a new Map instance with converted keys and values using the given ElementConverters
static
<KeyFrom,KeyTo,ValueFrom,ValueTo>
Map<KeyTo,ValueTo>
convertMap(Map<? extends KeyFrom,? extends ValueFrom> map, ElementConverter<Map.Entry<KeyFrom,ValueFrom>,Map.Entry<KeyTo,ValueTo>> entryElementConverter)
          Similar to convertMap(Map, ElementConverter, ElementConverter) using a single ElementConverter for an Map.Entry
static
<KeyFrom,KeyTo,Value>
Map<KeyTo,Value>
convertMapKey(Map<? extends KeyFrom,? extends Value> map, ElementConverter<KeyFrom,KeyTo> keyElementConverter)
          Returns a new Map instance with converted keys using the given ElementConverter
static
<Key,ValueFrom,ValueTo>
Map<Key,ValueTo>
convertMapValue(Map<? extends Key,? extends ValueFrom> map, ElementConverter<ValueFrom,ValueTo> valueElementConverter)
          Returns a new Map instance with all values converted using the given ElementConverter
static
<K,V> Map<K,V>
defaultValueMap(Map<K,V> map, FactoryParameterized<V,K> valueFactory)
          Returns a Map which always returns a value prodcued by the given factory if the value would have be null
static
<K,V> Map<K,V>
defaultValueMap(Map<K,V> map, V defaultValue)
          Similar to defaultValueMap(Map, FactoryParameterized)
static
<K,V> MapDelta<K,V>
delta(Map<K,V> mapFirst, Map<K,V> mapSecond)
          Returns a new MapDelta for the given two Map instances
static
<K,V> Map.Entry<K,V>
entryAt(Map<K,V> map, int indexPosition)
          Returns the Map.Entry at the given index position of the given Map.
static
<K,V> Map<K,V>
filteredMap(Map<K,V> map, ElementFilter<K> keyElementFilter)
          Returns a new filtered Map instance.
static
<K,V> Map<K,V>
filteredMap(Map<K,V> map, Iterable<K> filterKeyIterable)
          Filters a given Map by its keys.
static
<K,V> Map<K,V>
filteredMapExcludingNullValues(Map<K,V> map)
          Filters a given Map by values which are null.
static
<K,V> V[]
filteredValues(Map<K,V> map, Class<V> valueType, K... keys)
           
static
<K,V> Map.Entry<K,V>
firstEntry(Map<K,V> map)
          Returns the first Map.Entry of a given Map or null if the Map reference is null or the Map is empty
static
<E> E
getValueByRegex(Map<String,? extends E> map, String regex)
          Returns the value of the first key within a Map which matches a given regular expression.
static
<E> Map<E,AtomicInteger>
initializedCounterMap()
          Returns a Map instance which returns for Map.get(Object) always an AtomicInteger instance.
static
<K extends Enum<K>,V>
EnumMap<K,V>
initializedEnumMap(Class<K> enumType, Factory<V> factory)
          Returns an EnumMap filled with all available values of the given Enum type as keys and the result of the Factory as value for each Enum key.
static
<K,V> Map<K,V>
initializedMap(Factory<V> valueFactory)
          Similar to initializedMap(Map, Factory) using a new LinkedHashMap instance
static
<K,V> Map<K,V>
initializedMap(FactoryParameterized<V,K> valueFactory)
          Similar to initializedMap(Map, FactoryParameterized) using a new LinkedHashMap instance
static
<K,V> Map<K,V>
initializedMap(Map<K,V> map, Factory<V> valueFactory)
          Returns a MapDecorator which ensures that all Map.get(Object) invocations with a valid key type will return a value.
static
<K,V> Map<K,V>
initializedMap(Map<K,V> map, FactoryParameterized<V,K> valueFactory)
          Returns a MapDecorator which ensures that all Map.get(Object) invocations with a valid key type will return a value.
static
<K,V> SortedMap<K,V>
initializedSortedMap(SortedMap<K,V> sortedMap, Factory<V> valueFactory)
          Similar to initializedMap(Map, Factory) but for any SortedMap instance
static
<K,V> Map<K,List<V>>
initializedValueListMap()
          Short form of initializedMap(Factory) using an ArrayListFactory
static
<K,V> Map<K,List<V>>
initializedValueListMap(Map<K,List<V>> map)
          Short form of initializedMap(Factory) using an ArrayListFactory
static
<K,V> Map<K,Set<V>>
initializedValueSetMap()
          Short form of initializedMap(Factory) using an LinkedHashSetFactory
static
<K,V> Map<K,Set<V>>
initializedValueSetMap(Map<K,Set<V>> map)
          Short form of initializedMap(Factory) using an LinkedHashSetFactory
static
<K,V> void
initializeMap(Map<K,V> map, Iterable<K> keyIterable, Factory<V> valueFactory)
          Similar to initializeMap(Map, Iterable, Factory, boolean) but does not overwrite values of already existing keys.
static
<K,V> void
initializeMap(Map<K,V> map, Iterable<K> keyIterable, Factory<V> valueFactory, boolean overwriteValuesOfExistingKeys)
          Initializes the given Map for all keys from the key Iterable with values created by the value Factory.newInstance() method.
static
<K,VA,VB> Map<K,Tuple2<VA,VB>>
innerJoinMapByKey(Map<K,VA> mapA, Map<K,VB> mapB)
          Returns a Map with the matching keys and the respective value instances within a of Tuple2 wrapper, which has always the value of the first map and the value of the second map.
static
<K,V> Map<V,Set<K>>
invert(Map<? extends K,? extends V> map)
          Returns the inverted Map for the given one.
static
<K,V> Map<V,K>
invertedBidirectionalMap(Map<? extends K,? extends V> map)
          Returns the inverted Map for the given one.
static boolean isMapType(Class<?> type)
          Returns true if the given type is assignable to the Map interface
static boolean isSortedMapType(Class<?> type)
          Returns true if the given type is assignable to the SortedMap interface
static MapJoiner joiner()
          Returns a new MapJoiner instance
static
<K,V> Map.Entry<K,V>
lastEntry(Map<K,V> map)
          Returns the last Map.Entry of the given Map.
static
<K,V> Map<K,V>
locked(Map<K,V> map, Lock lock)
          Returns a view of the given Map using the given Lock to synchronize all of its methods
static
<K,V> Map<K,V>
lockedByReentrantLock(Map<K,V> map)
          Returns a view of the given Map using a new ReentrantLock instance to synchronize all of its methods
static
<K,V> Map<K,V>
mergeAll(Collection<Map<? extends K,? extends V>> mapCollection, MapUtils.MapElementMergeOperation<K,V> mapElementMergeOperation)
          Merges all given Map instances into a single LinkedHashMap using the given MapUtils.MapElementMergeOperation.
static
<K,V> Map<K,V>
mergeAll(Collection<Map<K,V>> mapCollection)
          Merges all given Map instances into a single LinkedHashMap.
static
<K,V> Map<K,V>
mergeAll(Map<K,V>... maps)
          Merges all given Map instances into a single LinkedHashMap.
static
<K,V> Map<K,V>
mergeAll(MapUtils.MapElementMergeOperation<K,V> mapElementMergeOperation, Map<? extends K,? extends V>... maps)
          Merges all given Map instances into a single LinkedHashMap using the given MapUtils.MapElementMergeOperation.
static
<K,V> Map<K,List<V>>
mergeAllValuesIntoList(Collection<Map<K,V>> mapCollection)
          Merges multiple Map instances whereby multiple values for the same key are merged into a List
static
<K,V> Map<K,List<V>>
mergeAllValuesIntoList(Map<K,V>... maps)
          Similar to mergeAllValuesIntoList(Collection)
static
<K,V> Map<K,Set<V>>
mergeAllValuesIntoSet(Collection<Map<K,V>> mapCollection)
          Merges multiple Map instances whereby multiple values for the same key are merged into a Set
static
<K,V> Map<K,Set<V>>
mergeAllValuesIntoSet(Map<K,V>... maps)
          Similar to mergeAllValuesIntoSet(Collection)
static Map<String,String> parseString(String content)
          Similar to parseString(String, String, String)
static Map<String,String> parseString(String content, String entityDelimiterRegEx, String keyValueDelimiterRegEx)
          Parses a given text into a LinkedHashMap instance.
static void printMapHierarchical(PrintStream printStream, Map map)
          Prints out a given Map using String.valueOf(Object) and all submaps indented to a new column.
static
<K,V> boolean
putIfAbsent(Map<K,V> map, K key, Factory<V> valueFactory)
          Puts the given key and value into the given Map only if there is no equal key already contained within the Map.
static
<K,V> boolean
putIfAbsent(Map<K,V> map, K key, V value)
          Puts the given key and value into the given Map only if there is no equal key already contained within the Map.
static int size(Map<?,?> map)
          Returns the size of the given Map or 0 if the given Map reference is null.
static
<TO,K,V> List<TO>
toList(Map<K,V> map, MapUtils.MapEntryToElementConverter<TO,K,V> mapEntryToElementConverter)
          Transforms a given Map to a List using the given MapUtils.MapEntryToElementConverter to create single elements for the List based on the Map.Entrys of the given Map
static
<K,V> String
toString(Map<K,V> map)
          Returns a String representation for a Map
static
<K,V> String
toStringUsingHierarchy(Map<K,V> map)
          Similar to printMapHierarchical(PrintStream, Map) but returning a String value
static
<K,V,E> Map<K,V>
valueOf(Iterable<E> iterable, ElementConverterElementToMapEntry<E,K,V> elementToMapEntryTransformer)
          Returns a new Map instance for the given Iterable using the ElementConverterElementToMapEntry instance to convert every element to an Map.Entry
static
<K,V> Map<K,V>
valueOf(KeyExtractor<? extends K,V> keyExtractor, Iterable<? extends V> iterable)
          Returns a new Map instance for the given Iterable using the given KeyExtractor to extract every key one by one from the Iterable
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MapUtils

public MapUtils()
Method Detail

mergeAll

public static <K,V> Map<K,V> mergeAll(MapUtils.MapElementMergeOperation<K,V> mapElementMergeOperation,
                                      Map<? extends K,? extends V>... maps)
Merges all given Map instances into a single LinkedHashMap using the given MapUtils.MapElementMergeOperation.

Type Parameters:
K -
V -
Parameters:
mapElementMergeOperation -
maps -
Returns:
See Also:
mergeAll(Map...), mergeAll(Collection, MapElementMergeOperation)

mergeAll

public static <K,V> Map<K,V> mergeAll(Collection<Map<? extends K,? extends V>> mapCollection,
                                      MapUtils.MapElementMergeOperation<K,V> mapElementMergeOperation)
Merges all given Map instances into a single LinkedHashMap using the given MapUtils.MapElementMergeOperation.

Type Parameters:
K -
V -
Parameters:
mapCollection -
mapElementMergeOperation -
Returns:
See Also:
mergeAll(Map...)

mergeAll

public static <K,V> Map<K,V> mergeAll(Collection<Map<K,V>> mapCollection)
Merges all given Map instances into a single LinkedHashMap.

Type Parameters:
K -
V -
Parameters:
mapCollection -
Returns:
See Also:
mergeAll(Map...)

mergeAll

public static <K,V> Map<K,V> mergeAll(Map<K,V>... maps)
Merges all given Map instances into a single LinkedHashMap.

Type Parameters:
K -
V -
Parameters:
maps -
Returns:
See Also:
mergeAll(Collection), mergeAll(Collection, MapElementMergeOperation)

mergeAllValuesIntoList

public static <K,V> Map<K,List<V>> mergeAllValuesIntoList(Map<K,V>... maps)
Similar to mergeAllValuesIntoList(Collection)

Parameters:
maps -
Returns:

mergeAllValuesIntoList

public static <K,V> Map<K,List<V>> mergeAllValuesIntoList(Collection<Map<K,V>> mapCollection)
Merges multiple Map instances whereby multiple values for the same key are merged into a List

Parameters:
mapCollection -
Returns:

mergeAllValuesIntoSet

public static <K,V> Map<K,Set<V>> mergeAllValuesIntoSet(Map<K,V>... maps)
Similar to mergeAllValuesIntoSet(Collection)

Parameters:
maps -
Returns:

mergeAllValuesIntoSet

public static <K,V> Map<K,Set<V>> mergeAllValuesIntoSet(Collection<Map<K,V>> mapCollection)
Merges multiple Map instances whereby multiple values for the same key are merged into a Set

Parameters:
mapCollection -
Returns:

innerJoinMapByKey

public static <K,VA,VB> Map<K,Tuple2<VA,VB>> innerJoinMapByKey(Map<K,VA> mapA,
                                                               Map<K,VB> mapB)
Returns a Map with the matching keys and the respective value instances within a of Tuple2 wrapper, which has always the value of the first map and the value of the second map. Only the keys which are contained in both Maps will be returned.

Type Parameters:
K -
VA -
VB -
Parameters:
mapA -
mapB -
Returns:

joiner

public static MapJoiner joiner()
Returns a new MapJoiner instance

Returns:

convertMapKey

public static <KeyFrom,KeyTo,Value> Map<KeyTo,Value> convertMapKey(Map<? extends KeyFrom,? extends Value> map,
                                                                   ElementConverter<KeyFrom,KeyTo> keyElementConverter)
Returns a new Map instance with converted keys using the given ElementConverter

Type Parameters:
KeyFrom -
KeyTo -
Value -
Parameters:
map -
keyElementConverter -
Returns:
new ordered Map
See Also:
convertMap(Map, ElementConverter, ElementConverter), convertMapValue(Map, ElementConverter)

convertMapValue

public static <Key,ValueFrom,ValueTo> Map<Key,ValueTo> convertMapValue(Map<? extends Key,? extends ValueFrom> map,
                                                                       ElementConverter<ValueFrom,ValueTo> valueElementConverter)
Returns a new Map instance with all values converted using the given ElementConverter

Type Parameters:
Key -
ValueFrom -
ValueTo -
Parameters:
map -
valueElementConverter -
Returns:
new ordered Map
See Also:
convertMapKey(Map, ElementConverter), convertMap(Map, ElementConverter, ElementConverter)

convertMap

public static <KeyFrom,KeyTo,ValueFrom,ValueTo> Map<KeyTo,ValueTo> convertMap(Map<? extends KeyFrom,? extends ValueFrom> map,
                                                                              ElementConverter<KeyFrom,KeyTo> keyElementConverter,
                                                                              ElementConverter<ValueFrom,ValueTo> valueElementConverter)
Returns a new Map instance with converted keys and values using the given ElementConverters

Parameters:
map -
keyElementConverter -
valueElementConverter -
Returns:
new ordered Map
See Also:
convertMapKey(Map, ElementConverter), convertMapValue(Map, ElementConverter), ElementConverter

convertMap

public static <KeyFrom,KeyTo,ValueFrom,ValueTo> Map<KeyTo,ValueTo> convertMap(Map<? extends KeyFrom,? extends ValueFrom> map,
                                                                              ElementConverter<Map.Entry<KeyFrom,ValueFrom>,Map.Entry<KeyTo,ValueTo>> entryElementConverter)
Similar to convertMap(Map, ElementConverter, ElementConverter) using a single ElementConverter for an Map.Entry

Parameters:
map - Map
entryElementConverter - ElementConverter
Returns:
new Map instance

toStringUsingHierarchy

public static <K,V> String toStringUsingHierarchy(Map<K,V> map)
Similar to printMapHierarchical(PrintStream, Map) but returning a String value

Parameters:
map -
Returns:

toString

public static <K,V> String toString(Map<K,V> map)
Returns a String representation for a Map

Parameters:
map -
Returns:

toList

public static <TO,K,V> List<TO> toList(Map<K,V> map,
                                       MapUtils.MapEntryToElementConverter<TO,K,V> mapEntryToElementConverter)
Transforms a given Map to a List using the given MapUtils.MapEntryToElementConverter to create single elements for the List based on the Map.Entrys of the given Map

Parameters:
map -
mapEntryToElementConverter -
Returns:
List

filteredMap

public static <K,V> Map<K,V> filteredMap(Map<K,V> map,
                                         ElementFilter<K> keyElementFilter)
Returns a new filtered Map instance. All keys of the given Map are filtered using the given ElementFilter

Parameters:
map -
keyElementFilter -
Returns:
See Also:
filteredMap(Map, Iterable)

filteredMap

public static <K,V> Map<K,V> filteredMap(Map<K,V> map,
                                         Iterable<K> filterKeyIterable)
Filters a given Map by its keys. Only keys which are contained within the given key Iterable will be returned.

Parameters:
map -
filterKeyIterable -
Returns:
new LinkedHashMap instance
See Also:
filteredMap(Map, ElementFilter)

filteredMapExcludingNullValues

public static <K,V> Map<K,V> filteredMapExcludingNullValues(Map<K,V> map)
Filters a given Map by values which are null. Only keys which have a value not null will be retained.

Parameters:
map -
Returns:
new LinkedHashMap instance

printMapHierarchical

public static void printMapHierarchical(PrintStream printStream,
                                        Map map)
Prints out a given Map using String.valueOf(Object) and all submaps indented to a new column.

Example:
 -+
  |-- valueDouble=1.234
  |-+ testClassCopy
  | |-- valueDouble=5.678
  | |-- testClassCopy=null
  | |-- privateField=privateValue0.16433438667207334
  | |-+ future
  | | |-- countDownLatch=java.util.concurrent.CountDownLatch@1f4384c2[Count = 1]
  | | |-- shouldCancel=false
  | | |-- isCancelled=false
  | | |-- value=null
  | | |-- clazz=org.omnaest.utils.structure.element.FutureSimple
 

Parameters:
printStream -
map -

locked

public static <K,V> Map<K,V> locked(Map<K,V> map,
                                    Lock lock)
Returns a view of the given Map using the given Lock to synchronize all of its methods

Parameters:
map -
lock -
Returns:
See Also:
lockedByReentrantLock(Map)

lockedByReentrantLock

public static <K,V> Map<K,V> lockedByReentrantLock(Map<K,V> map)
Returns a view of the given Map using a new ReentrantLock instance to synchronize all of its methods

Parameters:
map -
Returns:
See Also:
locked(Map, Lock)

invertedBidirectionalMap

public static <K,V> Map<V,K> invertedBidirectionalMap(Map<? extends K,? extends V> map)
Returns the inverted Map for the given one.

The given Map has to be bidirectional, which means that the key value pairs inverted into value to key pairs have unique values, too.

If the given Map reference is null, null is returned.

For non bidirectional Maps use invert(Map) instead.

If a given Map has some non unique values the first occurring value wins and all further values are dropped.

Parameters:
map -
Returns:
LinkedHashMap

invert

public static <K,V> Map<V,Set<K>> invert(Map<? extends K,? extends V> map)
Returns the inverted Map for the given one. If the given Map reference is null, null is returned.

Since Multiple key of the original Map can be mapped to the same value, the inverted Map will contain a Set of original key values per original value.

For bidirectional Maps consider using invertedBidirectionalMap(Map)

Parameters:
map -
Returns:
LinkedHashMap and LinkedHashSet

initializedEnumMap

public static <K extends Enum<K>,V> EnumMap<K,V> initializedEnumMap(Class<K> enumType,
                                                                    Factory<V> factory)
Returns an EnumMap filled with all available values of the given Enum type as keys and the result of the Factory as value for each Enum key.

Parameters:
enumType -
factory -
Returns:
EnumMap

initializeMap

public static <K,V> void initializeMap(Map<K,V> map,
                                       Iterable<K> keyIterable,
                                       Factory<V> valueFactory)
Similar to initializeMap(Map, Iterable, Factory, boolean) but does not overwrite values of already existing keys.

Parameters:
map -
keyIterable -
valueFactory -

initializeMap

public static <K,V> void initializeMap(Map<K,V> map,
                                       Iterable<K> keyIterable,
                                       Factory<V> valueFactory,
                                       boolean overwriteValuesOfExistingKeys)
Initializes the given Map for all keys from the key Iterable with values created by the value Factory.newInstance() method. If the overwrite values of existing keys flag is set to true, any value of an already existing key is overwritten.

Parameters:
map -
keyIterable -
valueFactory -
overwriteValuesOfExistingKeys -
See Also:
initializedMap(Map, Factory)

initializedMap

public static <K,V> Map<K,V> initializedMap(Factory<V> valueFactory)
Similar to initializedMap(Map, Factory) using a new LinkedHashMap instance

Parameters:
valueFactory -
Returns:

initializedMap

public static <K,V> Map<K,V> initializedMap(FactoryParameterized<V,K> valueFactory)
Similar to initializedMap(Map, FactoryParameterized) using a new LinkedHashMap instance

Parameters:
valueFactory -
Returns:

initializedMap

public static <K,V> Map<K,V> initializedMap(Map<K,V> map,
                                            Factory<V> valueFactory)
Returns a MapDecorator which ensures that all Map.get(Object) invocations with a valid key type will return a value. If the underlying Map would return a null value the value Factory.newInstance() is invoked and the new value is stored within the Map.

This is e.g. useful for scenarios where a Map contains a Collection as value and the Collection should always be present.

Be aware of the fact, that Map.containsKey(Object) will still return false for any non existing key.

Parameters:
map -
valueFactory -
Returns:
Map decorator
See Also:
initializeMap(Map, Iterable, Factory)

initializedValueListMap

public static <K,V> Map<K,List<V>> initializedValueListMap()
Short form of initializedMap(Factory) using an ArrayListFactory

Returns:

initializedValueListMap

public static <K,V> Map<K,List<V>> initializedValueListMap(Map<K,List<V>> map)
Short form of initializedMap(Factory) using an ArrayListFactory

Parameters:
map - underlying Map
Returns:

initializedValueSetMap

public static <K,V> Map<K,Set<V>> initializedValueSetMap()
Short form of initializedMap(Factory) using an LinkedHashSetFactory

Returns:

initializedValueSetMap

public static <K,V> Map<K,Set<V>> initializedValueSetMap(Map<K,Set<V>> map)
Short form of initializedMap(Factory) using an LinkedHashSetFactory

Parameters:
map - underlying Map
Returns:

initializedMap

public static <K,V> Map<K,V> initializedMap(Map<K,V> map,
                                            FactoryParameterized<V,K> valueFactory)
Returns a MapDecorator which ensures that all Map.get(Object) invocations with a valid key type will return a value. If the underlying Map would return a null value the value FactoryParameterized.newInstance(Object) with the key as argument is invoked and the new value is stored within the Map.

This is e.g. useful for scenarios where a Map contains a Collection as value and the Collection should always be present.

Be aware of the fact, that Map.containsKey(Object) will still return false for any non existing key.

Parameters:
map -
valueFactory - FactoryParameterized
Returns:
Map decorator
See Also:
initializeMap(Map, Iterable, Factory), initializedMap(Map, Factory)

defaultValueMap

public static <K,V> Map<K,V> defaultValueMap(Map<K,V> map,
                                             V defaultValue)
Similar to defaultValueMap(Map, FactoryParameterized)

Parameters:
map -
defaultValue -
Returns:
See Also:
initializedMap(FactoryParameterized)

defaultValueMap

public static <K,V> Map<K,V> defaultValueMap(Map<K,V> map,
                                             FactoryParameterized<V,K> valueFactory)
Returns a Map which always returns a value prodcued by the given factory if the value would have be null

Parameters:
map -
valueFactory -
Returns:
See Also:
initializedMap(Map, FactoryParameterized)

initializedSortedMap

public static <K,V> SortedMap<K,V> initializedSortedMap(SortedMap<K,V> sortedMap,
                                                        Factory<V> valueFactory)
Similar to initializedMap(Map, Factory) but for any SortedMap instance

Parameters:
sortedMap -
valueFactory -
Returns:
SortedMap decorator

getValueByRegex

public static <E> E getValueByRegex(Map<String,? extends E> map,
                                    String regex)
Returns the value of the first key within a Map which matches a given regular expression.

 MapUtils.getValueByRegex( null, anyRegex ) = null
 MapUtils.getValueByRegex( map, null ) = null
 MapUtils.getValueByRegex( map, regex ) = value for first key matching the regex
 MapUtils.getValueByRegex( map, ".*" ) = first value of the map for LinkedHashMap, random value for HashMap
 
 

Example:
 //
 final Map<String, String> map = new LinkedHashMap<String, String>();
 map.put( "key1", "value1" );
 map.put( "key2", "value2" );
 map.put( "thisKey", "value3" );
 
 //
 String regex = "this.*";
 String value = MapUtils.getValueByRegex( map, regex );
 
 //
 assertEquals( "value3", value );
 

Parameters:
map -
regex -
Returns:

firstEntry

public static <K,V> Map.Entry<K,V> firstEntry(Map<K,V> map)
Returns the first Map.Entry of a given Map or null if the Map reference is null or the Map is empty

Parameters:
map -
Returns:
See Also:
lastEntry(Map), entryAt(Map, int)

lastEntry

public static <K,V> Map.Entry<K,V> lastEntry(Map<K,V> map)
Returns the last Map.Entry of the given Map. If the given Map reference is null or the Map is empty, null will be returned.

Parameters:
map -
Returns:
See Also:
firstEntry(Map), entryAt(Map, int)

entryAt

public static <K,V> Map.Entry<K,V> entryAt(Map<K,V> map,
                                           int indexPosition)
Returns the Map.Entry at the given index position of the given Map. If null is given as Map null will be returned. If the index position is invalid null is returned, too.

Parameters:
map -
indexPosition -
Returns:
See Also:
firstEntry(Map), lastEntry(Map)

valueOf

public static <K,V> Map<K,V> valueOf(KeyExtractor<? extends K,V> keyExtractor,
                                     Iterable<? extends V> iterable)
Returns a new Map instance for the given Iterable using the given KeyExtractor to extract every key one by one from the Iterable

Parameters:
keyExtractor - KeyExtractor
iterable - Iterable
Returns:
new Map

valueOf

public static <K,V,E> Map<K,V> valueOf(Iterable<E> iterable,
                                       ElementConverterElementToMapEntry<E,K,V> elementToMapEntryTransformer)
Returns a new Map instance for the given Iterable using the ElementConverterElementToMapEntry instance to convert every element to an Map.Entry

Parameters:
iterable -
elementToMapEntryTransformer -
Returns:

composite

public static <K,V> Map<K,V> composite(Map<K,V>... maps)
Returns a new MapComposite instance for the given Maps

Parameters:
maps -
Returns:

composite

public static <K,V> Map<K,V> composite(List<Map<K,V>> mapList)
Returns a new MapComposite instance for the given Maps

Parameters:
mapList -
Returns:

adapter

public static <KEY_FROM,VALUE_FROM,KEY_TO,VALUE_TO> Map<KEY_TO,VALUE_TO> adapter(Map<KEY_FROM,VALUE_FROM> sourceMap,
                                                                                 ElementBidirectionalConverter<KEY_FROM,KEY_TO> elementBidirectionalConverterKey,
                                                                                 ElementBidirectionalConverter<VALUE_FROM,VALUE_TO> elementBidirectionalConverterValue)
Returns a MapToMapAdapter for the given source Map

Parameters:
sourceMap - Map
elementBidirectionalConverterKey - ElementBidirectionalConverter
elementBidirectionalConverterValue - ElementBidirectionalConverter
Returns:
new MapToMapAdapter instance

adapter

public static <KEY,VALUE_FROM,VALUE_TO> SortedMap<KEY,VALUE_TO> adapter(SortedMap<KEY,VALUE_FROM> sourceMap,
                                                                        ElementBidirectionalConverter<VALUE_FROM,VALUE_TO> elementBidirectionalConverterValue)
Returns a SortedMapToSortedMapAdapter for the given source SortedMap

Parameters:
sourceMap - SortedMap
elementBidirectionalConverterValue - ElementBidirectionalConverter
Returns:
new SortedMapToSortedMapAdapter instance

parseString

public static Map<String,String> parseString(String content)
Similar to parseString(String, String, String)

Parameters:
content -
Returns:

parseString

public static Map<String,String> parseString(String content,
                                             String entityDelimiterRegEx,
                                             String keyValueDelimiterRegEx)
Parses a given text into a LinkedHashMap instance.

Example:
 key1=value1;key2=value2
 
would be parsed into a Map with the keys "key1" and "key2" and "key1" would have the value "value1" and similar for "key2".

Parameters:
content -
entityDelimiterRegEx - defaults to ";" or "|"
keyValueDelimiterRegEx - defaults to "=" or ":"
Returns:

isMapType

public static boolean isMapType(Class<?> type)
Returns true if the given type is assignable to the Map interface

Parameters:
type -
Returns:

isSortedMapType

public static boolean isSortedMapType(Class<?> type)
Returns true if the given type is assignable to the SortedMap interface

Parameters:
type -
Returns:

builder

public static MapBuilder builder()
Returns a new MapBuilder instance.

Examples:
 Map<String, Long> map = MapUtils.builder()
                                 .put( "key1", 1l )
                                 .put( "key2", 2l )
                                 .put( "key3", 3l )
                                 .put( "key4", 4l )
                                 .buildAs()
                                 .linkedHashMap();
 
 Map<String, Object> map = MapUtils.builder()
                                   .<String, Object> put( "key1", 1l )
                                   .put( "key2", 2l )
                                   .put( "key3", 3l )
                                   .put( "key4", 4l )
                                   .buildAs()
                                   .linkedHashMap();
 

Returns:

size

public static int size(Map<?,?> map)
Returns the size of the given Map or 0 if the given Map reference is null.

Parameters:
map -
Returns:

filteredValues

public static <K,V> V[] filteredValues(Map<K,V> map,
                                       Class<V> valueType,
                                       K... keys)

initializedCounterMap

public static <E> Map<E,AtomicInteger> initializedCounterMap()
Returns a Map instance which returns for Map.get(Object) always an AtomicInteger instance. This allows statements like:
 Map<Object, AtomicInteger> map = MapUtils.initializedCounterMap();
 int value1 = map.get( "lala" ).getAndIncrement();
 int value2 = map.get( "lala" ).incrementAndGet();
 assertEquals( 0, value1 );
 assertEquals( 2, value2 );
 

Returns:

delta

public static <K,V> MapDelta<K,V> delta(Map<K,V> mapFirst,
                                        Map<K,V> mapSecond)
Returns a new MapDelta for the given two Map instances

Parameters:
mapFirst -
mapSecond -
Returns:
new MapDelta
See Also:
SetUtils.delta(Set, Set), Map

putIfAbsent

public static <K,V> boolean putIfAbsent(Map<K,V> map,
                                        K key,
                                        V value)
Puts the given key and value into the given Map only if there is no equal key already contained within the Map.
If the given Map is null, the method does nothing.

Parameters:
map - Map
key -
value -
Returns:
true, if the key and value were put into the Map

putIfAbsent

public static <K,V> boolean putIfAbsent(Map<K,V> map,
                                        K key,
                                        Factory<V> valueFactory)
Puts the given key and value into the given Map only if there is no equal key already contained within the Map.
The value factory is only invoked, if the value has to be put into the Map.
If the given Map is null, the method does nothing.

Parameters:
map - Map
key -
valueFactory - Factory for a single value
Returns:
true, if the key and value were put into the Map


Copyright © 2013. All Rights Reserved.