001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.map;
018
019import java.io.IOException;
020import java.io.ObjectInputStream;
021import java.io.ObjectOutputStream;
022import java.io.Serializable;
023import java.util.Collection;
024import java.util.Map;
025import java.util.Set;
026
027import org.apache.commons.collections4.set.UnmodifiableSet;
028import org.apache.commons.collections4.BoundedMap;
029import org.apache.commons.collections4.collection.UnmodifiableCollection;
030
031/**
032 * Decorates another <code>Map</code> to fix the size, preventing add/remove.
033 * <p>
034 * Any action that would change the size of the map is disallowed.
035 * The put method is allowed to change the value associated with an existing
036 * key however.
037 * <p>
038 * If trying to remove or clear the map, an UnsupportedOperationException is
039 * thrown. If trying to put a new mapping into the map, an
040 * IllegalArgumentException is thrown. This is because the put method can
041 * succeed if the mapping's key already exists in the map, so the put method
042 * is not always unsupported.
043 * <p>
044 * <strong>Note that FixedSizeMap is not synchronized and is not thread-safe.</strong>
045 * If you wish to use this map from multiple threads concurrently, you must use
046 * appropriate synchronization. The simplest approach is to wrap this map
047 * using {@link java.util.Collections#synchronizedMap(Map)}. This class may throw
048 * exceptions when accessed by concurrent threads without synchronization.
049 * <p>
050 * This class is Serializable from Commons Collections 3.1.
051 *
052 * @since 3.0
053 * @version $Id: FixedSizeMap.html 887892 2013-11-24 13:43:45Z tn $
054 */
055public class FixedSizeMap<K, V>
056        extends AbstractMapDecorator<K, V>
057        implements BoundedMap<K, V>, Serializable {
058
059    /** Serialization version */
060    private static final long serialVersionUID = 7450927208116179316L;
061
062    /**
063     * Factory method to create a fixed size map.
064     *
065     * @param <K>  the key type
066     * @param <V>  the value type
067     * @param map  the map to decorate, must not be null
068     * @return a new fixed size map
069     * @throws IllegalArgumentException if map is null
070     * @since 4.0
071     */
072    public static <K, V> FixedSizeMap<K, V> fixedSizeMap(final Map<K, V> map) {
073        return new FixedSizeMap<K, V>(map);
074    }
075
076    //-----------------------------------------------------------------------
077    /**
078     * Constructor that wraps (not copies).
079     *
080     * @param map  the map to decorate, must not be null
081     * @throws IllegalArgumentException if map is null
082     */
083    protected FixedSizeMap(final Map<K, V> map) {
084        super(map);
085    }
086
087    //-----------------------------------------------------------------------
088    /**
089     * Write the map out using a custom routine.
090     *
091     * @param out  the output stream
092     * @throws IOException
093     * @since 3.1
094     */
095    private void writeObject(final ObjectOutputStream out) throws IOException {
096        out.defaultWriteObject();
097        out.writeObject(map);
098    }
099
100    /**
101     * Read the map in using a custom routine.
102     *
103     * @param in  the input stream
104     * @throws IOException
105     * @throws ClassNotFoundException
106     * @since 3.1
107     */
108    @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect
109    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
110        in.defaultReadObject();
111        map = (Map<K, V>) in.readObject(); // (1)
112    }
113
114    //-----------------------------------------------------------------------
115    @Override
116    public V put(final K key, final V value) {
117        if (map.containsKey(key) == false) {
118            throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
119        }
120        return map.put(key, value);
121    }
122
123    @Override
124    public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
125        for (final K key : mapToCopy.keySet()) {
126            if (!containsKey(key)) {
127                throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
128            }
129        }
130        map.putAll(mapToCopy);
131    }
132
133    @Override
134    public void clear() {
135        throw new UnsupportedOperationException("Map is fixed size");
136    }
137
138    @Override
139    public V remove(final Object key) {
140        throw new UnsupportedOperationException("Map is fixed size");
141    }
142
143    @Override
144    public Set<Map.Entry<K, V>> entrySet() {
145        final Set<Map.Entry<K, V>> set = map.entrySet();
146        // unmodifiable set will still allow modification via Map.Entry objects
147        return UnmodifiableSet.unmodifiableSet(set);
148    }
149
150    @Override
151    public Set<K> keySet() {
152        final Set<K> set = map.keySet();
153        return UnmodifiableSet.unmodifiableSet(set);
154    }
155
156    @Override
157    public Collection<V> values() {
158        final Collection<V> coll = map.values();
159        return UnmodifiableCollection.unmodifiableCollection(coll);
160    }
161
162    public boolean isFull() {
163        return true;
164    }
165
166    public int maxSize() {
167        return size();
168    }
169
170}