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 voidclear()Clear all entries from mapbooleancontainsValue(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.VfirstMatch(Predicate<V> valuePredicate)Find first entry that matches the predicateVget(K key)Collect entry from containerKgetKeyByValue(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 keybooleanisEmpty()Verify if the map is emptyIterator<V>iterator()Vregister(V value)Register on the internal container (no null keys allowed which will trigger a warning and the value not to be registered)Vremove(V value)Remove value from mapvoidremoveIf(Predicate<V> condition)Remove all values that satisfy the given conditionprotected voidsetMap(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 createdintsize()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:
registerin interfaceMapWrapper<K,V>- Parameters:
value- to register- Returns:
- old value if any
-
remove
public V remove(V value)
Remove value from map- Specified by:
removein 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:
removeIfin interfaceMapWrapper<K,V>- Parameters:
condition- to remove
-
get
public V get(K key)
Collect entry from container- Specified by:
getin interfaceMapWrapper<K,V>- Parameters:
key- to use- Returns:
- value for key
-
size
public int size()
The number of registered entries- Specified by:
sizein interfaceMapWrapper<K,V>- Returns:
- number of entries
-
isEmpty
public boolean isEmpty()
Verify if the map is empty- Specified by:
isEmptyin interfaceMapWrapper<K,V>- Returns:
- true when empty false otherwise
-
toCollection
public Collection<V> toCollection()
Collect values as unmodifiable collection- Specified by:
toCollectionin interfaceMapWrapper<K,V>- Returns:
- collection
-
valuesAsNewSet
public Set<V> valuesAsNewSet()
Create a copy of the map's distinct values as a set- Specified by:
valuesAsNewSetin 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:
firstMatchin 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:
shallowClonein interfaceMapWrapper<K,V>- Returns:
- copy
-
containsValue
public boolean containsValue(V value)
Check if value is already present- Specified by:
containsValuein 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:
getKeyByValuein interfaceMapWrapper<K,V>- Parameters:
value- to collect used key for- Returns:
- key
-
clear
public void clear()
Clear all entries from map- Specified by:
clearin interfaceMapWrapper<K,V>
-
-