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.map;
017:
018: import java.io.IOException;
019: import java.io.ObjectInputStream;
020: import java.io.ObjectOutputStream;
021: import java.io.Serializable;
022: import java.util.Collection;
023: import java.util.Comparator;
024: import java.util.Map;
025: import java.util.Set;
026: import java.util.SortedMap;
027:
028: import org.apache.commons.collections.Unmodifiable;
029: import org.apache.commons.collections.collection.UnmodifiableCollection;
030: import org.apache.commons.collections.set.UnmodifiableSet;
031:
032: /**
033: * Decorates another <code>SortedMap</code> to ensure it can't be altered.
034: * <p>
035: * This class is Serializable from Commons Collections 3.1.
036: *
037: * @since Commons Collections 3.0
038: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
039: *
040: * @author Stephen Colebourne
041: */
042: public final class UnmodifiableSortedMap extends
043: AbstractSortedMapDecorator implements Unmodifiable,
044: Serializable {
045:
046: /** Serialization version */
047: private static final long serialVersionUID = 5805344239827376360L;
048:
049: /**
050: * Factory method to create an unmodifiable sorted map.
051: *
052: * @param map the map to decorate, must not be null
053: * @throws IllegalArgumentException if map is null
054: */
055: public static SortedMap decorate(SortedMap map) {
056: if (map instanceof Unmodifiable) {
057: return map;
058: }
059: return new UnmodifiableSortedMap(map);
060: }
061:
062: //-----------------------------------------------------------------------
063: /**
064: * Constructor that wraps (not copies).
065: *
066: * @param map the map to decorate, must not be null
067: * @throws IllegalArgumentException if map is null
068: */
069: private UnmodifiableSortedMap(SortedMap map) {
070: super (map);
071: }
072:
073: //-----------------------------------------------------------------------
074: /**
075: * Write the map out using a custom routine.
076: *
077: * @param out the output stream
078: * @throws IOException
079: * @since Commons Collections 3.1
080: */
081: private void writeObject(ObjectOutputStream out) throws IOException {
082: out.defaultWriteObject();
083: out.writeObject(map);
084: }
085:
086: /**
087: * Read the map in using a custom routine.
088: *
089: * @param in the input stream
090: * @throws IOException
091: * @throws ClassNotFoundException
092: * @since Commons Collections 3.1
093: */
094: private void readObject(ObjectInputStream in) throws IOException,
095: ClassNotFoundException {
096: in.defaultReadObject();
097: map = (Map) in.readObject();
098: }
099:
100: //-----------------------------------------------------------------------
101: public void clear() {
102: throw new UnsupportedOperationException();
103: }
104:
105: public Object put(Object key, Object value) {
106: throw new UnsupportedOperationException();
107: }
108:
109: public void putAll(Map mapToCopy) {
110: throw new UnsupportedOperationException();
111: }
112:
113: public Object remove(Object key) {
114: throw new UnsupportedOperationException();
115: }
116:
117: public Set entrySet() {
118: Set set = super .entrySet();
119: return UnmodifiableEntrySet.decorate(set);
120: }
121:
122: public Set keySet() {
123: Set set = super .keySet();
124: return UnmodifiableSet.decorate(set);
125: }
126:
127: public Collection values() {
128: Collection coll = super .values();
129: return UnmodifiableCollection.decorate(coll);
130: }
131:
132: //-----------------------------------------------------------------------
133: public Object firstKey() {
134: return getSortedMap().firstKey();
135: }
136:
137: public Object lastKey() {
138: return getSortedMap().lastKey();
139: }
140:
141: public Comparator comparator() {
142: return getSortedMap().comparator();
143: }
144:
145: public SortedMap subMap(Object fromKey, Object toKey) {
146: SortedMap map = getSortedMap().subMap(fromKey, toKey);
147: return new UnmodifiableSortedMap(map);
148: }
149:
150: public SortedMap headMap(Object toKey) {
151: SortedMap map = getSortedMap().headMap(toKey);
152: return new UnmodifiableSortedMap(map);
153: }
154:
155: public SortedMap tailMap(Object fromKey) {
156: SortedMap map = getSortedMap().tailMap(fromKey);
157: return new UnmodifiableSortedMap(map);
158: }
159:
160: }
|