1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright (c) 1998, 2008, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
> |
* or visit www.oracle.com if you need additional information or have any |
23 |
> |
* questions. |
24 |
|
*/ |
25 |
|
|
26 |
|
package java.util; |
74 |
|
* should be used only to detect bugs.</i> |
75 |
|
* |
76 |
|
* <p>This class is a member of the |
77 |
< |
* <a href="{@docRoot}/../guide/collections/index.html"> |
77 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
78 |
|
* Java Collections Framework</a>. |
79 |
|
* |
80 |
|
* @param <E> the type of elements maintained by this set |
81 |
|
* |
82 |
|
* @author Josh Bloch |
83 |
< |
* @version %I%, %G% |
84 |
< |
* @see Collection |
85 |
< |
* @see Set |
68 |
< |
* @see HashSet |
83 |
> |
* @see Collection |
84 |
> |
* @see Set |
85 |
> |
* @see HashSet |
86 |
|
* @see Comparable |
87 |
|
* @see Comparator |
88 |
< |
* @see TreeMap |
88 |
> |
* @see TreeMap |
89 |
|
* @since 1.2 |
90 |
|
*/ |
91 |
|
|
117 |
|
* {@code e2} in the set. If the user attempts to add an element |
118 |
|
* to the set that violates this constraint (for example, the user |
119 |
|
* attempts to add a string element to a set whose elements are |
120 |
< |
* integers), the {@code add(Object)} call will throw a |
120 |
> |
* integers), the {@code add} call will throw a |
121 |
|
* {@code ClassCastException}. |
122 |
|
*/ |
123 |
|
public TreeSet() { |
124 |
< |
this(new TreeMap<E,Object>()); |
124 |
> |
this(new TreeMap<E,Object>()); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
131 |
|
* e2)} must not throw a {@code ClassCastException} for any elements |
132 |
|
* {@code e1} and {@code e2} in the set. If the user attempts to add |
133 |
|
* an element to the set that violates this constraint, the |
134 |
< |
* {@code add(Object)} call will throw a {@code ClassCastException}. |
134 |
> |
* {@code add} call will throw a {@code ClassCastException}. |
135 |
|
* |
136 |
|
* @param comparator the comparator that will be used to order this set. |
137 |
|
* If {@code null}, the {@linkplain Comparable natural |
138 |
|
* ordering} of the elements will be used. |
139 |
|
*/ |
140 |
|
public TreeSet(Comparator<? super E> comparator) { |
141 |
< |
this(new TreeMap<E,Object>(comparator)); |
141 |
> |
this(new TreeMap<E,Object>(comparator)); |
142 |
|
} |
143 |
|
|
144 |
|
/** |
169 |
|
*/ |
170 |
|
public TreeSet(SortedSet<E> s) { |
171 |
|
this(s.comparator()); |
172 |
< |
addAll(s); |
172 |
> |
addAll(s); |
173 |
|
} |
174 |
|
|
175 |
|
/** |
188 |
|
* @since 1.6 |
189 |
|
*/ |
190 |
|
public Iterator<E> descendingIterator() { |
191 |
< |
return m.descendingKeySet().iterator(); |
191 |
> |
return m.descendingKeySet().iterator(); |
192 |
|
} |
193 |
|
|
194 |
|
/** |
195 |
|
* @since 1.6 |
196 |
|
*/ |
197 |
|
public NavigableSet<E> descendingSet() { |
198 |
< |
return new TreeSet(m.descendingMap()); |
198 |
> |
return new TreeSet<E>(m.descendingMap()); |
199 |
|
} |
200 |
|
|
201 |
|
/** |
204 |
|
* @return the number of elements in this set (its cardinality) |
205 |
|
*/ |
206 |
|
public int size() { |
207 |
< |
return m.size(); |
207 |
> |
return m.size(); |
208 |
|
} |
209 |
|
|
210 |
|
/** |
213 |
|
* @return {@code true} if this set contains no elements |
214 |
|
*/ |
215 |
|
public boolean isEmpty() { |
216 |
< |
return m.isEmpty(); |
216 |
> |
return m.isEmpty(); |
217 |
|
} |
218 |
|
|
219 |
|
/** |
231 |
|
* does not permit null elements |
232 |
|
*/ |
233 |
|
public boolean contains(Object o) { |
234 |
< |
return m.containsKey(o); |
234 |
> |
return m.containsKey(o); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
252 |
|
* does not permit null elements |
253 |
|
*/ |
254 |
|
public boolean add(E e) { |
255 |
< |
return m.put(e, PRESENT)==null; |
255 |
> |
return m.put(e, PRESENT)==null; |
256 |
|
} |
257 |
|
|
258 |
|
/** |
273 |
|
* does not permit null elements |
274 |
|
*/ |
275 |
|
public boolean remove(Object o) { |
276 |
< |
return m.remove(o)==PRESENT; |
276 |
> |
return m.remove(o)==PRESENT; |
277 |
|
} |
278 |
|
|
279 |
|
/** |
281 |
|
* The set will be empty after this call returns. |
282 |
|
*/ |
283 |
|
public void clear() { |
284 |
< |
m.clear(); |
284 |
> |
m.clear(); |
285 |
|
} |
286 |
|
|
287 |
|
/** |
298 |
|
public boolean addAll(Collection<? extends E> c) { |
299 |
|
// Use linear-time version if applicable |
300 |
|
if (m.size()==0 && c.size() > 0 && |
301 |
< |
c instanceof SortedSet && |
301 |
> |
c instanceof SortedSet && |
302 |
|
m instanceof TreeMap) { |
303 |
|
SortedSet<? extends E> set = (SortedSet<? extends E>) c; |
304 |
|
TreeMap<E,Object> map = (TreeMap<E, Object>) m; |
322 |
|
*/ |
323 |
|
public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, |
324 |
|
E toElement, boolean toInclusive) { |
325 |
< |
return new TreeSet<E>(m.subMap(fromElement, fromInclusive, |
325 |
> |
return new TreeSet<E>(m.subMap(fromElement, fromInclusive, |
326 |
|
toElement, toInclusive)); |
327 |
|
} |
328 |
|
|
335 |
|
* @since 1.6 |
336 |
|
*/ |
337 |
|
public NavigableSet<E> headSet(E toElement, boolean inclusive) { |
338 |
< |
return new TreeSet<E>(m.headMap(toElement, inclusive)); |
338 |
> |
return new TreeSet<E>(m.headMap(toElement, inclusive)); |
339 |
|
} |
340 |
|
|
341 |
|
/** |
347 |
|
* @since 1.6 |
348 |
|
*/ |
349 |
|
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) { |
350 |
< |
return new TreeSet<E>(m.tailMap(fromElement, inclusive)); |
350 |
> |
return new TreeSet<E>(m.tailMap(fromElement, inclusive)); |
351 |
|
} |
352 |
|
|
353 |
|
/** |
358 |
|
* @throws IllegalArgumentException {@inheritDoc} |
359 |
|
*/ |
360 |
|
public SortedSet<E> subSet(E fromElement, E toElement) { |
361 |
< |
return subSet(fromElement, true, toElement, false); |
361 |
> |
return subSet(fromElement, true, toElement, false); |
362 |
|
} |
363 |
|
|
364 |
|
/** |
369 |
|
* @throws IllegalArgumentException {@inheritDoc} |
370 |
|
*/ |
371 |
|
public SortedSet<E> headSet(E toElement) { |
372 |
< |
return headSet(toElement, false); |
372 |
> |
return headSet(toElement, false); |
373 |
|
} |
374 |
|
|
375 |
|
/** |
380 |
|
* @throws IllegalArgumentException {@inheritDoc} |
381 |
|
*/ |
382 |
|
public SortedSet<E> tailSet(E fromElement) { |
383 |
< |
return tailSet(fromElement, true); |
383 |
> |
return tailSet(fromElement, true); |
384 |
|
} |
385 |
|
|
386 |
|
public Comparator<? super E> comparator() { |
471 |
|
*/ |
472 |
|
public Object clone() { |
473 |
|
TreeSet<E> clone = null; |
474 |
< |
try { |
475 |
< |
clone = (TreeSet<E>) super.clone(); |
476 |
< |
} catch (CloneNotSupportedException e) { |
477 |
< |
throw new InternalError(); |
478 |
< |
} |
474 |
> |
try { |
475 |
> |
clone = (TreeSet<E>) super.clone(); |
476 |
> |
} catch (CloneNotSupportedException e) { |
477 |
> |
throw new InternalError(); |
478 |
> |
} |
479 |
|
|
480 |
|
clone.m = new TreeMap<E,Object>(m); |
481 |
|
return clone; |
495 |
|
*/ |
496 |
|
private void writeObject(java.io.ObjectOutputStream s) |
497 |
|
throws java.io.IOException { |
498 |
< |
// Write out any hidden stuff |
499 |
< |
s.defaultWriteObject(); |
498 |
> |
// Write out any hidden stuff |
499 |
> |
s.defaultWriteObject(); |
500 |
|
|
501 |
|
// Write out Comparator |
502 |
|
s.writeObject(m.comparator()); |
504 |
|
// Write out size |
505 |
|
s.writeInt(m.size()); |
506 |
|
|
507 |
< |
// Write out all elements in the proper order. |
508 |
< |
for (Iterator i=m.keySet().iterator(); i.hasNext(); ) |
509 |
< |
s.writeObject(i.next()); |
507 |
> |
// Write out all elements in the proper order. |
508 |
> |
for (E e : m.keySet()) |
509 |
> |
s.writeObject(e); |
510 |
|
} |
511 |
|
|
512 |
|
/** |
515 |
|
*/ |
516 |
|
private void readObject(java.io.ObjectInputStream s) |
517 |
|
throws java.io.IOException, ClassNotFoundException { |
518 |
< |
// Read in any hidden stuff |
519 |
< |
s.defaultReadObject(); |
518 |
> |
// Read in any hidden stuff |
519 |
> |
s.defaultReadObject(); |
520 |
|
|
521 |
|
// Read in Comparator |
522 |
|
Comparator<? super E> c = (Comparator<? super E>) s.readObject(); |
523 |
|
|
524 |
|
// Create backing TreeMap |
525 |
< |
TreeMap<E,Object> tm; |
526 |
< |
if (c==null) |
527 |
< |
tm = new TreeMap<E,Object>(); |
528 |
< |
else |
529 |
< |
tm = new TreeMap<E,Object>(c); |
530 |
< |
m = tm; |
525 |
> |
TreeMap<E,Object> tm; |
526 |
> |
if (c==null) |
527 |
> |
tm = new TreeMap<E,Object>(); |
528 |
> |
else |
529 |
> |
tm = new TreeMap<E,Object>(c); |
530 |
> |
m = tm; |
531 |
|
|
532 |
|
// Read in size |
533 |
|
int size = s.readInt(); |