KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

77     protected FixedSizeSortedMap(SortedMap JavaDoc map) {
78         super(map);
79     }
80
81     /**
82      * Gets the map being decorated.
83      *
84      * @return the decorated map
85      */

86     protected SortedMap JavaDoc getSortedMap() {
87         return (SortedMap JavaDoc) map;
88     }
89
90     //-----------------------------------------------------------------------
91
/**
92      * Write the map out using a custom routine.
93      */

94     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
95         out.defaultWriteObject();
96         out.writeObject(map);
97     }
98
99     /**
100      * Read the map in using a custom routine.
101      */

102     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
103         in.defaultReadObject();
104         map = (Map JavaDoc) in.readObject();
105     }
106
107     //-----------------------------------------------------------------------
108
public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
109         if (map.containsKey(key) == false) {
110             throw new IllegalArgumentException JavaDoc("Cannot put new key/value pair - Map is fixed size");
111         }
112         return map.put(key, value);
113     }
114
115     public void putAll(Map JavaDoc mapToCopy) {
116         for (Iterator JavaDoc it = mapToCopy.keySet().iterator(); it.hasNext(); ) {
117             if (mapToCopy.containsKey(it.next()) == false) {
118                 throw new IllegalArgumentException JavaDoc("Cannot put new key/value pair - Map is fixed size");
119             }
120         }
121         map.putAll(mapToCopy);
122     }
123
124     public void clear() {
125         throw new UnsupportedOperationException JavaDoc("Map is fixed size");
126     }
127
128     public Object JavaDoc remove(Object JavaDoc key) {
129         throw new UnsupportedOperationException JavaDoc("Map is fixed size");
130     }
131
132     public Set JavaDoc entrySet() {
133         Set JavaDoc set = map.entrySet();
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     //-----------------------------------------------------------------------
148
public SortedMap JavaDoc subMap(Object JavaDoc fromKey, Object JavaDoc toKey) {
149         SortedMap JavaDoc map = getSortedMap().subMap(fromKey, toKey);
150         return new FixedSizeSortedMap(map);
151     }
152
153     public SortedMap JavaDoc headMap(Object JavaDoc toKey) {
154         SortedMap JavaDoc map = getSortedMap().headMap(toKey);
155         return new FixedSizeSortedMap(map);
156     }
157
158     public SortedMap JavaDoc tailMap(Object JavaDoc fromKey) {
159         SortedMap JavaDoc map = getSortedMap().tailMap(fromKey);
160         return new FixedSizeSortedMap(map);
161     }
162
163     public boolean isFull() {
164         return true;
165     }
166
167     public int maxSize() {
168         return size();
169     }
170
171 }
172
Popular Tags