Package org.goplanit.utils.wrapper
Class MapWrapperImpl<K,V>
- java.lang.Object
-
- org.goplanit.utils.wrapper.MapWrapperImpl<K,V>
-
- Type Parameters:
K
- map keyV
- map value
- All Implemented Interfaces:
Iterable<V>
,MapWrapper<K,V>
- Direct Known Subclasses:
LongMapWrapperImpl
public class MapWrapperImpl<K,V> extends Object implements MapWrapper<K,V>
Abstract Wrapper class implemented for a map instance- Author:
- markr
-
-
Constructor Summary
Constructors Constructor Description MapWrapperImpl(Map<K,V> mapToWrap, Function<V,K> valueToKey)
ConstructorMapWrapperImpl(Map<K,V> mapToWrap, Function<V,K> valueToKey, Collection<V> populateWith)
ConstructorMapWrapperImpl(Map<K,V> mapToWrap, Function<V,K> valueToKey, MapWrapper<U,V> populateWith)
Special copy(like) constructor allowing one to create a copy with a different index functionMapWrapperImpl(MapWrapperImpl<K,V> other)
Copy constructor which creates a new underlying map and copies entries over (shallow)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Clear all entries from mapboolean
containsValue(V value)
Check if value is already presentprotected static <U,L>
Map<U,L>createEmptyInstance(Map<U,L> mapToCopy)
Create an empty map of the same implementation using reflection, i.e., if the map is a TreeMap a TreeMap is used, same for hashmap etc.V
firstMatch(Predicate<V> valuePredicate)
Find first entry that matches the predicateV
get(K key)
Collect entry from containerK
getKeyByValue(V value)
collect the key used for the given valueprotected Map<K,V>
getMap()
Access to the wrapped mapFunction<V,K>
getValueToKey()
Collect the function used to map value to keyboolean
isEmpty()
Verify if the map is emptyIterator<V>
iterator()
V
register(V value)
Register on the internal container (no null keys allowed which will trigger a warning and the value not to be registered)V
remove(V value)
Remove value from mapvoid
removeIf(Predicate<V> condition)
Remove all values that satisfy the given conditionprotected void
setMap(Map<K,V> replacement)
Replace the wrapped mapMapWrapperImpl<K,V>
shallowClone()
Each map wrapper should be cloneable where the contents are references of the original where possible but the underlying map itself is newly createdint
size()
The number of registered entriesCollection<V>
toCollection()
Collect values as unmodifiable collectionSet<V>
valuesAsNewSet()
Create a copy of the map's distinct values as a set-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Methods inherited from interface org.goplanit.utils.wrapper.MapWrapper
addAll, forEachIn, getFirst, removeAll, stream, streamSorted, toMap
-
-
-
-
Constructor Detail
-
MapWrapperImpl
public MapWrapperImpl(Map<K,V> mapToWrap, Function<V,K> valueToKey)
Constructor- Parameters:
mapToWrap
- the map to wrapvalueToKey
- function to map values to their key
-
MapWrapperImpl
public MapWrapperImpl(Map<K,V> mapToWrap, Function<V,K> valueToKey, Collection<V> populateWith)
Constructor- Parameters:
mapToWrap
- the map to wrapvalueToKey
- function to map values to their keypopulateWith
- values to populate the map to wrap with based on index function
-
MapWrapperImpl
public MapWrapperImpl(Map<K,V> mapToWrap, Function<V,K> valueToKey, MapWrapper<U,V> populateWith)
Special copy(like) constructor allowing one to create a copy with a different index function- Type Parameters:
U
- key type of map with values to populate this wrapper with- Parameters:
mapToWrap
- the map to wrapvalueToKey
- function to map values to their keypopulateWith
- values to populate the map to wrap with based on provided index function
-
MapWrapperImpl
public MapWrapperImpl(MapWrapperImpl<K,V> other)
Copy constructor which creates a new underlying map and copies entries over (shallow)- Parameters:
other
- to copy
-
-
Method Detail
-
createEmptyInstance
protected static <U,L> Map<U,L> createEmptyInstance(Map<U,L> mapToCopy)
Create an empty map of the same implementation using reflection, i.e., if the map is a TreeMap a TreeMap is used, same for hashmap etc.- Type Parameters:
U
- map to copy key typeL
- map to copy value type- Parameters:
mapToCopy
- the map to copy- Returns:
- created copy with the same underlying map implementation
-
getValueToKey
public Function<V,K> getValueToKey()
Collect the function used to map value to key- Returns:
- valueToKey
-
setMap
protected void setMap(Map<K,V> replacement)
Replace the wrapped map- Parameters:
replacement
- map to use as replacement
-
register
public V register(V value)
Register on the internal container (no null keys allowed which will trigger a warning and the value not to be registered)- Specified by:
register
in interfaceMapWrapper<K,V>
- Parameters:
value
- to register- Returns:
- old value if any
-
remove
public V remove(V value)
Remove value from map- Specified by:
remove
in interfaceMapWrapper<K,V>
- Parameters:
value
- to remove- Returns:
- removed entry if any
-
removeIf
public void removeIf(Predicate<V> condition)
Remove all values that satisfy the given condition- Specified by:
removeIf
in interfaceMapWrapper<K,V>
- Parameters:
condition
- to remove
-
get
public V get(K key)
Collect entry from container- Specified by:
get
in interfaceMapWrapper<K,V>
- Parameters:
key
- to use- Returns:
- value for key
-
size
public int size()
The number of registered entries- Specified by:
size
in interfaceMapWrapper<K,V>
- Returns:
- number of entries
-
isEmpty
public boolean isEmpty()
Verify if the map is empty- Specified by:
isEmpty
in interfaceMapWrapper<K,V>
- Returns:
- true when empty false otherwise
-
toCollection
public Collection<V> toCollection()
Collect values as unmodifiable collection- Specified by:
toCollection
in interfaceMapWrapper<K,V>
- Returns:
- collection
-
valuesAsNewSet
public Set<V> valuesAsNewSet()
Create a copy of the map's distinct values as a set- Specified by:
valuesAsNewSet
in interfaceMapWrapper<K,V>
- Returns:
- copy of values as set
-
firstMatch
public V firstMatch(Predicate<V> valuePredicate)
Find first entry that matches the predicate- Specified by:
firstMatch
in interfaceMapWrapper<K,V>
- Parameters:
valuePredicate
- that checks a property of the value and the first which matches is returned- Returns:
- the retrieved entry, or null if no traveler type was found
-
shallowClone
public MapWrapperImpl<K,V> shallowClone()
Each map wrapper should be cloneable where the contents are references of the original where possible but the underlying map itself is newly created- Specified by:
shallowClone
in interfaceMapWrapper<K,V>
- Returns:
- copy
-
containsValue
public boolean containsValue(V value)
Check if value is already present- Specified by:
containsValue
in interfaceMapWrapper<K,V>
- Parameters:
value
- to verify- Returns:
- true if present, false otherwise
-
getKeyByValue
public K getKeyByValue(V value)
collect the key used for the given value- Specified by:
getKeyByValue
in interfaceMapWrapper<K,V>
- Parameters:
value
- to collect used key for- Returns:
- key
-
clear
public void clear()
Clear all entries from map- Specified by:
clear
in interfaceMapWrapper<K,V>
-
-