src/share/classes/java/util/Collections.java

Print this page

        

*** 122,132 **** * input arrays to n/2 object references for randomly ordered input * arrays. * * <p>The implementation takes equal advantage of ascending and * descending order in its input array, and can take advantage of ! * ascending and descending order in different parts of the the same * input array. It is well-suited to merging two or more sorted arrays: * simply concatenate the arrays and sort the resulting array. * * <p>The implementation was adapted from Tim Peters's list sort for Python * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> --- 122,132 ---- * input arrays to n/2 object references for randomly ordered input * arrays. * * <p>The implementation takes equal advantage of ascending and * descending order in its input array, and can take advantage of ! * ascending and descending order in different parts of the same * input array. It is well-suited to merging two or more sorted arrays: * simply concatenate the arrays and sort the resulting array. * * <p>The implementation was adapted from Tim Peters's list sort for Python * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
*** 182,192 **** * input arrays to n/2 object references for randomly ordered input * arrays. * * <p>The implementation takes equal advantage of ascending and * descending order in its input array, and can take advantage of ! * ascending and descending order in different parts of the the same * input array. It is well-suited to merging two or more sorted arrays: * simply concatenate the arrays and sort the resulting array. * * <p>The implementation was adapted from Tim Peters's list sort for Python * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> --- 182,192 ---- * input arrays to n/2 object references for randomly ordered input * arrays. * * <p>The implementation takes equal advantage of ascending and * descending order in its input array, and can take advantage of ! * ascending and descending order in different parts of the same * input array. It is well-suited to merging two or more sorted arrays: * simply concatenate the arrays and sort the resulting array. * * <p>The implementation was adapted from Tim Peters's list sort for Python * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
*** 821,831 **** i += distance; if (i >= size) i -= size; displaced = list.set(i, displaced); nMoved ++; ! } while(i != cycleStart); } } private static void rotate2(List<?> list, int distance) { int size = list.size(); --- 821,831 ---- i += distance; if (i >= size) i -= size; displaced = list.set(i, displaced); nMoved ++; ! } while (i != cycleStart); } } private static void rotate2(List<?> list, int distance) { int size = list.size();
*** 1450,1462 **** * The next two methods are overridden to protect against * an unscrupulous List whose contains(Object o) method senses * when o is a Map.Entry, and calls o.setValue. */ public boolean containsAll(Collection<?> coll) { ! Iterator<?> e = coll.iterator(); ! while (e.hasNext()) ! if (!contains(e.next())) // Invokes safe contains() above return false; return true; } public boolean equals(Object o) { if (o == this) --- 1450,1462 ---- * The next two methods are overridden to protect against * an unscrupulous List whose contains(Object o) method senses * when o is a Map.Entry, and calls o.setValue. */ public boolean containsAll(Collection<?> coll) { ! Iterator<?> it = coll.iterator(); ! while (it.hasNext()) ! if (!contains(it.next())) // Invokes safe contains() above return false; return true; } public boolean equals(Object o) { if (o == this)
*** 1560,1570 **** * It is imperative that the user manually synchronize on the returned * collection when iterating over it: * <pre> * Collection c = Collections.synchronizedCollection(myCollection); * ... ! * synchronized(c) { * Iterator i = c.iterator(); // Must be in the synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre> --- 1560,1570 ---- * It is imperative that the user manually synchronize on the returned * collection when iterating over it: * <pre> * Collection c = Collections.synchronizedCollection(myCollection); * ... ! * synchronized (c) { * Iterator i = c.iterator(); // Must be in the synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre>
*** 1609,1664 **** this.c = c; this.mutex = mutex; } public int size() { ! synchronized(mutex) {return c.size();} } public boolean isEmpty() { ! synchronized(mutex) {return c.isEmpty();} } public boolean contains(Object o) { ! synchronized(mutex) {return c.contains(o);} } public Object[] toArray() { ! synchronized(mutex) {return c.toArray();} } public <T> T[] toArray(T[] a) { ! synchronized(mutex) {return c.toArray(a);} } public Iterator<E> iterator() { return c.iterator(); // Must be manually synched by user! } public boolean add(E e) { ! synchronized(mutex) {return c.add(e);} } public boolean remove(Object o) { ! synchronized(mutex) {return c.remove(o);} } public boolean containsAll(Collection<?> coll) { ! synchronized(mutex) {return c.containsAll(coll);} } public boolean addAll(Collection<? extends E> coll) { ! synchronized(mutex) {return c.addAll(coll);} } public boolean removeAll(Collection<?> coll) { ! synchronized(mutex) {return c.removeAll(coll);} } public boolean retainAll(Collection<?> coll) { ! synchronized(mutex) {return c.retainAll(coll);} } public void clear() { ! synchronized(mutex) {c.clear();} } public String toString() { ! synchronized(mutex) {return c.toString();} } private void writeObject(ObjectOutputStream s) throws IOException { ! synchronized(mutex) {s.defaultWriteObject();} } } /** * Returns a synchronized (thread-safe) set backed by the specified --- 1609,1664 ---- this.c = c; this.mutex = mutex; } public int size() { ! synchronized (mutex) {return c.size();} } public boolean isEmpty() { ! synchronized (mutex) {return c.isEmpty();} } public boolean contains(Object o) { ! synchronized (mutex) {return c.contains(o);} } public Object[] toArray() { ! synchronized (mutex) {return c.toArray();} } public <T> T[] toArray(T[] a) { ! synchronized (mutex) {return c.toArray(a);} } public Iterator<E> iterator() { return c.iterator(); // Must be manually synched by user! } public boolean add(E e) { ! synchronized (mutex) {return c.add(e);} } public boolean remove(Object o) { ! synchronized (mutex) {return c.remove(o);} } public boolean containsAll(Collection<?> coll) { ! synchronized (mutex) {return c.containsAll(coll);} } public boolean addAll(Collection<? extends E> coll) { ! synchronized (mutex) {return c.addAll(coll);} } public boolean removeAll(Collection<?> coll) { ! synchronized (mutex) {return c.removeAll(coll);} } public boolean retainAll(Collection<?> coll) { ! synchronized (mutex) {return c.retainAll(coll);} } public void clear() { ! synchronized (mutex) {c.clear();} } public String toString() { ! synchronized (mutex) {return c.toString();} } private void writeObject(ObjectOutputStream s) throws IOException { ! synchronized (mutex) {s.defaultWriteObject();} } } /** * Returns a synchronized (thread-safe) set backed by the specified
*** 1669,1679 **** * It is imperative that the user manually synchronize on the returned * set when iterating over it: * <pre> * Set s = Collections.synchronizedSet(new HashSet()); * ... ! * synchronized(s) { * Iterator i = s.iterator(); // Must be in the synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre> --- 1669,1679 ---- * It is imperative that the user manually synchronize on the returned * set when iterating over it: * <pre> * Set s = Collections.synchronizedSet(new HashSet()); * ... ! * synchronized (s) { * Iterator i = s.iterator(); // Must be in the synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre>
*** 1707,1720 **** SynchronizedSet(Set<E> s, Object mutex) { super(s, mutex); } public boolean equals(Object o) { ! synchronized(mutex) {return c.equals(o);} } public int hashCode() { ! synchronized(mutex) {return c.hashCode();} } } /** * Returns a synchronized (thread-safe) sorted set backed by the specified --- 1707,1720 ---- SynchronizedSet(Set<E> s, Object mutex) { super(s, mutex); } public boolean equals(Object o) { ! synchronized (mutex) {return c.equals(o);} } public int hashCode() { ! synchronized (mutex) {return c.hashCode();} } } /** * Returns a synchronized (thread-safe) sorted set backed by the specified
*** 1726,1736 **** * sorted set when iterating over it or any of its <tt>subSet</tt>, * <tt>headSet</tt>, or <tt>tailSet</tt> views. * <pre> * SortedSet s = Collections.synchronizedSortedSet(new TreeSet()); * ... ! * synchronized(s) { * Iterator i = s.iterator(); // Must be in the synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre> --- 1726,1736 ---- * sorted set when iterating over it or any of its <tt>subSet</tt>, * <tt>headSet</tt>, or <tt>tailSet</tt> views. * <pre> * SortedSet s = Collections.synchronizedSortedSet(new TreeSet()); * ... ! * synchronized (s) { * Iterator i = s.iterator(); // Must be in the synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre>
*** 1737,1747 **** * or: * <pre> * SortedSet s = Collections.synchronizedSortedSet(new TreeSet()); * SortedSet s2 = s.headSet(foo); * ... ! * synchronized(s) { // Note: s, not s2!!! * Iterator i = s2.iterator(); // Must be in the synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre> --- 1737,1747 ---- * or: * <pre> * SortedSet s = Collections.synchronizedSortedSet(new TreeSet()); * SortedSet s2 = s.headSet(foo); * ... ! * synchronized (s) { // Note: s, not s2!!! * Iterator i = s2.iterator(); // Must be in the synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre>
*** 1764,1774 **** extends SynchronizedSet<E> implements SortedSet<E> { private static final long serialVersionUID = 8695801310862127406L; ! final private SortedSet<E> ss; SynchronizedSortedSet(SortedSet<E> s) { super(s); ss = s; } --- 1764,1774 ---- extends SynchronizedSet<E> implements SortedSet<E> { private static final long serialVersionUID = 8695801310862127406L; ! private final SortedSet<E> ss; SynchronizedSortedSet(SortedSet<E> s) { super(s); ss = s; }
*** 1776,1810 **** super(s, mutex); ss = s; } public Comparator<? super E> comparator() { ! synchronized(mutex) {return ss.comparator();} } public SortedSet<E> subSet(E fromElement, E toElement) { ! synchronized(mutex) { return new SynchronizedSortedSet<E>( ss.subSet(fromElement, toElement), mutex); } } public SortedSet<E> headSet(E toElement) { ! synchronized(mutex) { return new SynchronizedSortedSet<E>(ss.headSet(toElement), mutex); } } public SortedSet<E> tailSet(E fromElement) { ! synchronized(mutex) { return new SynchronizedSortedSet<E>(ss.tailSet(fromElement),mutex); } } public E first() { ! synchronized(mutex) {return ss.first();} } public E last() { ! synchronized(mutex) {return ss.last();} } } /** * Returns a synchronized (thread-safe) list backed by the specified --- 1776,1810 ---- super(s, mutex); ss = s; } public Comparator<? super E> comparator() { ! synchronized (mutex) {return ss.comparator();} } public SortedSet<E> subSet(E fromElement, E toElement) { ! synchronized (mutex) { return new SynchronizedSortedSet<E>( ss.subSet(fromElement, toElement), mutex); } } public SortedSet<E> headSet(E toElement) { ! synchronized (mutex) { return new SynchronizedSortedSet<E>(ss.headSet(toElement), mutex); } } public SortedSet<E> tailSet(E fromElement) { ! synchronized (mutex) { return new SynchronizedSortedSet<E>(ss.tailSet(fromElement),mutex); } } public E first() { ! synchronized (mutex) {return ss.first();} } public E last() { ! synchronized (mutex) {return ss.last();} } } /** * Returns a synchronized (thread-safe) list backed by the specified
*** 1815,1825 **** * It is imperative that the user manually synchronize on the returned * list when iterating over it: * <pre> * List list = Collections.synchronizedList(new ArrayList()); * ... ! * synchronized(list) { * Iterator i = list.iterator(); // Must be in synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre> --- 1815,1825 ---- * It is imperative that the user manually synchronize on the returned * list when iterating over it: * <pre> * List list = Collections.synchronizedList(new ArrayList()); * ... ! * synchronized (list) { * Iterator i = list.iterator(); // Must be in synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre>
*** 1861,1898 **** super(list, mutex); this.list = list; } public boolean equals(Object o) { ! synchronized(mutex) {return list.equals(o);} } public int hashCode() { ! synchronized(mutex) {return list.hashCode();} } public E get(int index) { ! synchronized(mutex) {return list.get(index);} } public E set(int index, E element) { ! synchronized(mutex) {return list.set(index, element);} } public void add(int index, E element) { ! synchronized(mutex) {list.add(index, element);} } public E remove(int index) { ! synchronized(mutex) {return list.remove(index);} } public int indexOf(Object o) { ! synchronized(mutex) {return list.indexOf(o);} } public int lastIndexOf(Object o) { ! synchronized(mutex) {return list.lastIndexOf(o);} } public boolean addAll(int index, Collection<? extends E> c) { ! synchronized(mutex) {return list.addAll(index, c);} } public ListIterator<E> listIterator() { return list.listIterator(); // Must be manually synched by user } --- 1861,1898 ---- super(list, mutex); this.list = list; } public boolean equals(Object o) { ! synchronized (mutex) {return list.equals(o);} } public int hashCode() { ! synchronized (mutex) {return list.hashCode();} } public E get(int index) { ! synchronized (mutex) {return list.get(index);} } public E set(int index, E element) { ! synchronized (mutex) {return list.set(index, element);} } public void add(int index, E element) { ! synchronized (mutex) {list.add(index, element);} } public E remove(int index) { ! synchronized (mutex) {return list.remove(index);} } public int indexOf(Object o) { ! synchronized (mutex) {return list.indexOf(o);} } public int lastIndexOf(Object o) { ! synchronized (mutex) {return list.lastIndexOf(o);} } public boolean addAll(int index, Collection<? extends E> c) { ! synchronized (mutex) {return list.addAll(index, c);} } public ListIterator<E> listIterator() { return list.listIterator(); // Must be manually synched by user }
*** 1900,1910 **** public ListIterator<E> listIterator(int index) { return list.listIterator(index); // Must be manually synched by user } public List<E> subList(int fromIndex, int toIndex) { ! synchronized(mutex) { return new SynchronizedList<E>(list.subList(fromIndex, toIndex), mutex); } } --- 1900,1910 ---- public ListIterator<E> listIterator(int index) { return list.listIterator(index); // Must be manually synched by user } public List<E> subList(int fromIndex, int toIndex) { ! synchronized (mutex) { return new SynchronizedList<E>(list.subList(fromIndex, toIndex), mutex); } }
*** 1941,1951 **** SynchronizedRandomAccessList(List<E> list, Object mutex) { super(list, mutex); } public List<E> subList(int fromIndex, int toIndex) { ! synchronized(mutex) { return new SynchronizedRandomAccessList<E>( list.subList(fromIndex, toIndex), mutex); } } --- 1941,1951 ---- SynchronizedRandomAccessList(List<E> list, Object mutex) { super(list, mutex); } public List<E> subList(int fromIndex, int toIndex) { ! synchronized (mutex) { return new SynchronizedRandomAccessList<E>( list.subList(fromIndex, toIndex), mutex); } }
*** 1973,1983 **** * <pre> * Map m = Collections.synchronizedMap(new HashMap()); * ... * Set s = m.keySet(); // Needn't be in synchronized block * ... ! * synchronized(m) { // Synchronizing on m, not s! * Iterator i = s.iterator(); // Must be in synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre> --- 1973,1983 ---- * <pre> * Map m = Collections.synchronizedMap(new HashMap()); * ... * Set s = m.keySet(); // Needn't be in synchronized block * ... ! * synchronized (m) { // Synchronizing on m, not s! * Iterator i = s.iterator(); // Must be in synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre>
*** 2014,2090 **** this.m = m; this.mutex = mutex; } public int size() { ! synchronized(mutex) {return m.size();} } public boolean isEmpty() { ! synchronized(mutex) {return m.isEmpty();} } public boolean containsKey(Object key) { ! synchronized(mutex) {return m.containsKey(key);} } public boolean containsValue(Object value) { ! synchronized(mutex) {return m.containsValue(value);} } public V get(Object key) { ! synchronized(mutex) {return m.get(key);} } public V put(K key, V value) { ! synchronized(mutex) {return m.put(key, value);} } public V remove(Object key) { ! synchronized(mutex) {return m.remove(key);} } public void putAll(Map<? extends K, ? extends V> map) { ! synchronized(mutex) {m.putAll(map);} } public void clear() { ! synchronized(mutex) {m.clear();} } private transient Set<K> keySet = null; private transient Set<Map.Entry<K,V>> entrySet = null; private transient Collection<V> values = null; public Set<K> keySet() { ! synchronized(mutex) { if (keySet==null) keySet = new SynchronizedSet<K>(m.keySet(), mutex); return keySet; } } public Set<Map.Entry<K,V>> entrySet() { ! synchronized(mutex) { if (entrySet==null) entrySet = new SynchronizedSet<Map.Entry<K,V>>(m.entrySet(), mutex); return entrySet; } } public Collection<V> values() { ! synchronized(mutex) { if (values==null) values = new SynchronizedCollection<V>(m.values(), mutex); return values; } } public boolean equals(Object o) { ! synchronized(mutex) {return m.equals(o);} } public int hashCode() { ! synchronized(mutex) {return m.hashCode();} } public String toString() { ! synchronized(mutex) {return m.toString();} } private void writeObject(ObjectOutputStream s) throws IOException { ! synchronized(mutex) {s.defaultWriteObject();} } } /** * Returns a synchronized (thread-safe) sorted map backed by the specified --- 2014,2090 ---- this.m = m; this.mutex = mutex; } public int size() { ! synchronized (mutex) {return m.size();} } public boolean isEmpty() { ! synchronized (mutex) {return m.isEmpty();} } public boolean containsKey(Object key) { ! synchronized (mutex) {return m.containsKey(key);} } public boolean containsValue(Object value) { ! synchronized (mutex) {return m.containsValue(value);} } public V get(Object key) { ! synchronized (mutex) {return m.get(key);} } public V put(K key, V value) { ! synchronized (mutex) {return m.put(key, value);} } public V remove(Object key) { ! synchronized (mutex) {return m.remove(key);} } public void putAll(Map<? extends K, ? extends V> map) { ! synchronized (mutex) {m.putAll(map);} } public void clear() { ! synchronized (mutex) {m.clear();} } private transient Set<K> keySet = null; private transient Set<Map.Entry<K,V>> entrySet = null; private transient Collection<V> values = null; public Set<K> keySet() { ! synchronized (mutex) { if (keySet==null) keySet = new SynchronizedSet<K>(m.keySet(), mutex); return keySet; } } public Set<Map.Entry<K,V>> entrySet() { ! synchronized (mutex) { if (entrySet==null) entrySet = new SynchronizedSet<Map.Entry<K,V>>(m.entrySet(), mutex); return entrySet; } } public Collection<V> values() { ! synchronized (mutex) { if (values==null) values = new SynchronizedCollection<V>(m.values(), mutex); return values; } } public boolean equals(Object o) { ! synchronized (mutex) {return m.equals(o);} } public int hashCode() { ! synchronized (mutex) {return m.hashCode();} } public String toString() { ! synchronized (mutex) {return m.toString();} } private void writeObject(ObjectOutputStream s) throws IOException { ! synchronized (mutex) {s.defaultWriteObject();} } } /** * Returns a synchronized (thread-safe) sorted map backed by the specified
*** 2099,2109 **** * <pre> * SortedMap m = Collections.synchronizedSortedMap(new TreeMap()); * ... * Set s = m.keySet(); // Needn't be in synchronized block * ... ! * synchronized(m) { // Synchronizing on m, not s! * Iterator i = s.iterator(); // Must be in synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre> --- 2099,2109 ---- * <pre> * SortedMap m = Collections.synchronizedSortedMap(new TreeMap()); * ... * Set s = m.keySet(); // Needn't be in synchronized block * ... ! * synchronized (m) { // Synchronizing on m, not s! * Iterator i = s.iterator(); // Must be in synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre>
*** 2112,2122 **** * SortedMap m = Collections.synchronizedSortedMap(new TreeMap()); * SortedMap m2 = m.subMap(foo, bar); * ... * Set s2 = m2.keySet(); // Needn't be in synchronized block * ... ! * synchronized(m) { // Synchronizing on m, not m2 or s2! * Iterator i = s.iterator(); // Must be in synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre> --- 2112,2122 ---- * SortedMap m = Collections.synchronizedSortedMap(new TreeMap()); * SortedMap m2 = m.subMap(foo, bar); * ... * Set s2 = m2.keySet(); // Needn't be in synchronized block * ... ! * synchronized (m) { // Synchronizing on m, not m2 or s2! * Iterator i = s.iterator(); // Must be in synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre>
*** 2152,2186 **** super(m, mutex); sm = m; } public Comparator<? super K> comparator() { ! synchronized(mutex) {return sm.comparator();} } public SortedMap<K,V> subMap(K fromKey, K toKey) { ! synchronized(mutex) { return new SynchronizedSortedMap<K,V>( sm.subMap(fromKey, toKey), mutex); } } public SortedMap<K,V> headMap(K toKey) { ! synchronized(mutex) { return new SynchronizedSortedMap<K,V>(sm.headMap(toKey), mutex); } } public SortedMap<K,V> tailMap(K fromKey) { ! synchronized(mutex) { return new SynchronizedSortedMap<K,V>(sm.tailMap(fromKey),mutex); } } public K firstKey() { ! synchronized(mutex) {return sm.firstKey();} } public K lastKey() { ! synchronized(mutex) {return sm.lastKey();} } } // Dynamically typesafe collection wrappers --- 2152,2186 ---- super(m, mutex); sm = m; } public Comparator<? super K> comparator() { ! synchronized (mutex) {return sm.comparator();} } public SortedMap<K,V> subMap(K fromKey, K toKey) { ! synchronized (mutex) { return new SynchronizedSortedMap<K,V>( sm.subMap(fromKey, toKey), mutex); } } public SortedMap<K,V> headMap(K toKey) { ! synchronized (mutex) { return new SynchronizedSortedMap<K,V>(sm.headMap(toKey), mutex); } } public SortedMap<K,V> tailMap(K fromKey) { ! synchronized (mutex) { return new SynchronizedSortedMap<K,V>(sm.tailMap(fromKey),mutex); } } public K firstKey() { ! synchronized (mutex) {return sm.firstKey();} } public K lastKey() { ! synchronized (mutex) {return sm.lastKey();} } } // Dynamically typesafe collection wrappers
*** 3315,3325 **** extends AbstractSet<E> implements Serializable { private static final long serialVersionUID = 3193687207550431679L; ! final private E element; SingletonSet(E e) {element = e;} public Iterator<E> iterator() { return singletonIterator(element); --- 3315,3325 ---- extends AbstractSet<E> implements Serializable { private static final long serialVersionUID = 3193687207550431679L; ! private final E element; SingletonSet(E e) {element = e;} public Iterator<E> iterator() { return singletonIterator(element);
*** 3446,3456 **** * * @param n the number of elements in the returned list. * @param o the element to appear repeatedly in the returned list. * @return an immutable list consisting of <tt>n</tt> copies of the * specified object. ! * @throws IllegalArgumentException if n &lt; 0. * @see List#addAll(Collection) * @see List#addAll(int, Collection) */ public static <T> List<T> nCopies(int n, T o) { if (n < 0) --- 3446,3456 ---- * * @param n the number of elements in the returned list. * @param o the element to appear repeatedly in the returned list. * @return an immutable list consisting of <tt>n</tt> copies of the * specified object. ! * @throws IllegalArgumentException if {@code n < 0} * @see List#addAll(Collection) * @see List#addAll(int, Collection) */ public static <T> List<T> nCopies(int n, T o) { if (n < 0)