KickJava   Java API By Example, From Geeks To Geeks.

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


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: FullTreeSetImpl.java,v 1.10 2003/11/27 15:55:11 leomekenkamp 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> results in the creation of
17  * an ozone object and thus in a write-action for the db.</p>
18  *
19  * @author <a HREF="mailto:ozoneATmekenkampD0Tcom">Leo Mekenkamp (mind the anti-sp@m)</a>
20  */

21 public class FullTreeSetImpl extends BaseTreeSetImpl implements FullTreeSet {
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 FullTreeSetImpl() {
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 FullTreeSetImpl(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 FullTreeSetImpl(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 FullTreeSetImpl(SortedSet JavaDoc sortedSet) {
72         super(sortedSet);
73     }
74
75     /**
76      * <P>DO NOT USE THIS CONSTRUCTOR YOURSELF, NOR USE EQUIVALENT CREATE
77      * METHOD FROM FACTORY.</p>
78      * <p>This constructor is used to implement the subSet() calls around
79      * a backing TreeMap.SubMap.</p>
80      *
81      * @param backingMap the submap
82      */

83     public FullTreeSetImpl(SortedMap JavaDoc backingMap, DoNotUse_SeeJavadoc x) {
84         super(0);
85         map = backingMap;
86     }
87
88     /**
89      * Returns a shallow copy of this Set. The elements are not cloned.
90      *
91      * @return the cloned set
92      */

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

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

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