--- jsr166/src/main/java/util/Collections.java 2005/12/05 02:56:59 1.18
+++ jsr166/src/main/java/util/Collections.java 2008/05/18 23:59:57 1.34
@@ -1,12 +1,29 @@
/*
- * %W% %E%
+ * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
- * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
*/
package java.util;
-import java.util.*; // for javadoc (till 6280605 is fixed)
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.IOException;
@@ -39,16 +56,15 @@ import java.lang.reflect.Array;
* already sorted may or may not throw UnsupportedOperationException.
*
*
This class is a member of the
- *
+ *
* Java Collections Framework.
*
* @author Josh Bloch
* @author Neal Gafter
- * @version %I%, %G%
- * @see Collection
- * @see Set
- * @see List
- * @see Map
+ * @see Collection
+ * @see Set
+ * @see List
+ * @see Map
* @since 1.2
*/
@@ -108,19 +124,19 @@ public class Collections {
*
* @param list the list to be sorted.
* @throws ClassCastException if the list contains elements that are not
- * mutually comparable (for example, strings and integers).
+ * mutually comparable (for example, strings and integers).
* @throws UnsupportedOperationException if the specified list's
- * list-iterator does not support the set operation.
+ * list-iterator does not support the set operation.
* @see Comparable
*/
public static > void sort(List list) {
- Object[] a = list.toArray();
- Arrays.sort(a);
- ListIterator i = list.listIterator();
- for (int j=0; j i = list.listIterator();
+ for (int j=0; jnull value indicates that the elements' natural
* ordering should be used.
* @throws ClassCastException if the list contains elements that are not
- * mutually comparable using the specified comparator.
+ * mutually comparable using the specified comparator.
* @throws UnsupportedOperationException if the specified list's
- * list-iterator does not support the set operation.
+ * list-iterator does not support the set operation.
* @see Comparator
*/
public static void sort(List list, Comparator super T> c) {
- Object[] a = list.toArray();
- Arrays.sort(a, (Comparator)c);
- ListIterator i = list.listIterator();
- for (int j=0; jnatural ordering of its elements (as by the
- * sort(List) method, above) prior to making this call. If it is
- * not sorted, the results are undefined. If the list contains multiple
- * elements equal to the specified object, there is no guarantee which one
- * will be found.
+ * according to the {@linkplain Comparable natural ordering} of its
+ * elements (as by the {@link #sort(List)} method) prior to making this
+ * call. If it is not sorted, the results are undefined. If the list
+ * contains multiple elements equal to the specified object, there is no
+ * guarantee which one will be found.
*
- * This method runs in log(n) time for a "random access" list (which
+ *
This method runs in log(n) time for a "random access" list (which
* provides near-constant-time positional access). If the specified list
* does not implement the {@link RandomAccess} interface and is large,
* this method will do an iterator-based binary search that performs
@@ -184,19 +200,17 @@ public class Collections {
* @param list the list to be searched.
* @param key the key to be searched for.
* @return the index of the search key, if it is contained in the list;
- * otherwise, (-(insertion point) - 1). The
- * insertion point is defined as the point at which the
- * key would be inserted into the list: the index of the first
- * element greater than the key, or list.size(), if all
- * elements in the list are less than the specified key. Note
- * that this guarantees that the return value will be >= 0 if
- * and only if the key is found.
+ * otherwise, (-(insertion point) - 1). The
+ * insertion point is defined as the point at which the
+ * key would be inserted into the list: the index of the first
+ * element greater than the key, or list.size() if all
+ * elements in the list are less than the specified key. Note
+ * that this guarantees that the return value will be >= 0 if
+ * and only if the key is found.
* @throws ClassCastException if the list contains elements that are not
- * mutually comparable (for example, strings and
- * integers), or the search key in not mutually comparable
- * with the elements of the list.
- * @see Comparable
- * @see #sort(List)
+ * mutually comparable (for example, strings and
+ * integers), or the search key is not mutually comparable
+ * with the elements of the list.
*/
public static
int binarySearch(List extends Comparable super T>> list, T key) {
@@ -209,33 +223,33 @@ public class Collections {
private static
int indexedBinarySearch(List extends Comparable super T>> list, T key)
{
- int low = 0;
- int high = list.size()-1;
+ int low = 0;
+ int high = list.size()-1;
+
+ while (low <= high) {
+ int mid = (low + high) >>> 1;
+ Comparable super T> midVal = list.get(mid);
+ int cmp = midVal.compareTo(key);
- while (low <= high) {
- int mid = (low + high) >> 1;
- Comparable super T> midVal = list.get(mid);
- int cmp = midVal.compareTo(key);
-
- if (cmp < 0)
- low = mid + 1;
- else if (cmp > 0)
- high = mid - 1;
- else
- return mid; // key found
- }
- return -(low + 1); // key not found
+ if (cmp < 0)
+ low = mid + 1;
+ else if (cmp > 0)
+ high = mid - 1;
+ else
+ return mid; // key found
+ }
+ return -(low + 1); // key not found
}
private static
int iteratorBinarySearch(List extends Comparable super T>> list, T key)
{
- int low = 0;
- int high = list.size()-1;
+ int low = 0;
+ int high = list.size()-1;
ListIterator extends Comparable super T>> i = list.listIterator();
while (low <= high) {
- int mid = (low + high) >> 1;
+ int mid = (low + high) >>> 1;
Comparable super T> midVal = get(i, mid);
int cmp = midVal.compareTo(key);
@@ -254,7 +268,7 @@ public class Collections {
* list listIterator.
*/
private static T get(ListIterator extends T> i, int index) {
- T obj = null;
+ T obj = null;
int pos = i.nextIndex();
if (pos <= index) {
do {
@@ -271,13 +285,14 @@ public class Collections {
/**
* Searches the specified list for the specified object using the binary
* search algorithm. The list must be sorted into ascending order
- * according to the specified comparator (as by the Sort(List,
- * Comparator) method, above), prior to making this call. If it is
+ * according to the specified comparator (as by the
+ * {@link #sort(List, Comparator) sort(List, Comparator)}
+ * method), prior to making this call. If it is
* not sorted, the results are undefined. If the list contains multiple
* elements equal to the specified object, there is no guarantee which one
- * will be found.
+ * will be found.
*
- * This method runs in log(n) time for a "random access" list (which
+ *
This method runs in log(n) time for a "random access" list (which
* provides near-constant-time positional access). If the specified list
* does not implement the {@link RandomAccess} interface and is large,
* this method will do an iterator-based binary search that performs
@@ -285,23 +300,21 @@ public class Collections {
*
* @param list the list to be searched.
* @param key the key to be searched for.
- * @param c the comparator by which the list is ordered. A
- * null value indicates that the elements' natural
- * ordering should be used.
+ * @param c the comparator by which the list is ordered.
+ * A null value indicates that the elements'
+ * {@linkplain Comparable natural ordering} should be used.
* @return the index of the search key, if it is contained in the list;
- * otherwise, (-(insertion point) - 1). The
- * insertion point is defined as the point at which the
- * key would be inserted into the list: the index of the first
- * element greater than the key, or list.size(), if all
- * elements in the list are less than the specified key. Note
- * that this guarantees that the return value will be >= 0 if
- * and only if the key is found.
+ * otherwise, (-(insertion point) - 1). The
+ * insertion point is defined as the point at which the
+ * key would be inserted into the list: the index of the first
+ * element greater than the key, or list.size() if all
+ * elements in the list are less than the specified key. Note
+ * that this guarantees that the return value will be >= 0 if
+ * and only if the key is found.
* @throws ClassCastException if the list contains elements that are not
- * mutually comparable using the specified comparator,
- * or the search key in not mutually comparable with the
- * elements of the list using this comparator.
- * @see Comparable
- * @see #sort(List, Comparator)
+ * mutually comparable using the specified comparator,
+ * or the search key is not mutually comparable with the
+ * elements of the list using this comparator.
*/
public static int binarySearch(List extends T> list, T key, Comparator super T> c) {
if (c==null)
@@ -314,31 +327,31 @@ public class Collections {
}
private static int indexedBinarySearch(List extends T> l, T key, Comparator super T> c) {
- int low = 0;
- int high = l.size()-1;
+ int low = 0;
+ int high = l.size()-1;
+
+ while (low <= high) {
+ int mid = (low + high) >>> 1;
+ T midVal = l.get(mid);
+ int cmp = c.compare(midVal, key);
- while (low <= high) {
- int mid = (low + high) >> 1;
- T midVal = l.get(mid);
- int cmp = c.compare(midVal, key);
-
- if (cmp < 0)
- low = mid + 1;
- else if (cmp > 0)
- high = mid - 1;
- else
- return mid; // key found
- }
- return -(low + 1); // key not found
+ if (cmp < 0)
+ low = mid + 1;
+ else if (cmp > 0)
+ high = mid - 1;
+ else
+ return mid; // key found
+ }
+ return -(low + 1); // key not found
}
private static int iteratorBinarySearch(List extends T> l, T key, Comparator super T> c) {
- int low = 0;
- int high = l.size()-1;
+ int low = 0;
+ int high = l.size()-1;
ListIterator extends T> i = l.listIterator();
while (low <= high) {
- int mid = (low + high) >> 1;
+ int mid = (low + high) >>> 1;
T midVal = get(i, mid);
int cmp = c.compare(midVal, key);
@@ -373,7 +386,7 @@ public class Collections {
ListIterator fwd = list.listIterator();
ListIterator rev = list.listIterator(size);
for (int i=0, mid=list.size()>>1; i list, int i, int j) {
- final List l = list;
- l.set(i, l.set(j, l.get(i)));
+ final List l = list;
+ l.set(i, l.set(j, l.get(i)));
}
/**
@@ -496,7 +509,7 @@ public class Collections {
* @param list the list to be filled with the specified element.
* @param obj The element with which to fill the specified list.
* @throws UnsupportedOperationException if the specified list or its
- * list-iterator does not support the set operation.
+ * list-iterator does not support the set operation.
*/
public static void fill(List super T> list, T obj) {
int size = list.size();
@@ -540,7 +553,7 @@ public class Collections {
dest.set(i, src.get(i));
} else {
ListIterator super T> di=dest.listIterator();
- ListIterator extends T> si=src.listIterator();
+ ListIterator extends T> si=src.listIterator();
for (int i=0; inatural ordering of its elements.
* @throws ClassCastException if the collection contains elements that are
- * not mutually comparable (for example, strings and
- * integers).
+ * not mutually comparable (for example, strings and
+ * integers).
* @throws NoSuchElementException if the collection is empty.
* @see Comparable
*/
public static > T min(Collection extends T> coll) {
- Iterator extends T> i = coll.iterator();
- T candidate = i.next();
+ Iterator extends T> i = coll.iterator();
+ T candidate = i.next();
while (i.hasNext()) {
- T next = i.next();
- if (next.compareTo(candidate) < 0)
- candidate = next;
- }
- return candidate;
+ T next = i.next();
+ if (next.compareTo(candidate) < 0)
+ candidate = next;
+ }
+ return candidate;
}
/**
@@ -599,7 +612,7 @@ public class Collections {
* @return the minimum element of the given collection, according
* to the specified comparator.
* @throws ClassCastException if the collection contains elements that are
- * not mutually comparable using the specified comparator.
+ * not mutually comparable using the specified comparator.
* @throws NoSuchElementException if the collection is empty.
* @see Comparable
*/
@@ -607,15 +620,15 @@ public class Collections {
if (comp==null)
return (T)min((Collection) (Collection) coll);
- Iterator extends T> i = coll.iterator();
- T candidate = i.next();
+ Iterator extends T> i = coll.iterator();
+ T candidate = i.next();
while (i.hasNext()) {
- T next = i.next();
- if (comp.compare(next, candidate) < 0)
- candidate = next;
- }
- return candidate;
+ T next = i.next();
+ if (comp.compare(next, candidate) < 0)
+ candidate = next;
+ }
+ return candidate;
}
/**
@@ -634,21 +647,21 @@ public class Collections {
* @return the maximum element of the given collection, according
* to the natural ordering of its elements.
* @throws ClassCastException if the collection contains elements that are
- * not mutually comparable (for example, strings and
+ * not mutually comparable (for example, strings and
* integers).
* @throws NoSuchElementException if the collection is empty.
* @see Comparable
*/
public static > T max(Collection extends T> coll) {
- Iterator extends T> i = coll.iterator();
- T candidate = i.next();
+ Iterator extends T> i = coll.iterator();
+ T candidate = i.next();
while (i.hasNext()) {
- T next = i.next();
- if (next.compareTo(candidate) > 0)
- candidate = next;
- }
- return candidate;
+ T next = i.next();
+ if (next.compareTo(candidate) > 0)
+ candidate = next;
+ }
+ return candidate;
}
/**
@@ -669,7 +682,7 @@ public class Collections {
* @return the maximum element of the given collection, according
* to the specified comparator.
* @throws ClassCastException if the collection contains elements that are
- * not mutually comparable using the specified comparator.
+ * not mutually comparable using the specified comparator.
* @throws NoSuchElementException if the collection is empty.
* @see Comparable
*/
@@ -677,15 +690,15 @@ public class Collections {
if (comp==null)
return (T)max((Collection) (Collection) coll);
- Iterator extends T> i = coll.iterator();
- T candidate = i.next();
+ Iterator extends T> i = coll.iterator();
+ T candidate = i.next();
while (i.hasNext()) {
- T next = i.next();
- if (comp.compare(next, candidate) > 0)
- candidate = next;
- }
- return candidate;
+ T next = i.next();
+ if (comp.compare(next, candidate) > 0)
+ candidate = next;
+ }
+ return candidate;
}
/**
@@ -745,9 +758,9 @@ public class Collections {
*/
public static void rotate(List> list, int distance) {
if (list instanceof RandomAccess || list.size() < ROTATE_THRESHOLD)
- rotate1((List)list, distance);
+ rotate1(list, distance);
else
- rotate2((List)list, distance);
+ rotate2(list, distance);
}
private static void rotate1(List list, int distance) {
@@ -977,68 +990,67 @@ public class Collections {
* is serializable.
*
* @param c the collection for which an unmodifiable view is to be
- * returned.
+ * returned.
* @return an unmodifiable view of the specified collection.
*/
public static Collection unmodifiableCollection(Collection extends T> c) {
- return new UnmodifiableCollection(c);
+ return new UnmodifiableCollection(c);
}
/**
* @serial include
*/
static class UnmodifiableCollection implements Collection, Serializable {
- // use serialVersionUID from JDK 1.2.2 for interoperability
- private static final long serialVersionUID = 1820017752578914078L;
+ private static final long serialVersionUID = 1820017752578914078L;
- final Collection extends E> c;
+ final Collection extends E> c;
- UnmodifiableCollection(Collection extends E> c) {
+ UnmodifiableCollection(Collection extends E> c) {
if (c==null)
throw new NullPointerException();
this.c = c;
}
- public int size() {return c.size();}
- public boolean isEmpty() {return c.isEmpty();}
- public boolean contains(Object o) {return c.contains(o);}
- public Object[] toArray() {return c.toArray();}
- public T[] toArray(T[] a) {return c.toArray(a);}
+ public int size() {return c.size();}
+ public boolean isEmpty() {return c.isEmpty();}
+ public boolean contains(Object o) {return c.contains(o);}
+ public Object[] toArray() {return c.toArray();}
+ public T[] toArray(T[] a) {return c.toArray(a);}
public String toString() {return c.toString();}
- public Iterator iterator() {
- return new Iterator() {
- Iterator extends E> i = c.iterator();
-
- public boolean hasNext() {return i.hasNext();}
- public E next() {return i.next();}
- public void remove() {
- throw new UnsupportedOperationException();
+ public Iterator iterator() {
+ return new Iterator() {
+ private final Iterator extends E> i = c.iterator();
+
+ public boolean hasNext() {return i.hasNext();}
+ public E next() {return i.next();}
+ public void remove() {
+ throw new UnsupportedOperationException();
}
- };
+ };
}
- public boolean add(E e){
- throw new UnsupportedOperationException();
+ public boolean add(E e) {
+ throw new UnsupportedOperationException();
}
- public boolean remove(Object o) {
- throw new UnsupportedOperationException();
+ public boolean remove(Object o) {
+ throw new UnsupportedOperationException();
}
- public boolean containsAll(Collection> coll) {
- return c.containsAll(coll);
+ public boolean containsAll(Collection> coll) {
+ return c.containsAll(coll);
}
- public boolean addAll(Collection extends E> coll) {
- throw new UnsupportedOperationException();
+ public boolean addAll(Collection extends E> coll) {
+ throw new UnsupportedOperationException();
}
- public boolean removeAll(Collection> coll) {
- throw new UnsupportedOperationException();
+ public boolean removeAll(Collection> coll) {
+ throw new UnsupportedOperationException();
}
- public boolean retainAll(Collection> coll) {
- throw new UnsupportedOperationException();
+ public boolean retainAll(Collection> coll) {
+ throw new UnsupportedOperationException();
}
- public void clear() {
- throw new UnsupportedOperationException();
+ public void clear() {
+ throw new UnsupportedOperationException();
}
}
@@ -1056,19 +1068,19 @@ public class Collections {
* @return an unmodifiable view of the specified set.
*/
public static Set unmodifiableSet(Set extends T> s) {
- return new UnmodifiableSet(s);
+ return new UnmodifiableSet(s);
}
/**
* @serial include
*/
static class UnmodifiableSet extends UnmodifiableCollection
- implements Set, Serializable {
- private static final long serialVersionUID = -9215047833775013803L;
+ implements Set, Serializable {
+ private static final long serialVersionUID = -9215047833775013803L;
- UnmodifiableSet(Set extends E> s) {super(s);}
- public boolean equals(Object o) {return c.equals(o);}
- public int hashCode() {return c.hashCode();}
+ UnmodifiableSet(Set extends E> s) {super(s);}
+ public boolean equals(Object o) {return o == this || c.equals(o);}
+ public int hashCode() {return c.hashCode();}
}
/**
@@ -1088,19 +1100,19 @@ public class Collections {
* @return an unmodifiable view of the specified sorted set.
*/
public static SortedSet unmodifiableSortedSet(SortedSet s) {
- return new UnmodifiableSortedSet(s);
+ return new UnmodifiableSortedSet(s);
}
/**
* @serial include
*/
static class UnmodifiableSortedSet
- extends UnmodifiableSet
- implements SortedSet, Serializable {
- private static final long serialVersionUID = -4929149591599911165L;
+ extends UnmodifiableSet
+ implements SortedSet, Serializable {
+ private static final long serialVersionUID = -4929149591599911165L;
private final SortedSet ss;
- UnmodifiableSortedSet(SortedSet s) {super(s); ss = s;}
+ UnmodifiableSortedSet(SortedSet s) {super(s); ss = s;}
public Comparator super E> comparator() {return ss.comparator();}
@@ -1114,8 +1126,8 @@ public class Collections {
return new UnmodifiableSortedSet(ss.tailSet(fromElement));
}
- public E first() {return ss.first();}
- public E last() {return ss.last();}
+ public E first() {return ss.first();}
+ public E last() {return ss.last();}
}
/**
@@ -1134,7 +1146,7 @@ public class Collections {
* @return an unmodifiable view of the specified list.
*/
public static List unmodifiableList(List extends T> list) {
- return (list instanceof RandomAccess ?
+ return (list instanceof RandomAccess ?
new UnmodifiableRandomAccessList(list) :
new UnmodifiableList(list));
}
@@ -1143,59 +1155,60 @@ public class Collections {
* @serial include
*/
static class UnmodifiableList extends UnmodifiableCollection
- implements List {
- static final long serialVersionUID = -283967356065247728L;
- final List extends E> list;
-
- UnmodifiableList(List extends E> list) {
- super(list);
- this.list = list;
- }
-
- public boolean equals(Object o) {return list.equals(o);}
- public int hashCode() {return list.hashCode();}
-
- public E get(int index) {return list.get(index);}
- public E set(int index, E element) {
- throw new UnsupportedOperationException();
- }
- public void add(int index, E element) {
- throw new UnsupportedOperationException();
- }
- public E remove(int index) {
- throw new UnsupportedOperationException();
- }
- public int indexOf(Object o) {return list.indexOf(o);}
- public int lastIndexOf(Object o) {return list.lastIndexOf(o);}
- public boolean addAll(int index, Collection extends E> c) {
- throw new UnsupportedOperationException();
- }
- public ListIterator listIterator() {return listIterator(0);}
-
- public ListIterator listIterator(final int index) {
- return new ListIterator() {
- ListIterator extends E> i = list.listIterator(index);
-
- public boolean hasNext() {return i.hasNext();}
- public E next() {return i.next();}
- public boolean hasPrevious() {return i.hasPrevious();}
- public E previous() {return i.previous();}
- public int nextIndex() {return i.nextIndex();}
- public int previousIndex() {return i.previousIndex();}
+ implements List {
+ private static final long serialVersionUID = -283967356065247728L;
+ final List extends E> list;
+
+ UnmodifiableList(List extends E> list) {
+ super(list);
+ this.list = list;
+ }
- public void remove() {
- throw new UnsupportedOperationException();
+ public boolean equals(Object o) {return o == this || list.equals(o);}
+ public int hashCode() {return list.hashCode();}
+
+ public E get(int index) {return list.get(index);}
+ public E set(int index, E element) {
+ throw new UnsupportedOperationException();
+ }
+ public void add(int index, E element) {
+ throw new UnsupportedOperationException();
+ }
+ public E remove(int index) {
+ throw new UnsupportedOperationException();
+ }
+ public int indexOf(Object o) {return list.indexOf(o);}
+ public int lastIndexOf(Object o) {return list.lastIndexOf(o);}
+ public boolean addAll(int index, Collection extends E> c) {
+ throw new UnsupportedOperationException();
+ }
+ public ListIterator listIterator() {return listIterator(0);}
+
+ public ListIterator listIterator(final int index) {
+ return new ListIterator() {
+ private final ListIterator extends E> i
+ = list.listIterator(index);
+
+ public boolean hasNext() {return i.hasNext();}
+ public E next() {return i.next();}
+ public boolean hasPrevious() {return i.hasPrevious();}
+ public E previous() {return i.previous();}
+ public int nextIndex() {return i.nextIndex();}
+ public int previousIndex() {return i.previousIndex();}
+
+ public void remove() {
+ throw new UnsupportedOperationException();
}
- public void set(E e) {
- throw new UnsupportedOperationException();
+ public void set(E e) {
+ throw new UnsupportedOperationException();
}
- public void add(E e) {
- throw new UnsupportedOperationException();
+ public void add(E e) {
+ throw new UnsupportedOperationException();
}
- };
- }
+ };
+ }
- public List subList(int fromIndex, int toIndex) {
+ public List subList(int fromIndex, int toIndex) {
return new UnmodifiableList(list.subList(fromIndex, toIndex));
}
@@ -1213,8 +1226,8 @@ public class Collections {
*/
private Object readResolve() {
return (list instanceof RandomAccess
- ? new UnmodifiableRandomAccessList(list)
- : this);
+ ? new UnmodifiableRandomAccessList(list)
+ : this);
}
}
@@ -1228,7 +1241,7 @@ public class Collections {
super(list);
}
- public List subList(int fromIndex, int toIndex) {
+ public List subList(int fromIndex, int toIndex) {
return new UnmodifiableRandomAccessList(
list.subList(fromIndex, toIndex));
}
@@ -1261,67 +1274,66 @@ public class Collections {
* @return an unmodifiable view of the specified map.
*/
public static Map unmodifiableMap(Map extends K, ? extends V> m) {
- return new UnmodifiableMap(m);
+ return new UnmodifiableMap(m);
}
/**
* @serial include
*/
private static class UnmodifiableMap implements Map, Serializable {
- // use serialVersionUID from JDK 1.2.2 for interoperability
- private static final long serialVersionUID = -1034234728574286014L;
+ private static final long serialVersionUID = -1034234728574286014L;
- private final Map extends K, ? extends V> m;
+ private final Map extends K, ? extends V> m;
- UnmodifiableMap(Map extends K, ? extends V> m) {
+ UnmodifiableMap(Map extends K, ? extends V> m) {
if (m==null)
throw new NullPointerException();
this.m = m;
}
- public int size() {return m.size();}
- public boolean isEmpty() {return m.isEmpty();}
- public boolean containsKey(Object key) {return m.containsKey(key);}
- public boolean containsValue(Object val) {return m.containsValue(val);}
- public V get(Object key) {return m.get(key);}
-
- public V put(K key, V value) {
- throw new UnsupportedOperationException();
- }
- public V remove(Object key) {
- throw new UnsupportedOperationException();
- }
- public void putAll(Map extends K, ? extends V> m) {
- throw new UnsupportedOperationException();
- }
- public void clear() {
- throw new UnsupportedOperationException();
- }
-
- private transient Set keySet = null;
- private transient Set> entrySet = null;
- private transient Collection values = null;
-
- public Set keySet() {
- if (keySet==null)
- keySet = unmodifiableSet(m.keySet());
- return keySet;
- }
-
- public Set> entrySet() {
- if (entrySet==null)
- entrySet = new UnmodifiableEntrySet(m.entrySet());
- return entrySet;
- }
-
- public Collection values() {
- if (values==null)
- values = unmodifiableCollection(m.values());
- return values;
- }
+ public int size() {return m.size();}
+ public boolean isEmpty() {return m.isEmpty();}
+ public boolean containsKey(Object key) {return m.containsKey(key);}
+ public boolean containsValue(Object val) {return m.containsValue(val);}
+ public V get(Object key) {return m.get(key);}
+
+ public V put(K key, V value) {
+ throw new UnsupportedOperationException();
+ }
+ public V remove(Object key) {
+ throw new UnsupportedOperationException();
+ }
+ public void putAll(Map extends K, ? extends V> m) {
+ throw new UnsupportedOperationException();
+ }
+ public void clear() {
+ throw new UnsupportedOperationException();
+ }
+
+ private transient Set keySet = null;
+ private transient Set> entrySet = null;
+ private transient Collection values = null;
+
+ public Set keySet() {
+ if (keySet==null)
+ keySet = unmodifiableSet(m.keySet());
+ return keySet;
+ }
+
+ public Set> entrySet() {
+ if (entrySet==null)
+ entrySet = new UnmodifiableEntrySet(m.entrySet());
+ return entrySet;
+ }
+
+ public Collection values() {
+ if (values==null)
+ values = unmodifiableCollection(m.values());
+ return values;
+ }
- public boolean equals(Object o) {return m.equals(o);}
- public int hashCode() {return m.hashCode();}
+ public boolean equals(Object o) {return o == this || m.equals(o);}
+ public int hashCode() {return m.hashCode();}
public String toString() {return m.toString();}
/**
@@ -1333,21 +1345,21 @@ public class Collections {
* @serial include
*/
static class UnmodifiableEntrySet
- extends UnmodifiableSet> {
- private static final long serialVersionUID = 7854390611657943733L;
+ extends UnmodifiableSet> {
+ private static final long serialVersionUID = 7854390611657943733L;
UnmodifiableEntrySet(Set extends Map.Entry extends K, ? extends V>> s) {
super((Set)s);
}
public Iterator> iterator() {
return new Iterator>() {
- Iterator extends Map.Entry extends K, ? extends V>> i = c.iterator();
+ private final Iterator extends Map.Entry extends K, ? extends V>> i = c.iterator();
public boolean hasNext() {
return i.hasNext();
}
- public Map.Entry next() {
- return new UnmodifiableEntry(i.next());
+ public Map.Entry next() {
+ return new UnmodifiableEntry(i.next());
}
public void remove() {
throw new UnsupportedOperationException();
@@ -1366,7 +1378,7 @@ public class Collections {
// We don't pass a to c.toArray, to avoid window of
// vulnerability wherein an unscrupulous multithreaded client
// could get his hands on raw (unwrapped) Entries from c.
- Object[] arr = c.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
+ Object[] arr = c.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
for (int i=0; i((Map.Entry)arr[i]);
@@ -1389,7 +1401,8 @@ public class Collections {
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
- return c.contains(new UnmodifiableEntry((Map.Entry) o));
+ return c.contains(
+ new UnmodifiableEntry