001    /*--------------------------------------------------------------------------+
002    $Id: UnmodifiableMap.java 26283 2010-02-18 11:18:57Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.collections;
019    
020    import java.io.Serializable;
021    import java.util.Collections;
022    import java.util.Map;
023    
024    /**
025     * This is a wrapper for a {@link Map} prohibiting all calls which would modify
026     * its contents. As the construction of this class is performed in constant time
027     * it is prefered over copying the map (which takes linear time). Using this
028     * class is also preferred to using the <code>unmodifiableX()</code> in class
029     * {@link Collections} as they return the collection base type that does not
030     * signal, that the object ist unmodifiable. Using the classes in this package
031     * makes unmodifiability more explicit.
032     * <p>
033     * All prohibited methods throw an {@link UnsupportedOperationException}. The
034     * class is nearly the same as the one returned by
035     * {@link Collections#unmodifiableMap(Map)}, but by making it a public class we
036     * can make the return value of some methods more explicit.
037     * <p>
038     * This map is serializable if the wrapped map is serializable.
039     * 
040     * @author Benjamin Hummel
041     * @author $Author: juergens $
042     * 
043     * @version $Revision: 26283 $
044     * @levd.rating GREEN Hash: 4BC4212A490EEF9A14859C79C22B8A4D
045     */
046    public class UnmodifiableMap<K, V> implements Map<K, V>, Serializable {
047    
048            /** The underlying map. */
049            private final Map<K, V> m;
050    
051            /**
052             * Creates a new unmodifiable map from another map. All modifications to the
053             * underlying map will directly be visible in this wrapper.
054             */
055            public UnmodifiableMap(Map<K, V> m) {
056                    if (m == null) {
057                            throw new IllegalArgumentException(
058                                            "Underlying map may not be null!");
059                    }
060                    this.m = m;
061            }
062    
063            /** {@inheritDoc} */
064            public boolean isEmpty() {
065                    return m.isEmpty();
066            }
067    
068            /** {@inheritDoc} */
069            public int size() {
070                    return m.size();
071            }
072    
073            /** {@inheritDoc} */
074            public boolean containsKey(Object key) {
075                    return m.containsKey(key);
076            }
077    
078            /** {@inheritDoc} */
079            public boolean containsValue(Object value) {
080                    return m.containsValue(value);
081            }
082    
083            /** {@inheritDoc} */
084            public V get(Object key) {
085                    return m.get(key);
086            }
087    
088            /** {@inheritDoc} */
089            public UnmodifiableSet<K> keySet() {
090                    return new UnmodifiableSet<K>(m.keySet());
091            }
092    
093            /** {@inheritDoc} */
094            public UnmodifiableCollection<V> values() {
095                    return new UnmodifiableCollection<V>(m.values());
096            }
097    
098            /** {@inheritDoc} */
099            public UnmodifiableSet<java.util.Map.Entry<K, V>> entrySet() {
100                    return new UnmodifiableSet<java.util.Map.Entry<K, V>>(m.entrySet());
101            }
102    
103            /**
104             * Operation is not supported.
105             * 
106             * @throws UnsupportedOperationException
107             */
108            public void clear() {
109                    throw new UnsupportedOperationException();
110            }
111    
112            /**
113             * Operation is not supported.
114             * 
115             * @throws UnsupportedOperationException
116             */
117            public V put(K arg0, V arg1) {
118                    throw new UnsupportedOperationException();
119            }
120    
121            /**
122             * Operation is not supported.
123             * 
124             * @throws UnsupportedOperationException
125             */
126            public void putAll(Map<? extends K, ? extends V> arg0) {
127                    throw new UnsupportedOperationException();
128            }
129    
130            /**
131             * Operation is not supported.
132             * 
133             * @throws UnsupportedOperationException
134             */
135            public V remove(Object arg0) {
136                    throw new UnsupportedOperationException();
137            }
138    
139            /**
140             * Returns a string representation of this map.
141             */
142            @Override
143            public String toString() {
144                    return m.toString();
145            }
146    }