KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > trove > decorator > TLongHashSetDecorator


1 ///////////////////////////////////////////////////////////////////////////////
2
// Copyright (c) 2002, Eric D. Friedman All Rights Reserved.
3
//
4
// This library is free software; you can redistribute it and/or
5
// modify it under the terms of the GNU Lesser General Public
6
// License as published by the Free Software Foundation; either
7
// version 2.1 of the License, or (at your option) any later version.
8
//
9
// This library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU Lesser General Public
15
// License along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
///////////////////////////////////////////////////////////////////////////////
18

19 package gnu.trove.decorator;
20
21 import gnu.trove.TLongHashSet;
22 import gnu.trove.TLongIterator;
23 import java.util.AbstractSet;
24 import java.util.Collection;
25 import java.util.Iterator;
26 import java.util.Set;
27
28 /**
29  * Wrapper class to make a TLongHashSet conform to the <tt>java.util.Set</tt> API.
30  * This class simply decorates an underlying TLongHashSet and translates the Object-based
31  * APIs into their Trove primitive analogs.
32  *
33  * <p>
34  * Note that wrapping and unwrapping primitive values is extremely inefficient. If
35  * possible, users of this class should override the appropriate methods in this class
36  * and use a table of canonical values.
37  * </p>
38  *
39  * Created: Tue Sep 24 22:08:17 PDT 2002
40  *
41  * @author Eric D. Friedman
42  * @version $Id: TLongHashSetDecorator.java,v 1.5 2004/11/09 15:48:50 ericdf Exp $
43  * @since trove 0.1.8
44  */

45 public class TLongHashSetDecorator extends AbstractSet implements Set, Cloneable {
46     /** the wrapped primitive set */
47     protected TLongHashSet _set;
48
49     /**
50      * Creates a wrapper that decorates the specified primitive set.
51      */

52     public TLongHashSetDecorator(TLongHashSet set) {
53     super();
54     this._set = set;
55     }
56
57     /**
58      * Clones the underlying trove collection and returns the clone wrapped in a new
59      * decorator instance. This is a shallow clone except where primitives are
60      * concerned.
61      *
62      * @return a copy of the receiver
63      */

64     public Object clone() {
65         try {
66             TLongHashSetDecorator copy = (TLongHashSetDecorator) super.clone();
67             copy._set = (TLongHashSet) _set.clone();
68             return copy;
69         } catch (CloneNotSupportedException e) {
70             // assert(false);
71
throw new InternalError(); // we are cloneable
72
}
73     }
74
75     /**
76      * Inserts a value into the set.
77      *
78      * @param true if the set was modified by the insertion
79      */

80     public boolean add(Object value) {
81     return _set.add(unwrap(value));
82     }
83
84     /**
85      * Compares this set with another set for equality of their stored
86      * entries.
87      *
88      * @param other an <code>Object</code> value
89      * @return true if the sets are identical
90      */

91     public boolean equals(Object other) {
92     if (_set.equals(other)) {
93         return true; // comparing two trove sets
94
} else if (other instanceof Set) {
95         Set that = (Set)other;
96         if (that.size() != _set.size()) {
97         return false; // different sizes, no need to compare
98
} else { // now we have to do it the hard way
99
Iterator it = that.iterator();
100         for (int i = that.size(); i-- > 0;) {
101             Object val = it.next();
102             if (val instanceof Long) {
103             long v = unwrap(val);
104             if (_set.contains(v)) {
105                 // match, ok to continue
106
} else {
107                 return false; // no match: we're done
108
}
109             } else {
110             return false; // different type in other set
111
}
112         }
113         return true; // all entries match
114
}
115     } else {
116         return false;
117     }
118     }
119
120     /**
121      * Empties the set.
122      */

123     public void clear() {
124     this._set.clear();
125     }
126
127     /**
128      * Deletes a value from the set.
129      *
130      * @param value an <code>Object</code> value
131      * @return true if the set was modified
132      */

133     public boolean remove(Object value) {
134     return _set.remove(unwrap(value));
135     }
136
137     /**
138      * Creates an iterator over the values of the set.
139      *
140      * @return an iterator with support for removals in the underlying set
141      */

142     public Iterator iterator() {
143     return new Iterator() {
144         private final TLongIterator it = _set.iterator();
145                 
146         public Object next() {
147             return wrap(it.next());
148         }
149
150         public boolean hasNext() {
151             return it.hasNext();
152         }
153
154         public void remove() {
155             it.remove();
156         }
157         };
158     }
159
160     /**
161      * Returns the number of entries in the set.
162      * @return the set's size.
163      */

164     public int size() {
165     return this._set.size();
166     }
167
168     /**
169      * Indicates whether set has any entries.
170      * @return true if the set is empty
171      */

172     public boolean isEmpty() {
173     return (size() == 0);
174     }
175
176     /**
177      * Wraps a value
178      *
179      * @param a value in the underlying set
180      * @return an Object representation of the value
181      */

182     protected Long wrap(long k) {
183     return new Long(k);
184     }
185
186     /**
187      * Unwraps a value
188      *
189      * @param a wrapped value
190      * @return an unwrapped representation of the value
191      */

192     protected long unwrap(Object value) {
193     return ((Long)value).longValue();
194     }
195 } // TLongHashSetDecorator
196
Popular Tags