001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.bidimap;
017:
018: import java.util.Collection;
019: import java.util.Map;
020: import java.util.Set;
021:
022: import org.apache.commons.collections.BidiMap;
023: import org.apache.commons.collections.MapIterator;
024: import org.apache.commons.collections.Unmodifiable;
025: import org.apache.commons.collections.collection.UnmodifiableCollection;
026: import org.apache.commons.collections.iterators.UnmodifiableMapIterator;
027: import org.apache.commons.collections.map.UnmodifiableEntrySet;
028: import org.apache.commons.collections.set.UnmodifiableSet;
029:
030: /**
031: * Decorates another <code>BidiMap</code> to ensure it can't be altered.
032: *
033: * @since Commons Collections 3.0
034: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
035: *
036: * @author Stephen Colebourne
037: */
038: public final class UnmodifiableBidiMap extends AbstractBidiMapDecorator
039: implements Unmodifiable {
040:
041: /** The inverse unmodifiable map */
042: private UnmodifiableBidiMap inverse;
043:
044: /**
045: * Factory method to create an unmodifiable map.
046: * <p>
047: * If the map passed in is already unmodifiable, it is returned.
048: *
049: * @param map the map to decorate, must not be null
050: * @return an unmodifiable BidiMap
051: * @throws IllegalArgumentException if map is null
052: */
053: public static BidiMap decorate(BidiMap map) {
054: if (map instanceof Unmodifiable) {
055: return map;
056: }
057: return new UnmodifiableBidiMap(map);
058: }
059:
060: //-----------------------------------------------------------------------
061: /**
062: * Constructor that wraps (not copies).
063: *
064: * @param map the map to decorate, must not be null
065: * @throws IllegalArgumentException if map is null
066: */
067: private UnmodifiableBidiMap(BidiMap map) {
068: super (map);
069: }
070:
071: //-----------------------------------------------------------------------
072: public void clear() {
073: throw new UnsupportedOperationException();
074: }
075:
076: public Object put(Object key, Object value) {
077: throw new UnsupportedOperationException();
078: }
079:
080: public void putAll(Map mapToCopy) {
081: throw new UnsupportedOperationException();
082: }
083:
084: public Object remove(Object key) {
085: throw new UnsupportedOperationException();
086: }
087:
088: public Set entrySet() {
089: Set set = super .entrySet();
090: return UnmodifiableEntrySet.decorate(set);
091: }
092:
093: public Set keySet() {
094: Set set = super .keySet();
095: return UnmodifiableSet.decorate(set);
096: }
097:
098: public Collection values() {
099: Collection coll = super .values();
100: return UnmodifiableCollection.decorate(coll);
101: }
102:
103: //-----------------------------------------------------------------------
104: public Object removeValue(Object value) {
105: throw new UnsupportedOperationException();
106: }
107:
108: public MapIterator mapIterator() {
109: MapIterator it = getBidiMap().mapIterator();
110: return UnmodifiableMapIterator.decorate(it);
111: }
112:
113: public BidiMap inverseBidiMap() {
114: if (inverse == null) {
115: inverse = new UnmodifiableBidiMap(getBidiMap()
116: .inverseBidiMap());
117: inverse.inverse = this;
118: }
119: return inverse;
120: }
121:
122: }
|