001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.collections.map;
018
019 import java.io.IOException;
020 import java.io.ObjectInputStream;
021 import java.io.ObjectOutputStream;
022 import java.io.Serializable;
023 import java.util.Collection;
024 import java.util.Map;
025 import java.util.Set;
026
027 import org.apache.commons.collections.MapIterator;
028 import org.apache.commons.collections.OrderedMap;
029 import org.apache.commons.collections.OrderedMapIterator;
030 import org.apache.commons.collections.Unmodifiable;
031 import org.apache.commons.collections.collection.UnmodifiableCollection;
032 import org.apache.commons.collections.iterators.UnmodifiableMapIterator;
033 import org.apache.commons.collections.iterators.UnmodifiableOrderedMapIterator;
034 import org.apache.commons.collections.set.UnmodifiableSet;
035
036 /**
037 * Decorates another <code>OrderedMap</code> to ensure it can't be altered.
038 * <p>
039 * This class is Serializable from Commons Collections 3.1.
040 *
041 * @since Commons Collections 3.0
042 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
043 *
044 * @author Stephen Colebourne
045 */
046 public final class UnmodifiableOrderedMap
047 extends AbstractOrderedMapDecorator
048 implements Unmodifiable, Serializable {
049
050 /** Serialization version */
051 private static final long serialVersionUID = 8136428161720526266L;
052
053 /**
054 * Factory method to create an unmodifiable sorted map.
055 *
056 * @param map the map to decorate, must not be null
057 * @throws IllegalArgumentException if map is null
058 */
059 public static OrderedMap decorate(OrderedMap map) {
060 if (map instanceof Unmodifiable) {
061 return map;
062 }
063 return new UnmodifiableOrderedMap(map);
064 }
065
066 //-----------------------------------------------------------------------
067 /**
068 * Constructor that wraps (not copies).
069 *
070 * @param map the map to decorate, must not be null
071 * @throws IllegalArgumentException if map is null
072 */
073 private UnmodifiableOrderedMap(OrderedMap map) {
074 super(map);
075 }
076
077 //-----------------------------------------------------------------------
078 /**
079 * Write the map out using a custom routine.
080 *
081 * @param out the output stream
082 * @throws IOException
083 * @since Commons Collections 3.1
084 */
085 private void writeObject(ObjectOutputStream out) throws IOException {
086 out.defaultWriteObject();
087 out.writeObject(map);
088 }
089
090 /**
091 * Read the map in using a custom routine.
092 *
093 * @param in the input stream
094 * @throws IOException
095 * @throws ClassNotFoundException
096 * @since Commons Collections 3.1
097 */
098 private void readObject(ObjectInputStream in) throws IOException, 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 }