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

Comparing jsr166/src/main/java/util/Collections.java (file contents):
Revision 1.1 by dl, Tue Dec 28 12:14:07 2004 UTC vs.
Revision 1.5 by jsr166, Mon May 2 08:35:49 2005 UTC

# Line 1 | Line 1
1   /*
2   * %W% %E%
3   *
4 < * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
4 > * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5   * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6   */
7  
# Line 37 | Line 37 | import java.lang.reflect.Array;
37   * example, invoking the <tt>sort</tt> method on an unmodifiable list that is
38   * already sorted may or may not throw <tt>UnsupportedOperationException</tt>.
39   *
40 < * <p>This class is a member of the
40 > * <p>This class is a member of the
41   * <a href="{@docRoot}/../guide/collections/index.html">
42   * Java Collections Framework</a>.
43   *
# Line 97 | Line 97 | public class Collections {
97       * The sorting algorithm is a modified mergesort (in which the merge is
98       * omitted if the highest element in the low sublist is less than the
99       * lowest element in the high sublist).  This algorithm offers guaranteed
100 <     * n log(n) performance.
100 >     * n log(n) performance.
101       *
102       * This implementation dumps the specified list into an array, sorts
103       * the array, and iterates over the list resetting each element
# Line 135 | Line 135 | public class Collections {
135       * The sorting algorithm is a modified mergesort (in which the merge is
136       * omitted if the highest element in the low sublist is less than the
137       * lowest element in the high sublist).  This algorithm offers guaranteed
138 <     * n log(n) performance.
138 >     * n log(n) performance.
139       *
140       * The specified list must be modifiable, but need not be resizable.
141       * This implementation dumps the specified list into an array, sorts
# Line 182 | Line 182 | public class Collections {
182       *
183       * @param  list the list to be searched.
184       * @param  key the key to be searched for.
185 <     * @return index of the search key, if it is contained in the list;
185 >     * @return the index of the search key, if it is contained in the list;
186       *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
187       *         <i>insertion point</i> is defined as the point at which the
188       *         key would be inserted into the list: the index of the first
# Line 287 | Line 287 | public class Collections {
287       * @param  c the comparator by which the list is ordered.  A
288       *        <tt>null</tt> value indicates that the elements' <i>natural
289       *        ordering</i> should be used.
290 <     * @return index of the search key, if it is contained in the list;
290 >     * @return the index of the search key, if it is contained in the list;
291       *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
292       *         <i>insertion point</i> is defined as the point at which the
293       *         key would be inserted into the list: the index of the first
# Line 361 | Line 361 | public class Collections {
361       *
362       * @param  list the list whose elements are to be reversed.
363       * @throws UnsupportedOperationException if the specified list or
364 <     *         its list-iterator does not support the <tt>set</tt> method.
364 >     *         its list-iterator does not support the <tt>set</tt> operation.
365       */
366      public static void reverse(List<?> list) {
367          int size = list.size();
# Line 405 | Line 405 | public class Collections {
405       *
406       * @param  list the list to be shuffled.
407       * @throws UnsupportedOperationException if the specified list or
408 <     *         its list-iterator does not support the <tt>set</tt> method.
408 >     *         its list-iterator does not support the <tt>set</tt> operation.
409       */
410      public static void shuffle(List<?> list) {
411          shuffle(list, r);
# Line 712 | Line 712 | public class Collections {
712       *     Collections.rotate(l.subList(1, 4), -1);
713       * </pre>
714       * The resulting list is <tt>[a, c, d, b, e]</tt>.
715 <     *
715 >     *
716       * <p>To move more than one element forward, increase the absolute value
717       * of the rotation distance.  To move elements backward, use a positive
718       * shift distance.
# Line 736 | Line 736 | public class Collections {
736       *        constraints on this value; it may be zero, negative, or
737       *        greater than <tt>list.size()</tt>.
738       * @throws UnsupportedOperationException if the specified list or
739 <     *         its list-iterator does not support the <tt>set</tt> method.
739 >     *         its list-iterator does not support the <tt>set</tt> operation.
740       * @since 1.4
741       */
742      public static void rotate(List<?> list, int distance) {
# Line 772 | Line 772 | public class Collections {
772      private static void rotate2(List<?> list, int distance) {
773          int size = list.size();
774          if (size == 0)
775 <            return;
775 >            return;
776          int mid =  -distance % size;
777          if (mid < 0)
778              mid += size;
# Line 799 | Line 799 | public class Collections {
799       *         <tt>e</tt> such that
800       *         <tt>(oldVal==null ?  e==null : oldVal.equals(e))</tt>.
801       * @throws UnsupportedOperationException if the specified list or
802 <     *         its list-iterator does not support the <tt>set</tt> method.
802 >     *         its list-iterator does not support the <tt>set</tt> operation.
803       * @since  1.4
804       */
805      public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) {
# Line 970 | Line 970 | public class Collections {
970       * that the backing collection is a set or a list.<p>
971       *
972       * The returned collection will be serializable if the specified collection
973 <     * is serializable.
973 >     * is serializable.
974       *
975       * @param  c the collection for which an unmodifiable view is to be
976       *         returned.
# Line 1014 | Line 1014 | public class Collections {
1014              };
1015          }
1016  
1017 <        public boolean add(E o){
1017 >        public boolean add(E e){
1018              throw new UnsupportedOperationException();
1019          }
1020          public boolean remove(Object o) {
# Line 1046 | Line 1046 | public class Collections {
1046       * iterator, result in an <tt>UnsupportedOperationException</tt>.<p>
1047       *
1048       * The returned set will be serializable if the specified set
1049 <     * is serializable.
1049 >     * is serializable.
1050       *
1051       * @param  s the set for which an unmodifiable view is to be returned.
1052       * @return an unmodifiable view of the specified set.
# Line 1078 | Line 1078 | public class Collections {
1078       * an <tt>UnsupportedOperationException</tt>.<p>
1079       *
1080       * The returned sorted set will be serializable if the specified sorted set
1081 <     * is serializable.
1081 >     * is serializable.
1082       *
1083       * @param s the sorted set for which an unmodifiable view is to be
1084 <     *        returned.
1084 >     *        returned.
1085       * @return an unmodifiable view of the specified sorted set.
1086       */
1087      public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) {
# Line 1183 | Line 1183 | public class Collections {
1183                  public void remove() {
1184                      throw new UnsupportedOperationException();
1185                  }
1186 <                public void set(E o) {
1186 >                public void set(E e) {
1187                      throw new UnsupportedOperationException();
1188                  }
1189 <                public void add(E o) {
1189 >                public void add(E e) {
1190                      throw new UnsupportedOperationException();
1191                  }
1192              };
# Line 1252 | Line 1252 | public class Collections {
1252       * <tt>UnsupportedOperationException</tt>.<p>
1253       *
1254       * The returned map will be serializable if the specified map
1255 <     * is serializable.
1255 >     * is serializable.
1256       *
1257       * @param  m the map for which an unmodifiable view is to be returned.
1258       * @return an unmodifiable view of the specified map.
# Line 1334 | Line 1334 | public class Collections {
1334              private static final long serialVersionUID = 7854390611657943733L;
1335  
1336              UnmodifiableEntrySet(Set<? extends Map.Entry<? extends K, ? extends V>> s) {
1337 <                super((Set<Map.Entry<K,V>>)(Set)s);
1337 >                super((Set)s);
1338              }
1339              public Iterator<Map.Entry<K,V>> iterator() {
1340                  return new Iterator<Map.Entry<K,V>>() {
# Line 1456 | Line 1456 | public class Collections {
1456       * an <tt>UnsupportedOperationException</tt>.<p>
1457       *
1458       * The returned sorted map will be serializable if the specified sorted map
1459 <     * is serializable.
1459 >     * is serializable.
1460       *
1461       * @param m the sorted map for which an unmodifiable view is to be
1462 <     *        returned.
1462 >     *        returned.
1463       * @return an unmodifiable view of the specified sorted map.
1464       */
1465      public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m) {
# Line 1523 | Line 1523 | public class Collections {
1523       * that the backing collection is a set or a list.<p>
1524       *
1525       * The returned collection will be serializable if the specified collection
1526 <     * is serializable.
1526 >     * is serializable.
1527       *
1528       * @param  c the collection to be "wrapped" in a synchronized collection.
1529       * @return a synchronized view of the specified collection.
# Line 1577 | Line 1577 | public class Collections {
1577              return c.iterator(); // Must be manually synched by user!
1578          }
1579  
1580 <        public boolean add(E o) {
1581 <            synchronized(mutex) {return c.add(o);}
1580 >        public boolean add(E e) {
1581 >            synchronized(mutex) {return c.add(e);}
1582          }
1583          public boolean remove(Object o) {
1584              synchronized(mutex) {return c.remove(o);}
# Line 1673 | Line 1673 | public class Collections {
1673       * sorted set when iterating over it or any of its <tt>subSet</tt>,
1674       * <tt>headSet</tt>, or <tt>tailSet</tt> views.
1675       * <pre>
1676 <     *  SortedSet s = Collections.synchronizedSortedSet(new HashSortedSet());
1676 >     *  SortedSet s = Collections.synchronizedSortedSet(new TreeSet());
1677       *      ...
1678       *  synchronized(s) {
1679       *      Iterator i = s.iterator(); // Must be in the synchronized block
# Line 1683 | Line 1683 | public class Collections {
1683       * </pre>
1684       * or:
1685       * <pre>
1686 <     *  SortedSet s = Collections.synchronizedSortedSet(new HashSortedSet());
1686 >     *  SortedSet s = Collections.synchronizedSortedSet(new TreeSet());
1687       *  SortedSet s2 = s.headSet(foo);
1688       *      ...
1689       *  synchronized(s) {  // Note: s, not s2!!!
# Line 2007 | Line 2007 | public class Collections {
2007          public Set<Map.Entry<K,V>> entrySet() {
2008              synchronized(mutex) {
2009                  if (entrySet==null)
2010 <                    entrySet = new SynchronizedSet<Map.Entry<K,V>>((Set<Map.Entry<K,V>>)m.entrySet(), mutex);
2010 >                    entrySet = new SynchronizedSet<Map.Entry<K,V>>(m.entrySet(), mutex);
2011                  return entrySet;
2012              }
2013          }
# Line 2045 | Line 2045 | public class Collections {
2045       * collections views of any of its <tt>subMap</tt>, <tt>headMap</tt> or
2046       * <tt>tailMap</tt> views.
2047       * <pre>
2048 <     *  SortedMap m = Collections.synchronizedSortedMap(new HashSortedMap());
2048 >     *  SortedMap m = Collections.synchronizedSortedMap(new TreeMap());
2049       *      ...
2050       *  Set s = m.keySet();  // Needn't be in synchronized block
2051       *      ...
# Line 2057 | Line 2057 | public class Collections {
2057       * </pre>
2058       * or:
2059       * <pre>
2060 <     *  SortedMap m = Collections.synchronizedSortedMap(new HashSortedMap());
2060 >     *  SortedMap m = Collections.synchronizedSortedMap(new TreeMap());
2061       *  SortedMap m2 = m.subMap(foo, bar);
2062       *      ...
2063       *  Set s2 = m2.keySet();  // Needn't be in synchronized block
# Line 2191 | Line 2191 | public class Collections {
2191                                                        Class<E> type) {
2192          return new CheckedCollection<E>(c, type);
2193      }
2194 <
2194 >
2195      /**
2196       * @serial include
2197       */
# Line 2236 | Line 2236 | public class Collections {
2236              c.clear();
2237          }
2238  
2239 <        public boolean add(E o){
2240 <            typeCheck(o);
2241 <            return c.add(o);
2239 >        public boolean add(E e){
2240 >            typeCheck(e);
2241 >            return c.add(e);
2242          }
2243  
2244          public boolean addAll(Collection<? extends E> coll) {
# Line 2265 | Line 2265 | public class Collections {
2265          private E[] zeroLengthElementArray = null; // Lazily initialized
2266  
2267          /*
2268 <         * We don't need locking or volatile, because it's OK if we create
2268 >         * We don't need locking or volatile, because it's OK if we create
2269           * several zeroLengthElementArrays, and they're immutable.
2270           */
2271          E[] zeroLengthElementArray() {
# Line 2300 | Line 2300 | public class Collections {
2300      public static <E> Set<E> checkedSet(Set<E> s, Class<E> type) {
2301          return new CheckedSet<E>(s, type);
2302      }
2303 <
2303 >
2304      /**
2305       * @serial include
2306       */
# Line 2456 | Line 2456 | public class Collections {
2456                  public int previousIndex()   { return i.previousIndex(); }
2457                  public void remove()         { i.remove(); }
2458  
2459 <                public void set(E o) {
2460 <                    typeCheck(o);
2461 <                    i.set(o);
2459 >                public void set(E e) {
2460 >                    typeCheck(e);
2461 >                    i.set(e);
2462                  }
2463  
2464 <                public void add(E o) {
2465 <                    typeCheck(o);
2466 <                    i.add(o);
2464 >                public void add(E e) {
2465 >                    typeCheck(e);
2466 >                    i.add(e);
2467                  }
2468              };
2469          }
# Line 2604 | Line 2604 | public class Collections {
2604          private V[] zeroLengthValueArray = null;
2605  
2606          /*
2607 <         * We don't need locking or volatile, because it's OK if we create
2607 >         * We don't need locking or volatile, because it's OK if we create
2608           * several zeroLengthValueArrays, and they're immutable.
2609           */
2610          private K[] zeroLengthKeyArray() {
# Line 2658 | Line 2658 | public class Collections {
2658                  s.clear();
2659              }
2660  
2661 <            public boolean add(Map.Entry<K, V> o){
2661 >            public boolean add(Map.Entry<K, V> e){
2662                  throw new UnsupportedOperationException();
2663              }
2664              public boolean addAll(Collection<? extends Map.Entry<K, V>> coll) {
# Line 3060 | Line 3060 | public class Collections {
3060  
3061          final private E element;
3062  
3063 <        SingletonSet(E o) {element = o;}
3063 >        SingletonSet(E e) {element = e;}
3064  
3065          public Iterator<E> iterator() {
3066              return new Iterator<E>() {
# Line 3168 | Line 3168 | public class Collections {
3168  
3169          public Set<Map.Entry<K,V>> entrySet() {
3170              if (entrySet==null)
3171 <                entrySet = singleton((Map.Entry<K,V>)new ImmutableEntry<K,V>(k, v));
3171 >                entrySet = Collections.<Map.Entry<K,V>>singleton(
3172 >                    new SimpleImmutableEntry<K,V>(k, v));
3173              return entrySet;
3174          }
3175  
# Line 3178 | Line 3179 | public class Collections {
3179              return values;
3180          }
3181  
3181        private static class ImmutableEntry<K,V>
3182            implements Map.Entry<K,V> {
3183            final K k;
3184            final V v;
3185
3186            ImmutableEntry(K key, V value) {
3187                k = key;
3188                v = value;
3189            }
3190
3191            public K getKey()   {return k;}
3192
3193            public V getValue() {return v;}
3194
3195            public V setValue(V value) {
3196                throw new UnsupportedOperationException();
3197            }
3198
3199            public boolean equals(Object o) {
3200                if (!(o instanceof Map.Entry))
3201                    return false;
3202                Map.Entry e = (Map.Entry)o;
3203                return eq(e.getKey(), k) && eq(e.getValue(), v);
3204            }
3205
3206            public int hashCode() {
3207                return ((k==null ? 0 : k.hashCode()) ^
3208                        (v==null ? 0 : v.hashCode()));
3209            }
3210
3211            public String toString() {
3212                return k+"="+v;
3213            }
3214        }
3182      }
3183  
3184      /**
# Line 3245 | Line 3212 | public class Collections {
3212          int n;
3213          E element;
3214  
3215 <        CopiesList(int n, E o) {
3215 >        CopiesList(int n, E e) {
3216              if (n < 0)
3217                  throw new IllegalArgumentException("List length = " + n);
3218              this.n = n;
3219 <            element = o;
3219 >            element = e;
3220          }
3221  
3222          public int size() {
# Line 3324 | Line 3291 | public class Collections {
3291      public static <T> Comparator<T> reverseOrder(Comparator<T> cmp) {
3292          if (cmp == null)
3293              return new ReverseComparator();  // Unchecked warning!!
3294 <
3294 >
3295          return new ReverseComparator2<T>(cmp);
3296      }
3297 <
3297 >
3298      /**
3299       * @serial include
3300       */
# Line 3335 | Line 3302 | public class Collections {
3302          Serializable
3303      {
3304          private static final long serialVersionUID = 4374092139857L;
3305 <
3305 >
3306          /**
3307           * The comparator specified in the static factory.  This will never
3308           * be null, as the static factory returns a ReverseComparator
# Line 3344 | Line 3311 | public class Collections {
3311           * @serial
3312           */
3313          private Comparator<T> cmp;
3314 <
3314 >
3315          ReverseComparator2(Comparator<T> cmp) {
3316              assert cmp != null;
3317              this.cmp = cmp;
3318          }
3319 <
3319 >
3320          public int compare(T t1, T t2) {
3321              return cmp.compare(t2, t1);
3322          }
# Line 3469 | Line 3436 | public class Collections {
3436              c1 = c2;
3437              c2 = tmp;
3438          }
3439 <
3439 >
3440          for (Object e : c1)
3441              if (c2.contains(e))
3442                  return false;
# Line 3493 | Line 3460 | public class Collections {
3460       * @param a the elements to insert into <tt>c</tt>
3461       * @return <tt>true</tt> if the collection changed as a result of the call
3462       * @throws UnsupportedOperationException if <tt>c</tt> does not support
3463 <     *         the <tt>add</tt> method
3463 >     *         the <tt>add</tt> operation.
3464       * @throws NullPointerException if <tt>elements</tt> contains one or more
3465       *         null values and <tt>c</tt> does not support null elements, or
3466       *         if <tt>c</tt> or <tt>elements</tt> are <tt>null</tt>
# Line 3599 | Line 3566 | public class Collections {
3566          return new AsLIFOQueue<T>(deque);
3567      }
3568  
3569 <    static class AsLIFOQueue<E> extends AbstractQueue<E>
3569 >    static class AsLIFOQueue<E> extends AbstractQueue<E>
3570          implements Queue<E>, Serializable {
3571          private final Deque<E> q;
3572          AsLIFOQueue(Deque<E> q)            { this.q = q; }
3573 <        public boolean offer(E o)          { return q.offerFirst(o); }
3573 >        public boolean offer(E e)          { return q.offerFirst(e); }
3574          public E poll()                    { return q.pollFirst(); }
3575          public E remove()                  { return q.removeFirst(); }
3576          public E peek()                    { return q.peekFirst(); }
# Line 3614 | Line 3581 | public class Collections {
3581          public Iterator<E> iterator()      { return q.iterator(); }
3582          public Object[] toArray()          { return q.toArray(); }
3583          public <T> T[] toArray(T[] a)      { return q.toArray(a); }
3584 <        public boolean add(E o)            { return q.offerFirst(o); }
3584 >        public boolean add(E e)            { return q.offerFirst(e); }
3585          public boolean remove(Object o)    { return q.remove(o); }
3586          public void clear()                { q.clear(); }
3587      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines