ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/TreeSet.java
(Generate patch)

Comparing jsr166/src/main/java/util/TreeSet.java (file contents):
Revision 1.23 by jsr166, Fri Apr 21 03:24:07 2006 UTC vs.
Revision 1.28 by jsr166, Sun May 18 23:59:57 2008 UTC

# Line 1 | Line 1
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;
# Line 56 | Line 74 | 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  
# Line 100 | Line 117 | public class TreeSet<E> extends Abstract
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      /**
# Line 114 | Line 131 | public class TreeSet<E> extends Abstract
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      /**
# Line 152 | Line 169 | public class TreeSet<E> extends Abstract
169       */
170      public TreeSet(SortedSet<E> s) {
171          this(s.comparator());
172 <        addAll(s);
172 >        addAll(s);
173      }
174  
175      /**
# Line 171 | Line 188 | public class TreeSet<E> extends Abstract
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(m.descendingMap());
199      }
200  
201      /**
# Line 187 | Line 204 | public class TreeSet<E> extends Abstract
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      /**
# Line 196 | Line 213 | public class TreeSet<E> extends Abstract
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      /**
# Line 214 | Line 231 | public class TreeSet<E> extends Abstract
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      /**
# Line 235 | Line 252 | public class TreeSet<E> extends Abstract
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      /**
# Line 256 | Line 273 | public class TreeSet<E> extends Abstract
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      /**
# Line 264 | Line 281 | public class TreeSet<E> extends Abstract
281       * The set will be empty after this call returns.
282       */
283      public void clear() {
284 <        m.clear();
284 >        m.clear();
285      }
286  
287      /**
# Line 281 | Line 298 | public class TreeSet<E> extends Abstract
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;
# Line 305 | Line 322 | public class TreeSet<E> extends Abstract
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  
# Line 318 | Line 335 | public class TreeSet<E> extends Abstract
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      /**
# Line 330 | Line 347 | public class TreeSet<E> extends Abstract
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      /**
# Line 341 | Line 358 | public class TreeSet<E> extends Abstract
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      /**
# Line 352 | Line 369 | public class TreeSet<E> extends Abstract
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      /**
# Line 363 | Line 380 | public class TreeSet<E> extends Abstract
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() {
# Line 454 | Line 471 | public class TreeSet<E> extends Abstract
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;
# Line 478 | Line 495 | public class TreeSet<E> extends Abstract
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());
# Line 487 | Line 504 | public class TreeSet<E> extends Abstract
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(); )
507 >        // Write out all elements in the proper order.
508 >        for (Iterator i=m.keySet().iterator(); i.hasNext(); )
509              s.writeObject(i.next());
510      }
511  
# Line 498 | Line 515 | public class TreeSet<E> extends Abstract
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();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines