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    import java.util.SortedMap;
023    
024    import org.apache.commons.collections.BidiMap;
025    import org.apache.commons.collections.MapIterator;
026    import org.apache.commons.collections.OrderedBidiMap;
027    import org.apache.commons.collections.OrderedMapIterator;
028    import org.apache.commons.collections.SortedBidiMap;
029    import org.apache.commons.collections.Unmodifiable;
030    import org.apache.commons.collections.collection.UnmodifiableCollection;
031    import org.apache.commons.collections.iterators.UnmodifiableOrderedMapIterator;
032    import org.apache.commons.collections.map.UnmodifiableEntrySet;
033    import org.apache.commons.collections.map.UnmodifiableSortedMap;
034    import org.apache.commons.collections.set.UnmodifiableSet;
035    
036    /**
037     * Decorates another <code>SortedBidiMap</code> to ensure it can't be altered.
038     *
039     * @since Commons Collections 3.0
040     * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
041     * 
042     * @author Stephen Colebourne
043     */
044    public final class UnmodifiableSortedBidiMap
045            extends AbstractSortedBidiMapDecorator implements Unmodifiable {
046        
047        /** The inverse unmodifiable map */
048        private UnmodifiableSortedBidiMap inverse;
049    
050        /**
051         * Factory method to create an unmodifiable map.
052         * <p>
053         * If the map passed in is already unmodifiable, it is returned.
054         * 
055         * @param map  the map to decorate, must not be null
056         * @return an unmodifiable SortedBidiMap
057         * @throws IllegalArgumentException if map is null
058         */
059        public static SortedBidiMap decorate(SortedBidiMap map) {
060            if (map instanceof Unmodifiable) {
061                return map;
062            }
063            return new UnmodifiableSortedBidiMap(map);
064        }
065    
066        //-----------------------------------------------------------------------
067        /**
068         * Constructor that wraps (not copies).
069         * 
070         * @param map  the map to decorate, must not be null
071         * @throws IllegalArgumentException if map is null
072         */
073        private UnmodifiableSortedBidiMap(SortedBidiMap map) {
074            super(map);
075        }
076    
077        //-----------------------------------------------------------------------
078        public void clear() {
079            throw new UnsupportedOperationException();
080        }
081    
082        public Object put(Object key, Object value) {
083            throw new UnsupportedOperationException();
084        }
085    
086        public void putAll(Map mapToCopy) {
087            throw new UnsupportedOperationException();
088        }
089    
090        public Object remove(Object key) {
091            throw new UnsupportedOperationException();
092        }
093    
094        public Set entrySet() {
095            Set set = super.entrySet();
096            return UnmodifiableEntrySet.decorate(set);
097        }
098    
099        public Set keySet() {
100            Set set = super.keySet();
101            return UnmodifiableSet.decorate(set);
102        }
103    
104        public Collection values() {
105            Collection coll = super.values();
106            return UnmodifiableCollection.decorate(coll);
107        }
108    
109        //-----------------------------------------------------------------------
110        public Object removeValue(Object value) {
111            throw new UnsupportedOperationException();
112        }
113    
114        public MapIterator mapIterator() {
115            return orderedMapIterator();
116        }
117    
118        public BidiMap inverseBidiMap() {
119            return inverseSortedBidiMap();
120        }
121        
122        //-----------------------------------------------------------------------
123        public OrderedMapIterator orderedMapIterator() {
124            OrderedMapIterator it = getSortedBidiMap().orderedMapIterator();
125            return UnmodifiableOrderedMapIterator.decorate(it);
126        }
127    
128        public OrderedBidiMap inverseOrderedBidiMap() {
129            return inverseSortedBidiMap();
130        }
131    
132        //-----------------------------------------------------------------------
133        public SortedBidiMap inverseSortedBidiMap() {
134            if (inverse == null) {
135                inverse = new UnmodifiableSortedBidiMap(getSortedBidiMap().inverseSortedBidiMap());
136                inverse.inverse = this;
137            }
138            return inverse;
139        }
140    
141        public SortedMap subMap(Object fromKey, Object toKey) {
142            SortedMap sm = getSortedBidiMap().subMap(fromKey, toKey);
143            return UnmodifiableSortedMap.decorate(sm);
144        }
145    
146        public SortedMap headMap(Object toKey) {
147            SortedMap sm = getSortedBidiMap().headMap(toKey);
148            return UnmodifiableSortedMap.decorate(sm);
149        }
150    
151        public SortedMap tailMap(Object fromKey) {
152            SortedMap sm = getSortedBidiMap().tailMap(fromKey);
153            return UnmodifiableSortedMap.decorate(sm);
154        }
155    
156    }