001    /*--------------------------------------------------------------------------+
002    $Id: UnmodifiableHashedListMap.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.util.List;
021    
022    /**
023     * This is a wrapper for a {@link HashedListMap} prohibiting all calls that
024     * would modify its contents. As the construction of this class is performed in
025     * constant time it is prefered over copying the collection (which takes linear
026     * time). All prohibited methods throw an {@link UnsupportedOperationException}.
027     * 
028     * @author Florian Deissenboeck
029     * @author $Author: juergens $
030     * 
031     * @version $Revision: 26283 $
032     * @levd.rating GREEN Hash: DF82643A09598C2B595F267F0B8E66AA
033     */
034    public class UnmodifiableHashedListMap<K, I> extends HashedListMap<K, I> {
035    
036            /** The wrapped list. */
037            private final HashedListMap<K, I> map;
038    
039            /**
040             * Create new unmodifiable hashed list map.
041             * 
042             * @param map
043             *            the map to wrap
044             * @throws IllegalArgumentException
045             *             if map is <code>null</code>
046             */
047            public UnmodifiableHashedListMap(HashedListMap<K, I> map) {
048                    if (map == null) {
049                            throw new IllegalArgumentException(
050                                            "Underlying map may not be null!");
051                    }
052                    this.map = map;
053            }
054    
055            /**
056             * Operation is not supported.
057             * 
058             * @throws UnsupportedOperationException
059             */
060            @Override
061            public List<I> createList(K key) {
062                    throw new UnsupportedOperationException();
063            }
064    
065            /**
066             * {@inheritDoc}
067             */
068            @Override
069            public UnmodifiableList<I> getList(K key) {
070                    List<I> list = map.getList(key);
071                    if (list == null) {
072                            return null;
073                    }
074                    return CollectionUtils.asUnmodifiable(list);
075            }
076    
077            /**
078             * Operation is not supported.
079             * 
080             * @throws UnsupportedOperationException
081             */
082            @Override
083            public void add(K key, I item) {
084                    throw new UnsupportedOperationException();
085            }
086    
087            /**
088             * {@inheritDoc}
089             */
090            @Override
091            public boolean containsList(K key) {
092                    return map.containsList(key);
093            }
094    
095            /**
096             * {@inheritDoc}
097             */
098            @Override
099            public UnmodifiableSet<K> getKeys() {
100                    return CollectionUtils.asUnmodifiable(map.getKeys());
101            }
102    
103            /**
104             * {@inheritDoc}
105             */
106            @Override
107            public UnmodifiableList<I> getValues() {
108                    return CollectionUtils.asUnmodifiable(map.getValues());
109            }
110    
111    }