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     */
017    package org.apache.commons.collections.bidimap;
018    
019    import java.util.Collection;
020    import java.util.Map;
021    import java.util.Set;
022    
023    import org.apache.commons.collections.BidiMap;
024    import org.apache.commons.collections.MapIterator;
025    import org.apache.commons.collections.Unmodifiable;
026    import org.apache.commons.collections.collection.UnmodifiableCollection;
027    import org.apache.commons.collections.iterators.UnmodifiableMapIterator;
028    import org.apache.commons.collections.map.UnmodifiableEntrySet;
029    import org.apache.commons.collections.set.UnmodifiableSet;
030    
031    /**
032     * Decorates another <code>BidiMap</code> to ensure it can't be altered.
033     *
034     * @since Commons Collections 3.0
035     * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
036     * 
037     * @author Stephen Colebourne
038     */
039    public final class UnmodifiableBidiMap
040            extends AbstractBidiMapDecorator implements Unmodifiable {
041        
042        /** The inverse unmodifiable map */
043        private UnmodifiableBidiMap inverse;
044    
045        /**
046         * Factory method to create an unmodifiable map.
047         * <p>
048         * If the map passed in is already unmodifiable, it is returned.
049         * 
050         * @param map  the map to decorate, must not be null
051         * @return an unmodifiable BidiMap
052         * @throws IllegalArgumentException if map is null
053         */
054        public static BidiMap decorate(BidiMap map) {
055            if (map instanceof Unmodifiable) {
056                return map;
057            }
058            return new UnmodifiableBidiMap(map);
059        }
060    
061        //-----------------------------------------------------------------------
062        /**
063         * Constructor that wraps (not copies).
064         * 
065         * @param map  the map to decorate, must not be null
066         * @throws IllegalArgumentException if map is null
067         */
068        private UnmodifiableBidiMap(BidiMap map) {
069            super(map);
070        }
071    
072        //-----------------------------------------------------------------------
073        public void clear() {
074            throw new UnsupportedOperationException();
075        }
076    
077        public Object put(Object key, Object value) {
078            throw new UnsupportedOperationException();
079        }
080    
081        public void putAll(Map mapToCopy) {
082            throw new UnsupportedOperationException();
083        }
084    
085        public Object remove(Object key) {
086            throw new UnsupportedOperationException();
087        }
088    
089        public Set entrySet() {
090            Set set = super.entrySet();
091            return UnmodifiableEntrySet.decorate(set);
092        }
093    
094        public Set keySet() {
095            Set set = super.keySet();
096            return UnmodifiableSet.decorate(set);
097        }
098    
099        public Collection values() {
100            Collection coll = super.values();
101            return UnmodifiableCollection.decorate(coll);
102        }
103    
104        //-----------------------------------------------------------------------
105        public Object removeValue(Object value) {
106            throw new UnsupportedOperationException();
107        }
108    
109        public MapIterator mapIterator() {
110            MapIterator it = getBidiMap().mapIterator();
111            return UnmodifiableMapIterator.decorate(it);
112        }
113    
114        public BidiMap inverseBidiMap() {
115            if (inverse == null) {
116                inverse = new UnmodifiableBidiMap(getBidiMap().inverseBidiMap());
117                inverse.inverse = this;
118            }
119            return inverse;
120        }
121        
122    }