KickJava   Java API By Example, From Geeks To Geeks.

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


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// This file is
5
// Copyright (C) 2002-@year@ Leo Mekenkamp. All rights reserved.
6
// $Id: NodeTreeSetImpl.java,v 1.6 2003/11/20 23:18:41 per_nyfelt Exp $
7

8 package org.ozoneDB.collections;
9
10 import java.util.Collection JavaDoc;
11 import java.util.Comparator JavaDoc;
12 import java.util.SortedMap JavaDoc;
13 import java.util.SortedSet JavaDoc;
14
15 /**
16  * <p>Note that calling <code>iterator()</code> does NOT result in the creation
17  * of an ozone object; this is opposite to behaviour of <code>FullTreeSetImpl</code>.</p>
18  *
19  * @author <a HREF="mailto:ozoneATmekenkampD0Tcom">Leo Mekenkamp (mind the anti-sp@m)</a>
20  */

21 public class NodeTreeSetImpl extends BaseTreeSetImpl implements NodeTreeSet {
22
23     private static final long serialVersionUID = 1L;
24
25     /**
26      * Construct a new TreeSet whose backing TreeMap using the "natural"
27      * ordering of keys. Elements that are not mutually comparable will cause
28      * ClassCastExceptions down the road.
29      *
30      * @see Comparable
31      */

32     public NodeTreeSetImpl() {
33     }
34
35     /**
36      * Construct a new TreeSet whose backing TreeMap uses the supplied
37      * Comparator. Elements that are not mutually comparable will cause
38      * ClassCastExceptions down the road.
39      *
40      * @param comparator the Comparator this Set will use
41      */

42     public NodeTreeSetImpl(Comparator JavaDoc comparator) {
43         super(comparator);
44     }
45
46     /**
47      * Construct a new TreeSet whose backing TreeMap uses the "natural"
48      * orering of the keys and which contains all of the elements in the
49      * supplied Collection. This runs in n*log(n) time.
50      *
51      * @param collection the new Set will be initialized with all
52      * of the elements in this Collection
53      * @throws ClassCastException if the elements of the collection are not
54      * comparable
55      * @throws NullPointerException if the collection is null
56      * @see Comparable
57      */

58     public NodeTreeSetImpl(Collection JavaDoc collection) {
59         super(collection);
60     }
61
62     /**
63      * Construct a new TreeSet, using the same key ordering as the supplied
64      * SortedSet and containing all of the elements in the supplied SortedSet.
65      * This constructor runs in linear time.
66      *
67      * @param sortedSet the new TreeSet will use this SortedSet's comparator
68      * and will initialize itself with all its elements
69      * @throws NullPointerException if sortedSet is null
70      */

71     public NodeTreeSetImpl(SortedSet JavaDoc sortedSet) {
72         super(sortedSet);
73     }
74
75     /**
76      * This constructor is used to implement the subSet() calls around
77      * a backing TreeMap.SubMap.
78      *
79      * @param backingMap the submap
80      */

81     NodeTreeSetImpl(SortedMap JavaDoc backingMap) {
82         map = backingMap;
83     }
84
85     /**
86      * Returns a shallow copy of this Set. The elements are not cloned.
87      *
88      * @return the cloned set
89      */

90     public Object JavaDoc clone() {
91
92         BaseTreeSet copy = null;
93         try {
94 // TODO: replace when FakeFactoryGenerator is ready
95
// copy = NodeTreeSetImplFactory.getDefault().create(self());
96
copy = (BaseTreeSet) database().createObject(
97                     NodeTreeSetImpl.class,
98                     new Class JavaDoc[] {NodeTreeSetImpl.class},
99                     new Object JavaDoc[] {self()});
100         }
101         catch (Exception JavaDoc e) {
102             throw new RuntimeException JavaDoc(e);
103         }
104         return copy;
105     }
106
107     /**
108      * Returns a view of this Set including all elements less than
109      * <code>to</code>. The returned set is backed by the original, so changes
110      * in one appear in the other. The subset will throw an
111      * {@link IllegalArgumentException} for any attempt to access or add an
112      * element beyond the specified cutoff. The returned set does not include
113      * the endpoint; if you want inclusion, pass the successor element.
114      *
115      * @param to the (exclusive) cutoff point
116      * @return a view of the set less than the cutoff
117      * @throws ClassCastException if <code>to</code> is not compatible with
118      * the comparator (or is not Comparable, for natural ordering)
119      * @throws NullPointerException if to is null, but the comparator does not
120      * tolerate null elements
121      */

122     public SortedSet JavaDoc headSet(Object JavaDoc to) {
123 // TODO: replace when FakeFactoryGenerator is ready
124
// return NodeTreeSetImplFactory.getDefault().create(map.headMap(to));
125
SortedMap JavaDoc headMap = map.headMap(to);
126         return (NodeTreeSet) database().createObject(
127                 NodeTreeSetImpl.class,
128                 new Class JavaDoc[] {SortedMap JavaDoc.class},
129                 new Object JavaDoc[] {headMap}
130         );
131     }
132
133     /**
134      * Returns a view of this Set including all elements greater or equal to
135      * <code>from</code> and less than <code>to</code> (a half-open interval).
136      * The returned set is backed by the original, so changes in one appear in
137      * the other. The subset will throw an {@link IllegalArgumentException}
138      * for any attempt to access or add an element beyond the specified cutoffs.
139      * The returned set includes the low endpoint but not the high; if you want
140      * to reverse this behavior on either end, pass in the successor element.
141      *
142      * @param from the (inclusive) low cutoff point
143      * @param to the (exclusive) high cutoff point
144      * @return a view of the set between the cutoffs
145      * @throws ClassCastException if either cutoff is not compatible with
146      * the comparator (or is not Comparable, for natural ordering)
147      * @throws NullPointerException if from or to is null, but the comparator
148      * does not tolerate null elements
149      * @throws IllegalArgumentException if from is greater than to
150      */

151     public SortedSet JavaDoc subSet(Object JavaDoc from, Object JavaDoc to) {
152 // TODO: replace when FakeFactoryGenerator is ready
153
// return NodeTreeSetImplFactory.getDefault().create(map.subMap(from, to));
154
SortedMap JavaDoc subMap = map.subMap(from, to);
155         return (NodeTreeSet) database().createObject(
156                 NodeTreeSetImpl.class,
157                 new Class JavaDoc[] {SortedMap JavaDoc.class},
158                 new Object JavaDoc[] {subMap}
159         );
160     }
161
162     /**
163      * Returns a view of this Set including all elements greater or equal to
164      * <code>from</code>. The returned set is backed by the original, so
165      * changes in one appear in the other. The subset will throw an
166      * {@link IllegalArgumentException} for any attempt to access or add an
167      * element beyond the specified cutoff. The returned set includes the
168      * endpoint; if you want to exclude it, pass in the successor element.
169      *
170      * @param from the (inclusive) low cutoff point
171      * @return a view of the set above the cutoff
172      * @throws ClassCastException if <code>from</code> is not compatible with
173      * the comparator (or is not Comparable, for natural ordering)
174      * @throws NullPointerException if from is null, but the comparator
175      * does not tolerate null elements
176      */

177     public SortedSet JavaDoc tailSet(Object JavaDoc from) {
178 // TODO: replace when FakeFactoryGenerator is ready
179
// return NodeTreeSetImplFactory.getDefault().create(map.tailMap(from));
180
SortedMap JavaDoc tailMap = map.tailMap(from);
181         return (NodeTreeSet) database().createObject(
182                 NodeTreeSetImpl.class,
183                 new Class JavaDoc[] {SortedMap JavaDoc.class},
184                 new Object JavaDoc[] {tailMap}
185         );
186     }
187
188     protected SortedMap JavaDoc newBackingMap() {
189 // TODO: replace when FakeFactoryGenerator is ready
190
// map = NodeTreeMapImplFactory.getDefault.create();
191
return (NodeTreeMap) database().createObject(NodeTreeMapImpl.class.getName());
192     }
193
194     protected SortedMap JavaDoc newBackingMap(Comparator JavaDoc comparator) {
195 // TODO: replace when FakeFactoryGenerator is ready
196
// return NodeTreeMapImplFactory.getDefault.create(comparator);
197
return (NodeTreeMap) database().createObject(NodeTreeMapImpl.class.getName(),
198                 Comparator JavaDoc.class.getName(), new Object JavaDoc[] {comparator});
199     }
200
201 }
Popular Tags