FixedSizeSortedMap.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 java.util.SortedMap;

  26. import org.apache.commons.collections4.BoundedMap;
  27. import org.apache.commons.collections4.CollectionUtils;
  28. import org.apache.commons.collections4.collection.UnmodifiableCollection;
  29. import org.apache.commons.collections4.set.UnmodifiableSet;

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

  62.     /** Serialization version */
  63.     private static final long serialVersionUID = 3126019624511683653L;

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

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

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

  90.     @Override
  91.     public Set<Map.Entry<K, V>> entrySet() {
  92.         return UnmodifiableSet.unmodifiableSet(map.entrySet());
  93.     }

  94.     /**
  95.      * Gets the map being decorated.
  96.      *
  97.      * @return the decorated map
  98.      */
  99.     protected SortedMap<K, V> getSortedMap() {
  100.         return (SortedMap<K, V>) map;
  101.     }

  102.     @Override
  103.     public SortedMap<K, V> headMap(final K toKey) {
  104.         return new FixedSizeSortedMap<>(getSortedMap().headMap(toKey));
  105.     }

  106.     @Override
  107.     public boolean isFull() {
  108.         return true;
  109.     }

  110.     @Override
  111.     public Set<K> keySet() {
  112.         return UnmodifiableSet.unmodifiableSet(map.keySet());
  113.     }

  114.     @Override
  115.     public int maxSize() {
  116.         return size();
  117.     }

  118.     @Override
  119.     public V put(final K key, final V value) {
  120.         if (!map.containsKey(key)) {
  121.             throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
  122.         }
  123.         return map.put(key, value);
  124.     }

  125.     @Override
  126.     public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
  127.         if (CollectionUtils.isSubCollection(mapToCopy.keySet(), keySet())) {
  128.             throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
  129.         }
  130.         map.putAll(mapToCopy);
  131.     }

  132.     /**
  133.      * Deserializes the map in using a custom routine.
  134.      *
  135.      * @param in the input stream
  136.      * @throws IOException if an error occurs while reading from the stream
  137.      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
  138.      */
  139.     @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect
  140.     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
  141.         in.defaultReadObject();
  142.         map = (Map<K, V>) in.readObject(); // (1)
  143.     }

  144.     @Override
  145.     public V remove(final Object key) {
  146.         throw new UnsupportedOperationException("Map is fixed size");
  147.     }

  148.     @Override
  149.     public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
  150.         return new FixedSizeSortedMap<>(getSortedMap().subMap(fromKey, toKey));
  151.     }

  152.     @Override
  153.     public SortedMap<K, V> tailMap(final K fromKey) {
  154.         return new FixedSizeSortedMap<>(getSortedMap().tailMap(fromKey));
  155.     }

  156.     @Override
  157.     public Collection<V> values() {
  158.         return UnmodifiableCollection.unmodifiableCollection(map.values());
  159.     }

  160.     /**
  161.      * Writes the map out using a custom routine.
  162.      *
  163.      * @param out  the output stream
  164.      * @throws IOException if an error occurs while writing to the stream
  165.      */
  166.     private void writeObject(final ObjectOutputStream out) throws IOException {
  167.         out.defaultWriteObject();
  168.         out.writeObject(map);
  169.     }

  170. }