KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > map > UnmodifiableMap


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.map;
17
18 import java.io.IOException JavaDoc;
19 import java.io.ObjectInputStream JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.apache.commons.collections.IterableMap;
27 import org.apache.commons.collections.MapIterator;
28 import org.apache.commons.collections.Unmodifiable;
29 import org.apache.commons.collections.collection.UnmodifiableCollection;
30 import org.apache.commons.collections.iterators.EntrySetMapIterator;
31 import org.apache.commons.collections.iterators.UnmodifiableMapIterator;
32 import org.apache.commons.collections.set.UnmodifiableSet;
33
34 /**
35  * Decorates another <code>Map</code> to ensure it can't be altered.
36  * <p>
37  * This class is Serializable from Commons Collections 3.1.
38  *
39  * @since Commons Collections 3.0
40  * @version $Revision: 1.10 $ $Date: 2004/04/09 10:32:25 $
41  *
42  * @author Stephen Colebourne
43  */

44 public final class UnmodifiableMap
45         extends AbstractMapDecorator
46         implements IterableMap, Unmodifiable, Serializable JavaDoc {
47
48     /** Serialization version */
49     private static final long serialVersionUID = 2737023427269031941L;
50
51     /**
52      * Factory method to create an unmodifiable map.
53      *
54      * @param map the map to decorate, must not be null
55      * @throws IllegalArgumentException if map is null
56      */

57     public static Map JavaDoc decorate(Map JavaDoc map) {
58         if (map instanceof Unmodifiable) {
59             return map;
60         }
61         return new UnmodifiableMap(map);
62     }
63
64     //-----------------------------------------------------------------------
65
/**
66      * Constructor that wraps (not copies).
67      *
68      * @param map the map to decorate, must not be null
69      * @throws IllegalArgumentException if map is null
70      */

71     private UnmodifiableMap(Map JavaDoc map) {
72         super(map);
73     }
74
75     //-----------------------------------------------------------------------
76
/**
77      * Write the map out using a custom routine.
78      *
79      * @param out the output stream
80      * @throws IOException
81      * @since Commons Collections 3.1
82      */

83     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
84         out.defaultWriteObject();
85         out.writeObject(map);
86     }
87
88     /**
89      * Read the map in using a custom routine.
90      *
91      * @param in the input stream
92      * @throws IOException
93      * @throws ClassNotFoundException
94      * @since Commons Collections 3.1
95      */

96     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
97         in.defaultReadObject();
98         map = (Map JavaDoc) in.readObject();
99     }
100
101     //-----------------------------------------------------------------------
102
public void clear() {
103         throw new UnsupportedOperationException JavaDoc();
104     }
105
106     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
107         throw new UnsupportedOperationException JavaDoc();
108     }
109
110     public void putAll(Map JavaDoc mapToCopy) {
111         throw new UnsupportedOperationException JavaDoc();
112     }
113
114     public Object JavaDoc remove(Object JavaDoc key) {
115         throw new UnsupportedOperationException JavaDoc();
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 JavaDoc entrySet() {
129         Set JavaDoc set = super.entrySet();
130         return UnmodifiableEntrySet.decorate(set);
131     }
132
133     public Set JavaDoc keySet() {
134         Set JavaDoc set = super.keySet();
135         return UnmodifiableSet.decorate(set);
136     }
137
138     public Collection JavaDoc values() {
139         Collection JavaDoc coll = super.values();
140         return UnmodifiableCollection.decorate(coll);
141     }
142
143 }
144
Popular Tags