Class FixedSizeMap<K,​V>

  • Type Parameters:
    K - the type of the keys in this map
    V - the type of the values in this map
    All Implemented Interfaces:
    java.io.Serializable, java.util.Map<K,​V>, BoundedMap<K,​V>, Get<K,​V>, IterableGet<K,​V>, IterableMap<K,​V>, Put<K,​V>

    public class FixedSizeMap<K,​V>
    extends AbstractMapDecorator<K,​V>
    implements BoundedMap<K,​V>, java.io.Serializable
    Decorates another Map to fix the size, preventing add/remove.

    Any action that would change the size of the map is disallowed. The put method is allowed to change the value associated with an existing key however.

    If trying to remove or clear the map, an UnsupportedOperationException is thrown. If trying to put a new mapping into the map, an IllegalArgumentException is thrown. This is because the put method can succeed if the mapping's key already exists in the map, so the put method is not always unsupported.

    Note that FixedSizeMap is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. The simplest approach is to wrap this map using Collections.synchronizedMap(Map). This class may throw exceptions when accessed by concurrent threads without synchronization.

    This class is Serializable from Commons Collections 3.1.

    Since:
    3.0
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static long serialVersionUID
      Serialization version
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected FixedSizeMap​(java.util.Map<K,​V> map)
      Constructor that wraps (not copies).
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      static <K,​V>
      FixedSizeMap<K,​V>
      fixedSizeMap​(java.util.Map<K,​V> map)
      Factory method to create a fixed size map.
      boolean isFull()
      Returns true if this map is full and no new elements can be added.
      java.util.Set<K> keySet()  
      int maxSize()
      Gets the maximum size of the map (the bound).
      V put​(K key, V value)
      Note that the return type is Object, rather than V as in the Map interface.
      void putAll​(java.util.Map<? extends K,​? extends V> mapToCopy)  
      private void readObject​(java.io.ObjectInputStream in)
      Read the map in using a custom routine.
      V remove​(java.lang.Object key)  
      java.util.Collection<V> values()  
      private void writeObject​(java.io.ObjectOutputStream out)
      Write the map out using a custom routine.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, equals, forEach, get, getOrDefault, hashCode, isEmpty, merge, putIfAbsent, remove, replace, replace, replaceAll, size
    • Field Detail

      • serialVersionUID

        private static final long serialVersionUID
        Serialization version
        See Also:
        Constant Field Values
    • Constructor Detail

      • FixedSizeMap

        protected FixedSizeMap​(java.util.Map<K,​V> map)
        Constructor that wraps (not copies).
        Parameters:
        map - the map to decorate, must not be null
        Throws:
        java.lang.NullPointerException - if map is null
    • Method Detail

      • fixedSizeMap

        public static <K,​V> FixedSizeMap<K,​V> fixedSizeMap​(java.util.Map<K,​V> map)
        Factory method to create a fixed size map.
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        map - the map to decorate, must not be null
        Returns:
        a new fixed size map
        Throws:
        java.lang.NullPointerException - if map is null
        Since:
        4.0
      • writeObject

        private void writeObject​(java.io.ObjectOutputStream out)
                          throws java.io.IOException
        Write the map out using a custom routine.
        Parameters:
        out - the output stream
        Throws:
        java.io.IOException - if an error occurs while writing to the stream
        Since:
        3.1
      • readObject

        private void readObject​(java.io.ObjectInputStream in)
                         throws java.io.IOException,
                                java.lang.ClassNotFoundException
        Read the map in using a custom routine.
        Parameters:
        in - the input stream
        Throws:
        java.io.IOException - if an error occurs while reading from the stream
        java.lang.ClassNotFoundException - if an object read from the stream can not be loaded
        Since:
        3.1
      • put

        public V put​(K key,
                     V value)
        Description copied from interface: Put
        Note that the return type is Object, rather than V as in the Map interface. See the class Javadoc for further info.
        Specified by:
        put in interface java.util.Map<K,​V>
        Specified by:
        put in interface Put<K,​V>
        Overrides:
        put in class AbstractMapDecorator<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        value - value to be associated with the specified key
        Returns:
        the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)
        See Also:
        Map.put(Object, Object)
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> mapToCopy)
        Specified by:
        putAll in interface java.util.Map<K,​V>
        Specified by:
        putAll in interface Put<K,​V>
        Overrides:
        putAll in class AbstractMapDecorator<K,​V>
        Parameters:
        mapToCopy - mappings to be stored in this map
        See Also:
        Map.putAll(Map)
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<K,​V>
        Specified by:
        clear in interface Put<K,​V>
        Overrides:
        clear in class AbstractMapDecorator<K,​V>
        See Also:
        Map.clear()
      • remove

        public V remove​(java.lang.Object key)
        Specified by:
        remove in interface Get<K,​V>
        Specified by:
        remove in interface java.util.Map<K,​V>
        Overrides:
        remove in class AbstractMapDecorator<K,​V>
        Parameters:
        key - key whose mapping is to be removed from the map
        Returns:
        the previous value associated with key, or null if there was no mapping for key.
        See Also:
        Map.remove(Object)
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet in interface Get<K,​V>
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Overrides:
        entrySet in class AbstractMapDecorator<K,​V>
        Returns:
        a set view of the mappings contained in this map
        See Also:
        Map.entrySet()
      • keySet

        public java.util.Set<K> keySet()
        Specified by:
        keySet in interface Get<K,​V>
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Overrides:
        keySet in class AbstractMapDecorator<K,​V>
        Returns:
        a set view of the keys contained in this map
        See Also:
        Map.keySet()
      • values

        public java.util.Collection<V> values()
        Specified by:
        values in interface Get<K,​V>
        Specified by:
        values in interface java.util.Map<K,​V>
        Overrides:
        values in class AbstractMapDecorator<K,​V>
        Returns:
        a collection view of the values contained in this map
        See Also:
        Map.values()
      • isFull

        public boolean isFull()
        Description copied from interface: BoundedMap
        Returns true if this map is full and no new elements can be added.
        Specified by:
        isFull in interface BoundedMap<K,​V>
        Returns:
        true if the map is full
      • maxSize

        public int maxSize()
        Description copied from interface: BoundedMap
        Gets the maximum size of the map (the bound).
        Specified by:
        maxSize in interface BoundedMap<K,​V>
        Returns:
        the maximum number of elements the map can hold