1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved. |
3 |
> |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
< |
* Copyright 2006 Sun Microsystems, Inc. All rights reserved. |
6 |
< |
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
5 |
> |
* This code is free software; you can redistribute it and/or modify it |
6 |
> |
* under the terms of the GNU General Public License version 2 only, as |
7 |
> |
* published by the Free Software Foundation. Sun designates this |
8 |
> |
* particular file as subject to the "Classpath" exception as provided |
9 |
> |
* by Sun in the LICENSE file that accompanied this code. |
10 |
> |
* |
11 |
> |
* This code is distributed in the hope that it will be useful, but WITHOUT |
12 |
> |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 |
> |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 |
> |
* version 2 for more details (a copy is included in the LICENSE file that |
15 |
> |
* accompanied this code). |
16 |
> |
* |
17 |
> |
* You should have received a copy of the GNU General Public License version |
18 |
> |
* 2 along with this work; if not, write to the Free Software Foundation, |
19 |
> |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
> |
* |
21 |
> |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
22 |
> |
* CA 95054 USA or visit www.sun.com if you need additional information or |
23 |
> |
* have any questions. |
24 |
|
*/ |
25 |
|
|
26 |
|
package java.util; |
81 |
|
* |
82 |
|
* @author Josh Bloch |
83 |
|
* @version %I%, %G% |
84 |
< |
* @see Collection |
85 |
< |
* @see Set |
86 |
< |
* @see HashSet |
84 |
> |
* @see Collection |
85 |
> |
* @see Set |
86 |
> |
* @see HashSet |
87 |
|
* @see Comparable |
88 |
|
* @see Comparator |
89 |
< |
* @see TreeMap |
89 |
> |
* @see TreeMap |
90 |
|
* @since 1.2 |
91 |
|
*/ |
92 |
|
|
122 |
|
* {@code ClassCastException}. |
123 |
|
*/ |
124 |
|
public TreeSet() { |
125 |
< |
this(new TreeMap<E,Object>()); |
125 |
> |
this(new TreeMap<E,Object>()); |
126 |
|
} |
127 |
|
|
128 |
|
/** |
139 |
|
* ordering} of the elements will be used. |
140 |
|
*/ |
141 |
|
public TreeSet(Comparator<? super E> comparator) { |
142 |
< |
this(new TreeMap<E,Object>(comparator)); |
142 |
> |
this(new TreeMap<E,Object>(comparator)); |
143 |
|
} |
144 |
|
|
145 |
|
/** |
170 |
|
*/ |
171 |
|
public TreeSet(SortedSet<E> s) { |
172 |
|
this(s.comparator()); |
173 |
< |
addAll(s); |
173 |
> |
addAll(s); |
174 |
|
} |
175 |
|
|
176 |
|
/** |
189 |
|
* @since 1.6 |
190 |
|
*/ |
191 |
|
public Iterator<E> descendingIterator() { |
192 |
< |
return m.descendingKeySet().iterator(); |
192 |
> |
return m.descendingKeySet().iterator(); |
193 |
|
} |
194 |
|
|
195 |
|
/** |
196 |
|
* @since 1.6 |
197 |
|
*/ |
198 |
|
public NavigableSet<E> descendingSet() { |
199 |
< |
return new TreeSet(m.descendingMap()); |
199 |
> |
return new TreeSet(m.descendingMap()); |
200 |
|
} |
201 |
|
|
202 |
|
/** |
205 |
|
* @return the number of elements in this set (its cardinality) |
206 |
|
*/ |
207 |
|
public int size() { |
208 |
< |
return m.size(); |
208 |
> |
return m.size(); |
209 |
|
} |
210 |
|
|
211 |
|
/** |
214 |
|
* @return {@code true} if this set contains no elements |
215 |
|
*/ |
216 |
|
public boolean isEmpty() { |
217 |
< |
return m.isEmpty(); |
217 |
> |
return m.isEmpty(); |
218 |
|
} |
219 |
|
|
220 |
|
/** |
232 |
|
* does not permit null elements |
233 |
|
*/ |
234 |
|
public boolean contains(Object o) { |
235 |
< |
return m.containsKey(o); |
235 |
> |
return m.containsKey(o); |
236 |
|
} |
237 |
|
|
238 |
|
/** |
253 |
|
* does not permit null elements |
254 |
|
*/ |
255 |
|
public boolean add(E e) { |
256 |
< |
return m.put(e, PRESENT)==null; |
256 |
> |
return m.put(e, PRESENT)==null; |
257 |
|
} |
258 |
|
|
259 |
|
/** |
274 |
|
* does not permit null elements |
275 |
|
*/ |
276 |
|
public boolean remove(Object o) { |
277 |
< |
return m.remove(o)==PRESENT; |
277 |
> |
return m.remove(o)==PRESENT; |
278 |
|
} |
279 |
|
|
280 |
|
/** |
282 |
|
* The set will be empty after this call returns. |
283 |
|
*/ |
284 |
|
public void clear() { |
285 |
< |
m.clear(); |
285 |
> |
m.clear(); |
286 |
|
} |
287 |
|
|
288 |
|
/** |
299 |
|
public boolean addAll(Collection<? extends E> c) { |
300 |
|
// Use linear-time version if applicable |
301 |
|
if (m.size()==0 && c.size() > 0 && |
302 |
< |
c instanceof SortedSet && |
302 |
> |
c instanceof SortedSet && |
303 |
|
m instanceof TreeMap) { |
304 |
|
SortedSet<? extends E> set = (SortedSet<? extends E>) c; |
305 |
|
TreeMap<E,Object> map = (TreeMap<E, Object>) m; |
323 |
|
*/ |
324 |
|
public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, |
325 |
|
E toElement, boolean toInclusive) { |
326 |
< |
return new TreeSet<E>(m.subMap(fromElement, fromInclusive, |
326 |
> |
return new TreeSet<E>(m.subMap(fromElement, fromInclusive, |
327 |
|
toElement, toInclusive)); |
328 |
|
} |
329 |
|
|
336 |
|
* @since 1.6 |
337 |
|
*/ |
338 |
|
public NavigableSet<E> headSet(E toElement, boolean inclusive) { |
339 |
< |
return new TreeSet<E>(m.headMap(toElement, inclusive)); |
339 |
> |
return new TreeSet<E>(m.headMap(toElement, inclusive)); |
340 |
|
} |
341 |
|
|
342 |
|
/** |
348 |
|
* @since 1.6 |
349 |
|
*/ |
350 |
|
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) { |
351 |
< |
return new TreeSet<E>(m.tailMap(fromElement, inclusive)); |
351 |
> |
return new TreeSet<E>(m.tailMap(fromElement, inclusive)); |
352 |
|
} |
353 |
|
|
354 |
|
/** |
359 |
|
* @throws IllegalArgumentException {@inheritDoc} |
360 |
|
*/ |
361 |
|
public SortedSet<E> subSet(E fromElement, E toElement) { |
362 |
< |
return subSet(fromElement, true, toElement, false); |
362 |
> |
return subSet(fromElement, true, toElement, false); |
363 |
|
} |
364 |
|
|
365 |
|
/** |
370 |
|
* @throws IllegalArgumentException {@inheritDoc} |
371 |
|
*/ |
372 |
|
public SortedSet<E> headSet(E toElement) { |
373 |
< |
return headSet(toElement, false); |
373 |
> |
return headSet(toElement, false); |
374 |
|
} |
375 |
|
|
376 |
|
/** |
381 |
|
* @throws IllegalArgumentException {@inheritDoc} |
382 |
|
*/ |
383 |
|
public SortedSet<E> tailSet(E fromElement) { |
384 |
< |
return tailSet(fromElement, true); |
384 |
> |
return tailSet(fromElement, true); |
385 |
|
} |
386 |
|
|
387 |
|
public Comparator<? super E> comparator() { |
472 |
|
*/ |
473 |
|
public Object clone() { |
474 |
|
TreeSet<E> clone = null; |
475 |
< |
try { |
476 |
< |
clone = (TreeSet<E>) super.clone(); |
477 |
< |
} catch (CloneNotSupportedException e) { |
478 |
< |
throw new InternalError(); |
479 |
< |
} |
475 |
> |
try { |
476 |
> |
clone = (TreeSet<E>) super.clone(); |
477 |
> |
} catch (CloneNotSupportedException e) { |
478 |
> |
throw new InternalError(); |
479 |
> |
} |
480 |
|
|
481 |
|
clone.m = new TreeMap<E,Object>(m); |
482 |
|
return clone; |
496 |
|
*/ |
497 |
|
private void writeObject(java.io.ObjectOutputStream s) |
498 |
|
throws java.io.IOException { |
499 |
< |
// Write out any hidden stuff |
500 |
< |
s.defaultWriteObject(); |
499 |
> |
// Write out any hidden stuff |
500 |
> |
s.defaultWriteObject(); |
501 |
|
|
502 |
|
// Write out Comparator |
503 |
|
s.writeObject(m.comparator()); |
505 |
|
// Write out size |
506 |
|
s.writeInt(m.size()); |
507 |
|
|
508 |
< |
// Write out all elements in the proper order. |
509 |
< |
for (Iterator i=m.keySet().iterator(); i.hasNext(); ) |
508 |
> |
// Write out all elements in the proper order. |
509 |
> |
for (Iterator i=m.keySet().iterator(); i.hasNext(); ) |
510 |
|
s.writeObject(i.next()); |
511 |
|
} |
512 |
|
|
516 |
|
*/ |
517 |
|
private void readObject(java.io.ObjectInputStream s) |
518 |
|
throws java.io.IOException, ClassNotFoundException { |
519 |
< |
// Read in any hidden stuff |
520 |
< |
s.defaultReadObject(); |
519 |
> |
// Read in any hidden stuff |
520 |
> |
s.defaultReadObject(); |
521 |
|
|
522 |
|
// Read in Comparator |
523 |
|
Comparator<? super E> c = (Comparator<? super E>) s.readObject(); |
524 |
|
|
525 |
|
// Create backing TreeMap |
526 |
< |
TreeMap<E,Object> tm; |
527 |
< |
if (c==null) |
528 |
< |
tm = new TreeMap<E,Object>(); |
529 |
< |
else |
530 |
< |
tm = new TreeMap<E,Object>(c); |
531 |
< |
m = tm; |
526 |
> |
TreeMap<E,Object> tm; |
527 |
> |
if (c==null) |
528 |
> |
tm = new TreeMap<E,Object>(); |
529 |
> |
else |
530 |
> |
tm = new TreeMap<E,Object>(c); |
531 |
> |
m = tm; |
532 |
|
|
533 |
|
// Read in size |
534 |
|
int size = s.readInt(); |