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.26 by jsr166, Sun May 20 07:54:01 2007 UTC vs.
Revision 1.32 by jsr166, Fri Oct 22 05:18:30 2010 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
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   * This code is free software; you can redistribute it and/or modify it
# Line 18 | Line 18
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.
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;
# Line 80 | Line 80 | package java.util;
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
86 < * @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 122 | Line 121 | public class TreeSet<E> extends Abstract
121       * {@code ClassCastException}.
122       */
123      public TreeSet() {
124 <        this(new TreeMap<E,Object>());
124 >        this(new TreeMap<E,Object>());
125      }
126  
127      /**
# Line 139 | Line 138 | public class TreeSet<E> extends Abstract
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 170 | 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 189 | 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<E>(m.descendingMap());
199      }
200  
201      /**
# Line 205 | 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 214 | 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 232 | 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 253 | 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 274 | 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 282 | 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 299 | 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 323 | 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 336 | 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 348 | 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 359 | 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 370 | 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 381 | 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 453 | Line 452 | public class TreeSet<E> extends Abstract
452       */
453      public E pollFirst() {
454          Map.Entry<E,?> e = m.pollFirstEntry();
455 <        return (e == null)? null : e.getKey();
455 >        return (e == null) ? null : e.getKey();
456      }
457  
458      /**
# Line 461 | Line 460 | public class TreeSet<E> extends Abstract
460       */
461      public E pollLast() {
462          Map.Entry<E,?> e = m.pollLastEntry();
463 <        return (e == null)? null : e.getKey();
463 >        return (e == null) ? null : e.getKey();
464      }
465  
466      /**
# Line 472 | 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 496 | 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 505 | 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(); )
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      /**
# Line 516 | 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