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