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