KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > collections > _BaseTreeMap_SubMapImpl


1 /*
2  * _BaseTreeSet_SubMap.java
3  * $Id: _BaseTreeMap_SubMapImpl.java,v 1.5 2003/11/20 23:18:41 per_nyfelt Exp $
4  * This file is based on TreeMap.java from GNU Classpath. Quote:
5
6 TreeMap.java -- a class providing a basic Red-Black Tree data structure,
7 mapping Object --> Object
8 Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
9
10 This file is part of GNU Classpath.
11
12 GNU Classpath is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2, or (at your option)
15 any later version.
16
17 GNU Classpath is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GNU Classpath; see the file COPYING. If not, write to the
24 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 02111-1307 USA.
26
27 Linking this library statically or dynamically with other modules is
28 making a combined work based on this library. Thus, the terms and
29 conditions of the GNU General Public License cover the whole
30 combination.
31
32 As a special exception, the copyright holders of this library give you
33 permission to link this library with independent modules to produce an
34 executable, regardless of the license terms of these independent
35 modules, and to copy and distribute the resulting executable under
36 terms of your choice, provided that you also meet, for each linked
37 independent module, the terms and conditions of the license of that
38 module. An independent module is a module which is not derived from
39 or based on this library. If you modify this library, you may extend
40 this exception to your version of the library, but you are not
41 obligated to do so. If you do not wish to do so, delete this
42 exception statement from your version.
43
44  * end quote.
45  *
46  * This file is licenced under the same conditions as its original (GPL +
47  * "special exception").
48  */

49
50 package org.ozoneDB.collections;
51
52 import java.util.Collection JavaDoc;
53 import java.util.Comparator JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.Map JavaDoc;
56 import java.util.NoSuchElementException JavaDoc;
57 import java.util.Set JavaDoc;
58 import java.util.SortedMap JavaDoc;
59 import java.util.TreeMap JavaDoc;
60
61 /**
62  * <p>Do not use this class directly.
63  * This should be an inner class; unfortunately ozone does not support those yet.</p>
64  * <p>Implementation of {@link #subMap(Object, Object)} and other map
65  * ranges. This class provides a view of a portion of the original backing
66  * map, and throws {@link IllegalArgumentException} for attempts to
67  * access beyond that range.</p>
68  *
69  * @author Eric Blake <ebb9@email.byu.edu>
70  * @author <a HREF="mailto:ozoneATmekenkampD0Tcom">Leo Mekenkamp (mind the anti-sp@m)</a> (adaptation for ozone)
71  */

72 public class _BaseTreeMap_SubMapImpl extends AbstractOzoneMap implements _BaseTreeMap_SubMap {
73
74     private static final long serialVersionUID = 1L;
75
76     /**
77      * underlying BaseTreeMap.
78      */

79     protected BaseTreeMap owner;
80
81     /**
82      * The lower range of this view, inclusive, or nil for unbounded.
83      * Package visible for use by nested classes.
84      */

85     final Object JavaDoc minKey;
86
87     /**
88      * The upper range of this view, exclusive, or nil for unbounded.
89      * Package visible for use by nested classes.
90      */

91     final Object JavaDoc maxKey;
92
93     /**
94      * The cache for {@link #entrySet()}.
95      */

96     private OzoneSet entries;
97
98     /**
99      * Create a SubMap representing the elements between minKey (inclusive)
100      * and maxKey (exclusive). If minKey is nil, SubMap has no lower bound
101      * (headMap). If maxKey is nil, the SubMap has no upper bound (tailMap).
102      *
103      * @param minKey the lower bound
104      * @param maxKey the upper bound
105      * @throws IllegalArgumentException if minKey &gt; maxKey
106      */

107     public _BaseTreeMap_SubMapImpl(BaseTreeMap owner, Object JavaDoc minKey, Object JavaDoc maxKey) {
108         this.owner = owner;
109         if ((minKey instanceof BaseTreeMap.Node) && (maxKey instanceof BaseTreeMap.Node) &&
110                 !((BaseTreeMap.Node) minKey).isNil() && !((BaseTreeMap.Node) maxKey).isNil() &&
111                 owner._org_ozoneDB_compare(minKey, maxKey) > 0)
112             throw new IllegalArgumentException JavaDoc("fromKey > toKey");
113         this.minKey = minKey;
114         this.maxKey = maxKey;
115     }
116
117     /**
118      * Check if "key" is in within the range bounds for this SubMap. The
119      * lower ("from") SubMap range is inclusive, and the upper ("to") bound
120      * is exclusive. Package visible for use by nested classes.
121      *
122      * @param key the key to check
123      * @return true if the key is in range
124      */

125     public final boolean keyInRange(Object JavaDoc key) {
126         return (((minKey instanceof BaseTreeMap.Node && ((BaseTreeMap.Node) minKey).isNil()) || owner._org_ozoneDB_compare(key, minKey) >= 0)
127         && ((maxKey instanceof BaseTreeMap.Node && ((BaseTreeMap.Node) maxKey).isNil()) || owner._org_ozoneDB_compare(key, maxKey) < 0));
128     }
129
130     public void clear() {
131         BaseTreeMap.Node next = owner._org_ozoneDB_lowestGreaterThan(minKey, true);
132         BaseTreeMap.Node max = owner._org_ozoneDB_lowestGreaterThan(maxKey, false);
133         while (!next.equals(max)) {
134             BaseTreeMap.Node current = next;
135             next = owner._org_ozoneDB_successor(current);
136             owner._org_ozoneDB_removeNode(current);
137         }
138     }
139
140     public Comparator JavaDoc comparator() {
141         return owner.comparator();
142     }
143
144     public boolean containsKey(Object JavaDoc key) {
145         return keyInRange(key) && owner.containsKey(key);
146     }
147
148     public boolean containsValue(Object JavaDoc value) {
149         BaseTreeMap.Node node = owner._org_ozoneDB_lowestGreaterThan(minKey, true);
150         BaseTreeMap.Node max = owner._org_ozoneDB_lowestGreaterThan(maxKey, false);
151         while (node != max) {
152             if (equals(value, node.getValue()))
153                 return true;
154             node = owner._org_ozoneDB_successor(node);
155         }
156         return false;
157     }
158
159     public Set JavaDoc entrySet() {
160         if (entries == null) {
161 // TODO: when FakeFactoryGenerator is finished replace with
162
// entries = BaseTreeMap_SubMap_entrySetFactory.getDefault.create(self());
163
entries = (OzoneSet) database().createObject(
164                     _BaseTreeMap_SubMap_entrySet.class,
165                     new Class JavaDoc[] {_BaseTreeMap_SubMap.class},
166                     new Object JavaDoc[] {self()});
167         }
168         return entries;
169     }
170
171     public Object JavaDoc firstKey() {
172         BaseTreeMap.Node node = owner._org_ozoneDB_lowestGreaterThan(minKey, true);
173         if (node.isNil() || ! keyInRange(node.getKey()))
174             throw new NoSuchElementException JavaDoc();
175         return node.getKey();
176     }
177
178     public Object JavaDoc get(Object JavaDoc key) {
179         if (keyInRange(key))
180             return owner.get(key);
181         return null;
182     }
183
184     public SortedMap JavaDoc headMap(Object JavaDoc toKey) {
185         if (! keyInRange(toKey))
186             throw new IllegalArgumentException JavaDoc("key outside range");
187 // TODO: replace when FakeFactoryGenerator is ready
188
// BaseTreeMapImpl_SubMapFactory.getDefault.create(owner, minKey, toKey);
189
return (SortedMap JavaDoc) database().createObject(
190                 _BaseTreeMap_SubMap.class,
191                 new Class JavaDoc[] {BaseTreeMap.class, Object JavaDoc.class, Object JavaDoc.class},
192                 new Object JavaDoc[] {owner, minKey, toKey}
193         );
194     }
195
196     public Set JavaDoc keySet() {
197         if (keys == null) {
198 // TODO: replace when FakeFactoryGenerator is ready
199
// BaseTreeMapImpl_SubMapFactory.getDefault.create(owner, minKey, toKey);
200
keys = (Set JavaDoc) database().createObject(
201                     _BaseTreeMap_SubMap_keySet.class,
202                     new Class JavaDoc[] {_BaseTreeMap_SubMap.class},
203                     new Object JavaDoc[] {self()}
204             );
205         }
206         return keys;
207     }
208
209     public Object JavaDoc lastKey() {
210         BaseTreeMap.Node node = owner._org_ozoneDB_highestLessThan(maxKey);
211         if (node.isNil() || ! keyInRange(node.getKey()))
212             throw new NoSuchElementException JavaDoc();
213         return node.getKey();
214     }
215
216     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
217         if (! keyInRange(key))
218             throw new IllegalArgumentException JavaDoc("Key outside range");
219         return owner.put(key, value);
220     }
221
222     public Object JavaDoc remove(Object JavaDoc key) {
223         if (keyInRange(key))
224             return owner.remove(key);
225         return null;
226     }
227
228     public int size() {
229         BaseTreeMap.Node node = owner._org_ozoneDB_lowestGreaterThan(minKey, true);
230         BaseTreeMap.Node max = owner._org_ozoneDB_lowestGreaterThan(maxKey, false);
231         int count = 0;
232         while (!node.equals(max)) {
233             count++;
234             node = owner._org_ozoneDB_successor(node);
235         }
236         return count;
237     }
238
239     public SortedMap JavaDoc subMap(Object JavaDoc fromKey, Object JavaDoc toKey) {
240         if (! keyInRange(fromKey) || ! keyInRange(toKey))
241             throw new IllegalArgumentException JavaDoc("key outside range");
242 // TODO: replace when FakeFactoryGenerator is ready
243
// BaseTreeMapImpl_SubMapFactory.getDefault.create(owner, fromKey, toKey);
244
return (SortedMap JavaDoc) database().createObject(
245                 _BaseTreeMap_SubMap.class,
246                 new Class JavaDoc[] {BaseTreeMap.class, Object JavaDoc.class, Object JavaDoc.class},
247                 new Object JavaDoc[] {owner, fromKey, toKey}
248         );
249     }
250
251     public SortedMap JavaDoc tailMap(Object JavaDoc fromKey) {
252         if (! keyInRange(fromKey))
253             throw new IllegalArgumentException JavaDoc("key outside range");
254 // TODO: replace when FakeFactoryGenerator is ready
255
// BaseTreeMapImpl_SubMapFactory.getDefault.create(owner, fromKey, maxKey);
256
return (SortedMap JavaDoc) database().createObject(
257                 _BaseTreeMap_SubMap.class,
258                 new Class JavaDoc[] {BaseTreeMap.class, Object JavaDoc.class, Object JavaDoc.class},
259                 new Object JavaDoc[] {owner, fromKey, maxKey}
260         );
261     }
262
263     public Collection JavaDoc values() {
264         if (values == null) {
265 // TODO: replace when FakeFactoryGenerator is ready
266
// BaseTreeMapImpl_SubMapFactory.getDefault.create(owner, minKey, toKey);
267
values = (OzoneCollection) database().createObject(
268                     _BaseTreeMap_SubMap_values.class,
269                     new Class JavaDoc[] {_BaseTreeMap_SubMap.class},
270                     new Object JavaDoc[] {self()}
271             );
272         }
273         return values;
274     }
275
276     /** <p>Returns a <code>Map</code> that contains the same entries as this
277      * persistent one; it is (by nature of the client-server enviromnent) always
278      * a 'deep' copy of this <code>OzoneMap</code>. I.e. the contents of
279      * this <code>OzoneMap</code> instance are always copied to the client
280      * by use of serialization.</p>
281      *
282      */

283     public Map JavaDoc getClientMap() {
284         return getClientSortedMap();
285     }
286
287     /** <p>Returns a <code>SortedMap</code> that contains the same entries as this
288      * persistent one; it is (by nature of the client-server enviromnent) always
289      * a 'deep' copy of this <code>OzoneSortedMap</code>. I.e. the contents of
290      * this <code>OzoneSortedMap</code> instance are always copied to the client
291      * by use of serialization.</p>
292      *
293      */

294     public SortedMap JavaDoc getClientSortedMap() {
295         TreeMap JavaDoc result = comparator() == null ? new TreeMap JavaDoc() : new TreeMap JavaDoc(comparator());
296         for (Iterator JavaDoc i = ((OzoneSet) entrySet())._org_ozoneDB_internalIterator(); i.hasNext(); ) {
297             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
298             result.put(entry.getKey(), entry.getValue());
299         }
300         return result;
301     }
302
303     public Object JavaDoc getMinKey() {
304         return minKey;
305     }
306
307     public Object JavaDoc getMaxKey() {
308         return maxKey;
309     }
310
311     public BaseTreeMap getOwner() {
312         return owner;
313     }
314
315     /** <p>Basically nothing more than a typecasted <code>HeadMap</code> method.
316      * Because subsets are also <code>OzoneSortedMap</code>s, this method is
317      * provided to do away with the need for a typecast.</p>
318      *
319      */

320     public OzoneSortedMap ozoneHeadMap(Object JavaDoc toKey) {
321         return (OzoneSortedMap) headMap(toKey);
322     }
323
324     /** <p>Basically nothing more than a typecasted <code>SubMap</code> method.
325      * Because subsets are also <code>OzoneSortedMap</code>s, this method is
326      * provided to do away with the need for a typecast.</p>
327      *
328      */

329     public OzoneSortedMap ozoneSubMap(Object JavaDoc fromKey, Object JavaDoc toKey) {
330         return (OzoneSortedMap) subMap(fromKey, toKey);
331     }
332
333     /** <p>Basically nothing more than a typecasted <code>TailMap</code> method.</p>
334      * Because subsets are also <code>OzoneSortedMap</code>s, this method is
335      * provided to do away with the need for a typecast.</p>
336      *
337      */

338     public OzoneSortedMap ozoneTailMap(Object JavaDoc toKey) {
339         return (OzoneSortedMap) tailMap(toKey);
340     }
341
342 }
Popular Tags