FixedSizeMap.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.map;

  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.io.Serializable;
  22. import java.util.Collection;
  23. import java.util.Map;
  24. import java.util.Set;

  25. import org.apache.commons.collections4.BoundedMap;
  26. import org.apache.commons.collections4.collection.UnmodifiableCollection;
  27. import org.apache.commons.collections4.set.UnmodifiableSet;

  28. /**
  29.  * Decorates another {@code Map} to fix the size, preventing add/remove.
  30.  * <p>
  31.  * Any action that would change the size of the map is disallowed.
  32.  * The put method is allowed to change the value associated with an existing
  33.  * key however.
  34.  * </p>
  35.  * <p>
  36.  * If trying to remove or clear the map, an UnsupportedOperationException is
  37.  * thrown. If trying to put a new mapping into the map, an
  38.  * IllegalArgumentException is thrown. This is because the put method can
  39.  * succeed if the mapping's key already exists in the map, so the put method
  40.  * is not always unsupported.
  41.  * </p>
  42.  * <p>
  43.  * <strong>Note that FixedSizeMap is not synchronized and is not thread-safe.</strong>
  44.  * If you wish to use this map from multiple threads concurrently, you must use
  45.  * appropriate synchronization. The simplest approach is to wrap this map
  46.  * using {@link java.util.Collections#synchronizedMap(Map)}. This class may throw
  47.  * exceptions when accessed by concurrent threads without synchronization.
  48.  * </p>
  49.  * <p>
  50.  * This class is Serializable from Commons Collections 3.1.
  51.  * </p>
  52.  *
  53.  * @param <K> the type of the keys in this map
  54.  * @param <V> the type of the values in this map
  55.  * @since 3.0
  56.  */
  57. public class FixedSizeMap<K, V>
  58.         extends AbstractMapDecorator<K, V>
  59.         implements BoundedMap<K, V>, Serializable {

  60.     /** Serialization version */
  61.     private static final long serialVersionUID = 7450927208116179316L;

  62.     /**
  63.      * Factory method to create a fixed size map.
  64.      *
  65.      * @param <K>  the key type
  66.      * @param <V>  the value type
  67.      * @param map  the map to decorate, must not be null
  68.      * @return a new fixed size map
  69.      * @throws NullPointerException if map is null
  70.      * @since 4.0
  71.      */
  72.     public static <K, V> FixedSizeMap<K, V> fixedSizeMap(final Map<K, V> map) {
  73.         return new FixedSizeMap<>(map);
  74.     }

  75.     /**
  76.      * Constructor that wraps (not copies).
  77.      *
  78.      * @param map  the map to decorate, must not be null
  79.      * @throws NullPointerException if map is null
  80.      */
  81.     protected FixedSizeMap(final Map<K, V> map) {
  82.         super(map);
  83.     }

  84.     @Override
  85.     public void clear() {
  86.         throw new UnsupportedOperationException("Map is fixed size");
  87.     }

  88.     @Override
  89.     public Set<Map.Entry<K, V>> entrySet() {
  90.         final Set<Map.Entry<K, V>> set = map.entrySet();
  91.         // unmodifiable set will still allow modification via Map.Entry objects
  92.         return UnmodifiableSet.unmodifiableSet(set);
  93.     }

  94.     @Override
  95.     public boolean isFull() {
  96.         return true;
  97.     }

  98.     @Override
  99.     public Set<K> keySet() {
  100.         final Set<K> set = map.keySet();
  101.         return UnmodifiableSet.unmodifiableSet(set);
  102.     }

  103.     @Override
  104.     public int maxSize() {
  105.         return size();
  106.     }

  107.     @Override
  108.     public V put(final K key, final V value) {
  109.         if (!map.containsKey(key)) {
  110.             throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
  111.         }
  112.         return map.put(key, value);
  113.     }

  114.     @Override
  115.     public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
  116.         for (final K key : mapToCopy.keySet()) {
  117.             if (!containsKey(key)) {
  118.                 throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
  119.             }
  120.         }
  121.         map.putAll(mapToCopy);
  122.     }

  123.     /**
  124.      * Deserializes the map in using a custom routine.
  125.      *
  126.      * @param in  the input stream
  127.      * @throws IOException if an error occurs while reading from the stream
  128.      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
  129.      * @since 3.1
  130.      */
  131.     @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect
  132.     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
  133.         in.defaultReadObject();
  134.         map = (Map<K, V>) in.readObject(); // (1)
  135.     }

  136.     @Override
  137.     public V remove(final Object key) {
  138.         throw new UnsupportedOperationException("Map is fixed size");
  139.     }

  140.     @Override
  141.     public Collection<V> values() {
  142.         final Collection<V> coll = map.values();
  143.         return UnmodifiableCollection.unmodifiableCollection(coll);
  144.     }

  145.     /**
  146.      * Serializes this object to an ObjectOutputStream.
  147.      *
  148.      * @param out the target ObjectOutputStream.
  149.      * @throws IOException thrown when an I/O errors occur writing to the target stream.
  150.      * @since 3.1
  151.      */
  152.     private void writeObject(final ObjectOutputStream out) throws IOException {
  153.         out.defaultWriteObject();
  154.         out.writeObject(map);
  155.     }

  156. }