KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > util > FastSet


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2006 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.util;
10
11 import java.io.IOException JavaDoc;
12
13 import j2me.io.ObjectInputStream;
14 import j2me.io.ObjectOutputStream;
15 import j2me.util.Collection;
16 import j2me.util.Set;
17 import javolution.lang.Reusable;
18
19 /**
20  * <p> This class represents a set collection backed by a {@link FastMap};
21  * smooth capacity increase and no rehashing ever performed.</p>
22  *
23  * <p> Instances of this class can directly be allocated from the current
24  * thread stack using the {@link #newInstance} factory method
25  * (e.g. for throw-away set to avoid the creation cost).</p>
26  *
27  * <p> {@link FastSet}, as for any {@link FastCollection} sub-class, supports
28  * thread-safe fast iterations without using iterators. For example:[code]
29  * for (FastSet.Record r = set.head(), end = set.tail(); (r = r.getNext()) != end;) {
30  * Object value = set.valueOf(r);
31  * }[/code]</p>
32  *
33  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
34  * @version 4.2, December 18, 2006
35  */

36 public class FastSet/*<E>*/ extends FastCollection/*<E>*/ implements Set/*<E>*/, Reusable {
37
38     /**
39      * Holds the set factory.
40      */

41     private static final Factory FACTORY = new Factory() {
42
43         public Object JavaDoc create() {
44             return new FastSet();
45         }
46
47         public void cleanup(Object JavaDoc obj) {
48             ((FastSet) obj).reset();
49         }
50     };
51
52     /**
53      * Holds the backing map.
54      */

55     private transient FastMap _map;
56
57     /**
58      * Creates a set of small initial capacity.
59      */

60     public FastSet() {
61         this(new FastMap());
62     }
63
64     /**
65      * Creates a persistent set associated to the specified unique identifier
66      * (convenience method).
67      *
68      * @param id the unique identifier for this map.
69      * @throws IllegalArgumentException if the identifier is not unique.
70      * @see javolution.context.PersistentContext.Reference
71      */

72     public FastSet(String JavaDoc id) {
73         this(new FastMap(id));
74     }
75
76     /**
77      * Creates a set of specified initial capacity; unless the set size
78      * reaches the specified capacity, operations on this set will not allocate
79      * memory (no lazy object creation).
80      *
81      * @param capacity the initial capacity.
82      */

83     public FastSet(int capacity) {
84         this(new FastMap(capacity));
85     }
86
87     /**
88      * Creates a set containing the specified elements, in the order they
89      * are returned by the set iterator.
90      *
91      * @param elements the elements to be placed into this fast set.
92      */

93     public FastSet(Set/*<? extends E>*/ elements) {
94         this(new FastMap(elements.size()));
95         addAll(elements);
96     }
97
98     /**
99      * Creates a set implemented using the specified map.
100      *
101      * @param map the backing map.
102      */

103     private FastSet(FastMap map) {
104         _map = map;
105     }
106
107     /**
108      * Returns a new, preallocated or {@link #recycle recycled} set instance
109      * (on the stack when executing in a {@link javolution.context.PoolContext
110      * PoolContext}).
111      *
112      * @return a new, preallocated or recycled set instance.
113      */

114     public static /*<E>*/ FastSet/*<E>*/ newInstance() {
115         return (FastSet/*<E>*/) FACTORY.object();
116     }
117
118     /**
119      * Recycles a set {@link #newInstance() instance} immediately
120      * (on the stack when executing in a {@link javolution.context.PoolContext
121      * PoolContext}).
122      */

123     public static void recycle(FastSet instance) {
124         FACTORY.recycle(instance);
125     }
126
127     /**
128      * Returns the number of elements in this set (its cardinality).
129      *
130      * @return the number of elements in this set (its cardinality).
131      */

132     public final int size() {
133         return _map.size();
134     }
135
136     /**
137      * Adds the specified value to this set if it is not already present.
138      *
139      * @param value the value to be added to this set.
140      * @return <code>true</code> if this set did not already contain the
141      * specified element.
142      * @throws NullPointerException if the value is <code>null</code>.
143      */

144     public final boolean add(Object JavaDoc/*{E}*/ value) {
145         return _map.put(value, value) == null;
146     }
147
148     // Overrides to return a set (JDK1.5+).
149
public Collection/*Set<E>*/unmodifiable() {
150         return (Collection/*Set<E>*/) super.unmodifiable();
151     }
152
153     // Overrides (optimization).
154
public final void clear() {
155         _map.clear();
156     }
157
158     // Overrides (optimization).
159
public final boolean contains(Object JavaDoc o) {
160         return _map.containsKey(o);
161     }
162
163     // Overrides (optimization).
164
public final boolean remove(Object JavaDoc o) {
165         return _map.remove(o) != null;
166     }
167
168     /**
169      * Sets the comparator to use for value equality.
170      *
171      * @param comparator the value comparator.
172      * @return <code>this</code>
173      */

174     public FastSet/*<E>*/ setValueComparator(FastComparator/*<? super E>*/ comparator) {
175         _map.setKeyComparator(comparator);
176         return this;
177     }
178     
179     // Overrides.
180
public FastComparator/*<? super E>*/ getValueComparator() {
181         return _map.getKeyComparator();
182     }
183
184     // Implements Reusable.
185
public void reset() {
186         super.reset();
187         _map.reset();
188     }
189
190     // Requires special handling during de-serialization process.
191
private void readObject(ObjectInputStream stream) throws IOException JavaDoc,
192             ClassNotFoundException JavaDoc {
193         FastComparator cmp = (FastComparator) stream.readObject();
194         final int size = stream.readInt();
195         _map = new FastMap(size);
196         this.setValueComparator(cmp);
197         for (int i = size; i-- != 0;) {
198             Object JavaDoc key = stream.readObject();
199             _map.put(key, key);
200         }
201     }
202
203     // Requires special handling during serialization process.
204
private void writeObject(ObjectOutputStream stream) throws IOException JavaDoc {
205         stream.writeObject(getValueComparator());
206         stream.writeInt(size());
207         for (FastMap.Entry e = _map.head(), end = _map.tail();
208               (e = (FastMap.Entry) e.getNext()) != end;) {
209             stream.writeObject(e.getKey());
210         }
211     }
212
213     // Implements FastCollection abstract method.
214
public final Record head() {
215         return _map.head();
216     }
217
218     // Implements FastCollection abstract method.
219
public final Record tail() {
220         return _map.tail();
221     }
222
223     // Implements FastCollection abstract method.
224
public final Object JavaDoc/*{E}*/ valueOf(Record record) {
225         return (Object JavaDoc/*{E}*/) ((FastMap.Entry) record).getKey();
226     }
227
228     // Implements FastCollection abstract method.
229
public final void delete(Record record) {
230         _map.remove(((FastMap.Entry) record).getKey());
231     }
232
233     private static final long serialVersionUID = 1L;
234 }
Popular Tags