KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import org.apache.commons.collections.BoundedMap;
28 import org.apache.commons.collections.collection.UnmodifiableCollection;
29 import org.apache.commons.collections.set.UnmodifiableSet;
30
31 /**
32  * Decorates another <code>Map</code> to fix the size, preventing add/remove.
33  * <p>
34  * Any action that would change the size of the map is disallowed.
35  * The put method is allowed to change the value associated with an existing
36  * key however.
37  * <p>
38  * If trying to remove or clear the map, an UnsupportedOperationException is
39  * thrown. If trying to put a new mapping into the map, an
40  * IllegalArgumentException is thrown. This is because the put method can
41  * succeed if the mapping's key already exists in the map, so the put method
42  * is not always unsupported.
43  * <p>
44  * This class is Serializable from Commons Collections 3.1.
45  *
46  * @since Commons Collections 3.0
47  * @version $Revision: 1.8 $ $Date: 2004/05/07 23:58:33 $
48  *
49  * @author Stephen Colebourne
50  * @author Paul Jack
51  */

52 public class FixedSizeMap
53         extends AbstractMapDecorator
54         implements Map JavaDoc, BoundedMap, Serializable JavaDoc {
55
56     /** Serialization version */
57     private static final long serialVersionUID = 7450927208116179316L;
58
59     /**
60      * Factory method to create a fixed size map.
61      *
62      * @param map the map to decorate, must not be null
63      * @throws IllegalArgumentException if map is null
64      */

65     public static Map JavaDoc decorate(Map JavaDoc map) {
66         return new FixedSizeMap(map);
67     }
68
69     //-----------------------------------------------------------------------
70
/**
71      * Constructor that wraps (not copies).
72      *
73      * @param map the map to decorate, must not be null
74      * @throws IllegalArgumentException if map is null
75      */

76     protected FixedSizeMap(Map JavaDoc map) {
77         super(map);
78     }
79
80     //-----------------------------------------------------------------------
81
/**
82      * Write the map out using a custom routine.
83      *
84      * @param out the output stream
85      * @throws IOException
86      * @since Commons Collections 3.1
87      */

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

101     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
102         in.defaultReadObject();
103         map = (Map JavaDoc) in.readObject();
104     }
105
106     //-----------------------------------------------------------------------
107
public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
108         if (map.containsKey(key) == false) {
109             throw new IllegalArgumentException JavaDoc("Cannot put new key/value pair - Map is fixed size");
110         }
111         return map.put(key, value);
112     }
113
114     public void putAll(Map JavaDoc mapToCopy) {
115         for (Iterator JavaDoc it = mapToCopy.keySet().iterator(); it.hasNext(); ) {
116             if (mapToCopy.containsKey(it.next()) == false) {
117                 throw new IllegalArgumentException JavaDoc("Cannot put new key/value pair - Map is fixed size");
118             }
119         }
120         map.putAll(mapToCopy);
121     }
122
123     public void clear() {
124         throw new UnsupportedOperationException JavaDoc("Map is fixed size");
125     }
126
127     public Object JavaDoc remove(Object JavaDoc key) {
128         throw new UnsupportedOperationException JavaDoc("Map is fixed size");
129     }
130
131     public Set JavaDoc entrySet() {
132         Set JavaDoc set = map.entrySet();
133         // unmodifiable set will still allow modification via Map.Entry objects
134
return UnmodifiableSet.decorate(set);
135     }
136
137     public Set JavaDoc keySet() {
138         Set JavaDoc set = map.keySet();
139         return UnmodifiableSet.decorate(set);
140     }
141
142     public Collection JavaDoc values() {
143         Collection JavaDoc coll = map.values();
144         return UnmodifiableCollection.decorate(coll);
145     }
146
147     public boolean isFull() {
148         return true;
149     }
150
151     public int maxSize() {
152         return size();
153     }
154    
155 }
156
Popular Tags