Class MapWrapperImpl<K,​V>

  • Type Parameters:
    K - map key
    V - 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 Detail

      • MapWrapperImpl

        public MapWrapperImpl​(Map<K,​V> mapToWrap,
                              Function<V,​K> valueToKey)
        Constructor
        Parameters:
        mapToWrap - the map to wrap
        valueToKey - 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 wrap
        valueToKey - function to map values to their key
        populateWith - 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 wrap
        valueToKey - function to map values to their key
        populateWith - 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 type
        L - 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
      • getMap

        protected Map<K,​V> getMap()
        Access to the wrapped map
        Returns:
        wrapper map
      • 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 interface MapWrapper<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 interface MapWrapper<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 interface MapWrapper<K,​V>
        Parameters:
        condition - to remove
      • get

        public V get​(K key)
        Collect entry from container
        Specified by:
        get in interface MapWrapper<K,​V>
        Parameters:
        key - to use
        Returns:
        value for key
      • size

        public int size()
        The number of registered entries
        Specified by:
        size in interface MapWrapper<K,​V>
        Returns:
        number of entries
      • isEmpty

        public boolean isEmpty()
        Verify if the map is empty
        Specified by:
        isEmpty in interface MapWrapper<K,​V>
        Returns:
        true when empty false otherwise
      • toCollection

        public Collection<V> toCollection()
        Collect values as unmodifiable collection
        Specified by:
        toCollection in interface MapWrapper<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 interface MapWrapper<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 interface MapWrapper<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 interface MapWrapper<K,​V>
        Returns:
        copy
      • containsValue

        public boolean containsValue​(V value)
        Check if value is already present
        Specified by:
        containsValue in interface MapWrapper<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 interface MapWrapper<K,​V>
        Parameters:
        value - to collect used key for
        Returns:
        key
      • clear

        public void clear()
        Clear all entries from map
        Specified by:
        clear in interface MapWrapper<K,​V>