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