You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2007/01/02 17:22:07 UTC
svn commit: r491834 [3/7] - in
/harmony/enhanced/classlib/trunk/modules/luni/src/main/java: java/net/
java/util/ org/apache/harmony/luni/internal/nls/
org/apache/harmony/luni/internal/reflect/ org/apache/harmony/luni/net/
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java?view=diff&rev=491834&r1=491833&r2=491834
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java Tue Jan 2 08:22:05 2007
@@ -22,171 +22,176 @@
import java.io.Serializable;
import java.lang.reflect.Array;
+import org.apache.harmony.luni.internal.nls.Messages;
+
/**
* Collections contains static methods which operate on Collection classes.
+ *
* @since 1.2
*/
public class Collections {
- private static final class CopiesList<E> extends AbstractList<E> implements
- Serializable {
- private static final long serialVersionUID = 2739099268398711800L;
-
- private final int n;
-
- private final E element;
-
- CopiesList(int length, E object) {
- if (length < 0) {
- throw new IllegalArgumentException();
- }
- n = length;
- element = object;
- }
+ private static final class CopiesList<E> extends AbstractList<E> implements
+ Serializable {
+ private static final long serialVersionUID = 2739099268398711800L;
+
+ private final int n;
+
+ private final E element;
+
+ CopiesList(int length, E object) {
+ if (length < 0) {
+ throw new IllegalArgumentException();
+ }
+ n = length;
+ element = object;
+ }
- @Override
+ @Override
public boolean contains(Object object) {
- return element == null ? object == null : element.equals(object);
- }
+ return element == null ? object == null : element.equals(object);
+ }
- @Override
+ @Override
public int size() {
- return n;
- }
+ return n;
+ }
- @Override
+ @Override
public E get(int location) {
- if (0 <= location && location < n) {
+ if (0 <= location && location < n) {
return element;
}
- throw new IndexOutOfBoundsException();
- }
- }
+ throw new IndexOutOfBoundsException();
+ }
+ }
- @SuppressWarnings("unchecked")
+ @SuppressWarnings("unchecked")
private static final class EmptyList extends AbstractList implements
- Serializable {
- private static final long serialVersionUID = 8842843931221139166L;
+ Serializable {
+ private static final long serialVersionUID = 8842843931221139166L;
- @Override
+ @Override
public boolean contains(Object object) {
- return false;
- }
+ return false;
+ }
- @Override
+ @Override
public int size() {
- return 0;
- }
+ return 0;
+ }
- @Override
+ @Override
public Object get(int location) {
- throw new IndexOutOfBoundsException();
- }
-
+ throw new IndexOutOfBoundsException();
+ }
+
private Object readResolve() {
return Collections.EMPTY_LIST;
}
- }
+ }
- @SuppressWarnings("unchecked")
+ @SuppressWarnings("unchecked")
private static final class EmptySet extends AbstractSet implements
- Serializable {
- private static final long serialVersionUID = 1582296315990362920L;
+ Serializable {
+ private static final long serialVersionUID = 1582296315990362920L;
- @Override
+ @Override
public boolean contains(Object object) {
- return false;
- }
+ return false;
+ }
- @Override
+ @Override
public int size() {
- return 0;
- }
+ return 0;
+ }
- @Override
+ @Override
public Iterator iterator() {
- return new Iterator() {
- public boolean hasNext() {
- return false;
- }
-
- public Object next() {
- throw new NoSuchElementException();
- }
-
- public void remove() {
- throw new UnsupportedOperationException();
- }
- };
- }
-
+ return new Iterator() {
+ public boolean hasNext() {
+ return false;
+ }
+
+ public Object next() {
+ throw new NoSuchElementException();
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+
private Object readResolve() {
return Collections.EMPTY_SET;
}
- }
+ }
- @SuppressWarnings("unchecked")
+ @SuppressWarnings("unchecked")
private static final class EmptyMap extends AbstractMap implements
- Serializable {
- private static final long serialVersionUID = 6428348081105594320L;
+ Serializable {
+ private static final long serialVersionUID = 6428348081105594320L;
- @Override
+ @Override
public boolean containsKey(Object key) {
- return false;
- }
+ return false;
+ }
- @Override
+ @Override
public boolean containsValue(Object value) {
- return false;
- }
+ return false;
+ }
- @Override
+ @Override
public Set entrySet() {
- return EMPTY_SET;
- }
+ return EMPTY_SET;
+ }
- @Override
+ @Override
public Object get(Object key) {
- return null;
- }
+ return null;
+ }
- @Override
+ @Override
public Set keySet() {
- return EMPTY_SET;
- }
+ return EMPTY_SET;
+ }
- @Override
+ @Override
public Collection values() {
- return EMPTY_LIST;
- }
-
+ return EMPTY_LIST;
+ }
+
private Object readResolve() {
return Collections.EMPTY_MAP;
}
- }
+ }
@SuppressWarnings("unchecked")
public static final List EMPTY_LIST = new EmptyList();
- @SuppressWarnings("unchecked")
+ @SuppressWarnings("unchecked")
public static final Set EMPTY_SET = new EmptySet();
- @SuppressWarnings("unchecked")
+ @SuppressWarnings("unchecked")
public static final Map EMPTY_MAP = new EmptyMap();
- private static final class ReverseComparator<T> implements Comparator<T>, Serializable {
- private static final long serialVersionUID = 7207038068494060240L;
+ private static final class ReverseComparator<T> implements Comparator<T>,
+ Serializable {
+ private static final long serialVersionUID = 7207038068494060240L;
- @SuppressWarnings("unchecked")
+ @SuppressWarnings("unchecked")
public int compare(T o1, T o2) {
- Comparable<T> c2 = (Comparable<T>)o2;
- return c2.compareTo(o1);
- }
- }
-
+ Comparable<T> c2 = (Comparable<T>) o2;
+ return c2.compareTo(o1);
+ }
+ }
+
private static final class ReverseComparatorWithComparator<T> implements
Comparator<T>, Serializable {
private static final long serialVersionUID = 4374092139857L;
+
private final Comparator<T> comparator;
ReverseComparatorWithComparator(Comparator<T> comparator) {
@@ -199,1512 +204,1534 @@
}
}
- private static final class SingletonSet<E> extends AbstractSet<E> implements
- Serializable {
- private static final long serialVersionUID = 3193687207550431679L;
+ private static final class SingletonSet<E> extends AbstractSet<E> implements
+ Serializable {
+ private static final long serialVersionUID = 3193687207550431679L;
- final E element;
+ final E element;
- SingletonSet(E object) {
- element = object;
- }
+ SingletonSet(E object) {
+ element = object;
+ }
- @Override
+ @Override
public boolean contains(Object object) {
- return element == null ? object == null : element.equals(object);
- }
+ return element == null ? object == null : element.equals(object);
+ }
- @Override
+ @Override
public int size() {
- return 1;
- }
+ return 1;
+ }
- @Override
+ @Override
public Iterator<E> iterator() {
- return new Iterator<E>() {
- boolean hasNext = true;
+ return new Iterator<E>() {
+ boolean hasNext = true;
+
+ public boolean hasNext() {
+ return hasNext;
+ }
- public boolean hasNext() {
- return hasNext;
- }
-
- public E next() {
- if (hasNext) {
- hasNext = false;
- return element;
- }
+ public E next() {
+ if (hasNext) {
+ hasNext = false;
+ return element;
+ }
throw new NoSuchElementException();
- }
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+ }
+
+ private static final class SingletonList<E> extends AbstractList<E>
+ implements Serializable {
+ private static final long serialVersionUID = 3093736618740652951L;
- public void remove() {
- throw new UnsupportedOperationException();
- }
- };
- }
- }
-
- private static final class SingletonList<E> extends AbstractList<E> implements
- Serializable {
- private static final long serialVersionUID = 3093736618740652951L;
-
- final E element;
-
- SingletonList(E object) {
- element = object;
- }
+ final E element;
- @Override
+ SingletonList(E object) {
+ element = object;
+ }
+
+ @Override
public boolean contains(Object object) {
- return element == null ? object == null : element.equals(object);
- }
+ return element == null ? object == null : element.equals(object);
+ }
- @Override
+ @Override
public E get(int location) {
- if (location == 0) {
+ if (location == 0) {
return element;
}
- throw new IndexOutOfBoundsException();
- }
+ throw new IndexOutOfBoundsException();
+ }
- @Override
+ @Override
public int size() {
- return 1;
- }
- }
-
- private static final class SingletonMap<K, V> extends AbstractMap<K, V> implements
- Serializable {
- private static final long serialVersionUID = -6979724477215052911L;
+ return 1;
+ }
+ }
+
+ private static final class SingletonMap<K, V> extends AbstractMap<K, V>
+ implements Serializable {
+ private static final long serialVersionUID = -6979724477215052911L;
+
+ final K k;
- final K k;
final V v;
- SingletonMap(K key, V value) {
- k = key;
- v = value;
- }
+ SingletonMap(K key, V value) {
+ k = key;
+ v = value;
+ }
- @Override
+ @Override
public boolean containsKey(Object key) {
- return k == null ? key == null : k.equals(key);
- }
+ return k == null ? key == null : k.equals(key);
+ }
- @Override
+ @Override
public boolean containsValue(Object value) {
- return v == null ? value == null : v.equals(value);
- }
+ return v == null ? value == null : v.equals(value);
+ }
- @Override
+ @Override
public V get(Object key) {
- if (containsKey(key)) {
+ if (containsKey(key)) {
return v;
}
- return null;
- }
+ return null;
+ }
- @Override
+ @Override
public int size() {
- return 1;
- }
+ return 1;
+ }
- @Override
+ @Override
public Set<Map.Entry<K, V>> entrySet() {
- return new AbstractSet<Map.Entry<K, V>>() {
- @Override
+ return new AbstractSet<Map.Entry<K, V>>() {
+ @Override
public boolean contains(Object object) {
- if (object instanceof Map.Entry) {
- Map.Entry<?, ?> entry = (Map.Entry) object;
- return containsKey(entry.getKey())
- && containsValue(entry.getValue());
- }
- return false;
- }
+ if (object instanceof Map.Entry) {
+ Map.Entry<?, ?> entry = (Map.Entry<?, ?>) object;
+ return containsKey(entry.getKey())
+ && containsValue(entry.getValue());
+ }
+ return false;
+ }
- @Override
+ @Override
public int size() {
- return 1;
- }
+ return 1;
+ }
- @Override
+ @Override
public Iterator<Map.Entry<K, V>> iterator() {
- return new Iterator<Map.Entry<K, V>>() {
- boolean hasNext = true;
+ return new Iterator<Map.Entry<K, V>>() {
+ boolean hasNext = true;
- public boolean hasNext() {
- return hasNext;
- }
-
- public Map.Entry<K, V> next() {
- if (hasNext) {
- hasNext = false;
- return new Map.Entry<K, V>() {
- @Override
+ public boolean hasNext() {
+ return hasNext;
+ }
+
+ public Map.Entry<K, V> next() {
+ if (hasNext) {
+ hasNext = false;
+ return new Map.Entry<K, V>() {
+ @Override
public boolean equals(Object object) {
- return contains(object);
- }
+ return contains(object);
+ }
- public K getKey() {
- return k;
- }
-
- public V getValue() {
- return v;
- }
+ public K getKey() {
+ return k;
+ }
+
+ public V getValue() {
+ return v;
+ }
- @Override
+ @Override
public int hashCode() {
- return (k == null ? 0 : k.hashCode())
- ^ (v == null ? 0 : v.hashCode());
- }
-
- public V setValue(V value) {
- throw new UnsupportedOperationException();
- }
- };
- }
+ return (k == null ? 0 : k.hashCode())
+ ^ (v == null ? 0 : v.hashCode());
+ }
+
+ public V setValue(V value) {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
throw new NoSuchElementException();
- }
+ }
- public void remove() {
- throw new UnsupportedOperationException();
- }
- };
- }
- };
- }
- }
-
- static class SynchronizedCollection<E> implements Collection<E>, Serializable {
- private static final long serialVersionUID = 3053995032091335093L;
-
- final Collection<E> c;
- final Object mutex;
-
- SynchronizedCollection(Collection<E> collection) {
- c = collection;
- mutex = this;
- }
-
- SynchronizedCollection(Collection<E> collection, Object mutex) {
- c = collection;
- this.mutex = mutex;
- }
-
- public boolean add(E object) {
- synchronized (mutex) {
- return c.add(object);
- }
- }
-
- public boolean addAll(Collection<? extends E> collection) {
- synchronized (mutex) {
- return c.addAll(collection);
- }
- }
-
- public void clear() {
- synchronized (mutex) {
- c.clear();
- }
- }
-
- public boolean contains(Object object) {
- synchronized (mutex) {
- return c.contains(object);
- }
- }
-
- public boolean containsAll(Collection<?> collection) {
- synchronized (mutex) {
- return c.containsAll(collection);
- }
- }
-
- public boolean isEmpty() {
- synchronized (mutex) {
- return c.isEmpty();
- }
- }
-
- public Iterator<E> iterator() {
- synchronized (mutex) {
- return c.iterator();
- }
- }
-
- public boolean remove(Object object) {
- synchronized (mutex) {
- return c.remove(object);
- }
- }
-
- public boolean removeAll(Collection<?> collection) {
- synchronized (mutex) {
- return c.removeAll(collection);
- }
- }
-
- public boolean retainAll(Collection<?> collection) {
- synchronized (mutex) {
- return c.retainAll(collection);
- }
- }
-
- public int size() {
- synchronized (mutex) {
- return c.size();
- }
- }
-
- public java.lang.Object[] toArray() {
- synchronized (mutex) {
- return c.toArray();
- }
- }
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+ };
+ }
+ }
- @Override
- public String toString() {
- synchronized (mutex) {
- return c.toString();
- }
- }
-
- public <T> T[] toArray(T[] array) {
- synchronized (mutex) {
- return c.toArray(array);
- }
- }
-
- private void writeObject(ObjectOutputStream stream) throws IOException {
- synchronized (mutex) {
- stream.defaultWriteObject();
- }
- }
- }
-
- static class SynchronizedRandomAccessList<E> extends SynchronizedList<E>
- implements RandomAccess {
- private static final long serialVersionUID = 1530674583602358482L;
-
- SynchronizedRandomAccessList(List<E> l) {
- super(l);
- }
-
- SynchronizedRandomAccessList(List<E> l, Object mutex) {
- super(l, mutex);
- }
+ static class SynchronizedCollection<E> implements Collection<E>,
+ Serializable {
+ private static final long serialVersionUID = 3053995032091335093L;
- @Override
- public List<E> subList(int start, int end) {
- synchronized (mutex) {
- return new SynchronizedRandomAccessList<E>(list
- .subList(start, end), mutex);
- }
- }
-
- /**
- * Replaces this SynchronizedRandomAccessList with a SynchronizedList so
- * that JREs before 1.4 can deserialize this object without any
- * problems. This is necessary since RandomAccess API was introduced
- * only in 1.4.
- * <p>
- * @return SynchronizedList
- *
- * @see SynchronizedList#readResolve()
- */
- private Object writeReplace() {
- return new SynchronizedList<E>(list);
- }
- }
-
- static class SynchronizedList<E> extends SynchronizedCollection<E> implements
- List<E> {
- private static final long serialVersionUID = -7754090372962971524L;
-
- final List<E> list;
-
- SynchronizedList(List<E> l) {
- super(l);
- list = l;
- }
-
- SynchronizedList(List<E> l, Object mutex) {
- super(l, mutex);
- list = l;
- }
-
- public void add(int location, E object) {
- synchronized (mutex) {
- list.add(location, object);
- }
- }
-
- public boolean addAll(int location, Collection<? extends E> collection) {
- synchronized (mutex) {
- return list.addAll(location, collection);
- }
- }
+ final Collection<E> c;
- @Override
- public boolean equals(Object object) {
- synchronized (mutex) {
- return list.equals(object);
- }
- }
-
- public E get(int location) {
- synchronized (mutex) {
- return list.get(location);
- }
- }
+ final Object mutex;
- @Override
- public int hashCode() {
- synchronized (mutex) {
- return list.hashCode();
- }
- }
-
- public int indexOf(Object object) {
- synchronized (mutex) {
- return list.indexOf(object);
- }
- }
-
- public int lastIndexOf(Object object) {
- synchronized (mutex) {
- return list.lastIndexOf(object);
- }
- }
-
- public ListIterator<E> listIterator() {
- synchronized (mutex) {
- return list.listIterator();
- }
- }
-
- public ListIterator<E> listIterator(int location) {
- synchronized (mutex) {
- return list.listIterator(location);
- }
- }
-
- public E remove(int location) {
- synchronized (mutex) {
- return list.remove(location);
- }
- }
-
- public E set(int location, E object) {
- synchronized (mutex) {
- return list.set(location, object);
- }
- }
-
- public List<E> subList(int start, int end) {
- synchronized (mutex) {
- return new SynchronizedList<E>(list.subList(start, end), mutex);
- }
- }
-
- private void writeObject(ObjectOutputStream stream) throws IOException {
- synchronized (mutex) {
- stream.defaultWriteObject();
- }
- }
-
- /**
- * Resolves SynchronizedList instances to SynchronizedRandomAccessList
- * instances if the underlying list is a Random Access list.
- * <p>
- * This is necessary since SynchronizedRandomAccessList instances are
- * replaced with SynchronizedList instances during serialization for
- * compliance with JREs before 1.4.
- * <p>
- *
- * @return a SynchronizedList instance if the underlying list implements
- * RandomAccess interface, or this same object if not.
- *
- * @see SynchronizedRandomAccessList#writeReplace()
- */
- private Object readResolve() {
- if (list instanceof RandomAccess) {
- return new SynchronizedRandomAccessList<E>(list, mutex);
+ SynchronizedCollection(Collection<E> collection) {
+ c = collection;
+ mutex = this;
+ }
+
+ SynchronizedCollection(Collection<E> collection, Object mutex) {
+ c = collection;
+ this.mutex = mutex;
+ }
+
+ public boolean add(E object) {
+ synchronized (mutex) {
+ return c.add(object);
}
- return this;
- }
- }
+ }
- static class SynchronizedMap<K, V> implements Map<K, V>, Serializable {
- private static final long serialVersionUID = 1978198479659022715L;
+ public boolean addAll(Collection<? extends E> collection) {
+ synchronized (mutex) {
+ return c.addAll(collection);
+ }
+ }
- private final Map<K, V> m;
- final Object mutex;
+ public void clear() {
+ synchronized (mutex) {
+ c.clear();
+ }
+ }
- SynchronizedMap(Map<K, V> map) {
- m = map;
- mutex = this;
- }
-
- SynchronizedMap(Map<K, V> map, Object mutex) {
- m = map;
- this.mutex = mutex;
- }
-
- public void clear() {
- synchronized (mutex) {
- m.clear();
- }
- }
-
- public boolean containsKey(Object key) {
- synchronized (mutex) {
- return m.containsKey(key);
- }
- }
-
- public boolean containsValue(Object value) {
- synchronized (mutex) {
- return m.containsValue(value);
- }
- }
-
- public Set<Map.Entry<K, V>> entrySet() {
- synchronized (mutex) {
- return new SynchronizedSet<Map.Entry<K, V>>(m.entrySet(), mutex);
- }
- }
+ public boolean contains(Object object) {
+ synchronized (mutex) {
+ return c.contains(object);
+ }
+ }
- @Override
- public boolean equals(Object object) {
- synchronized (mutex) {
- return m.equals(object);
- }
- }
-
- public V get(Object key) {
- synchronized (mutex) {
- return m.get(key);
- }
- }
+ public boolean containsAll(Collection<?> collection) {
+ synchronized (mutex) {
+ return c.containsAll(collection);
+ }
+ }
- @Override
- public int hashCode() {
- synchronized (mutex) {
- return m.hashCode();
- }
- }
-
- public boolean isEmpty() {
- synchronized (mutex) {
- return m.isEmpty();
- }
- }
-
- public Set<K> keySet() {
- synchronized (mutex) {
- return new SynchronizedSet<K>(m.keySet(), mutex);
- }
- }
-
- public V put(K key, V value) {
- synchronized (mutex) {
- return m.put(key, value);
- }
- }
-
- public void putAll(Map<? extends K, ? extends V> map) {
- synchronized (mutex) {
- m.putAll(map);
- }
- }
-
- public V remove(Object key) {
- synchronized (mutex) {
- return m.remove(key);
- }
- }
-
- public int size() {
- synchronized (mutex) {
- return m.size();
- }
- }
-
- public Collection<V> values() {
- synchronized (mutex) {
- return new SynchronizedCollection<V>(m.values(), mutex);
- }
- }
+ public boolean isEmpty() {
+ synchronized (mutex) {
+ return c.isEmpty();
+ }
+ }
- @Override
- public String toString() {
- synchronized (mutex) {
- return m.toString();
- }
- }
-
- private void writeObject(ObjectOutputStream stream) throws IOException {
- synchronized (mutex) {
- stream.defaultWriteObject();
- }
- }
- }
-
- static class SynchronizedSet<E> extends SynchronizedCollection<E> implements Set<E> {
- private static final long serialVersionUID = 487447009682186044L;
-
- SynchronizedSet(Set<E> set) {
- super(set);
- }
-
- SynchronizedSet(Set<E> set, Object mutex) {
- super(set, mutex);
- }
+ public Iterator<E> iterator() {
+ synchronized (mutex) {
+ return c.iterator();
+ }
+ }
- @Override
- public boolean equals(Object object) {
- synchronized (mutex) {
- return c.equals(object);
- }
- }
+ public boolean remove(Object object) {
+ synchronized (mutex) {
+ return c.remove(object);
+ }
+ }
- @Override
- public int hashCode() {
- synchronized (mutex) {
- return c.hashCode();
- }
- }
-
- private void writeObject(ObjectOutputStream stream) throws IOException {
- synchronized (mutex) {
- stream.defaultWriteObject();
- }
- }
- }
-
- static class SynchronizedSortedMap<K, V> extends SynchronizedMap<K, V> implements
- SortedMap<K, V> {
- private static final long serialVersionUID = -8798146769416483793L;
-
- private final SortedMap<K, V> sm;
-
- SynchronizedSortedMap(SortedMap<K, V> map) {
- super(map);
- sm = map;
- }
-
- SynchronizedSortedMap(SortedMap<K, V> map, Object mutex) {
- super(map, mutex);
- sm = map;
- }
-
- public Comparator<? super K> comparator() {
- synchronized (mutex) {
- return sm.comparator();
- }
- }
-
- public K firstKey() {
- synchronized (mutex) {
- return sm.firstKey();
- }
- }
-
- public SortedMap<K, V> headMap(K endKey) {
- synchronized (mutex) {
- return new SynchronizedSortedMap<K, V>(sm.headMap(endKey), mutex);
- }
- }
-
- public K lastKey() {
- synchronized (mutex) {
- return sm.lastKey();
- }
- }
-
- public SortedMap<K, V> subMap(K startKey, K endKey) {
- synchronized (mutex) {
- return new SynchronizedSortedMap<K, V>(sm.subMap(startKey, endKey),
- mutex);
- }
- }
-
- public SortedMap<K, V> tailMap(K startKey) {
- synchronized (mutex) {
- return new SynchronizedSortedMap<K, V>(sm.tailMap(startKey), mutex);
- }
- }
-
- private void writeObject(ObjectOutputStream stream) throws IOException {
- synchronized (mutex) {
- stream.defaultWriteObject();
- }
- }
- }
-
- static class SynchronizedSortedSet<E> extends SynchronizedSet<E> implements
- SortedSet<E> {
- private static final long serialVersionUID = 8695801310862127406L;
-
- private final SortedSet<E> ss;
-
- SynchronizedSortedSet(SortedSet<E> set) {
- super(set);
- ss = set;
- }
-
- SynchronizedSortedSet(SortedSet<E> set, Object mutex) {
- super(set, mutex);
- ss = set;
- }
-
- public Comparator<? super E> comparator() {
- synchronized (mutex) {
- return ss.comparator();
- }
- }
-
- public E first() {
- synchronized (mutex) {
- return ss.first();
- }
- }
-
- public SortedSet<E> headSet(E end) {
- synchronized (mutex) {
- return new SynchronizedSortedSet<E>(ss.headSet(end), mutex);
- }
- }
-
- public E last() {
- synchronized (mutex) {
- return ss.last();
- }
- }
-
- public SortedSet<E> subSet(E start, E end) {
- synchronized (mutex) {
- return new SynchronizedSortedSet<E>(ss.subSet(start, end), mutex);
- }
- }
-
- public SortedSet<E> tailSet(E start) {
- synchronized (mutex) {
- return new SynchronizedSortedSet<E>(ss.tailSet(start), mutex);
- }
- }
-
- private void writeObject(ObjectOutputStream stream) throws IOException {
- synchronized (mutex) {
- stream.defaultWriteObject();
- }
- }
- }
-
- private static class UnmodifiableCollection<E> implements Collection<E>,
- Serializable {
- private static final long serialVersionUID = 1820017752578914078L;
-
- final Collection<E> c;
-
- UnmodifiableCollection(Collection<E> collection) {
- c = collection;
- }
-
- public boolean add(E object) {
- throw new UnsupportedOperationException();
- }
-
- public boolean addAll(Collection<? extends E> collection) {
- throw new UnsupportedOperationException();
- }
-
- public void clear() {
- throw new UnsupportedOperationException();
- }
-
- public boolean contains(Object object) {
- return c.contains(object);
- }
-
- public boolean containsAll(Collection<?> collection) {
- return c.containsAll(collection);
- }
-
- public boolean isEmpty() {
- return c.isEmpty();
- }
-
- public Iterator<E> iterator() {
- return new Iterator<E>() {
- Iterator<E> iterator = c.iterator();
-
- public boolean hasNext() {
- return iterator.hasNext();
- }
-
- public E next() {
- return iterator.next();
- }
-
- public void remove() {
- throw new UnsupportedOperationException();
- }
- };
- }
-
- public boolean remove(Object object) {
- throw new UnsupportedOperationException();
- }
-
- public boolean removeAll(Collection<?> collection) {
- throw new UnsupportedOperationException();
- }
-
- public boolean retainAll(Collection<?> collection) {
- throw new UnsupportedOperationException();
- }
-
- public int size() {
- return c.size();
- }
-
- public Object[] toArray() {
- return c.toArray();
- }
+ public boolean removeAll(Collection<?> collection) {
+ synchronized (mutex) {
+ return c.removeAll(collection);
+ }
+ }
- public <T> T[] toArray(T[] array) {
- return c.toArray(array);
+ public boolean retainAll(Collection<?> collection) {
+ synchronized (mutex) {
+ return c.retainAll(collection);
+ }
+ }
+
+ public int size() {
+ synchronized (mutex) {
+ return c.size();
+ }
+ }
+
+ public java.lang.Object[] toArray() {
+ synchronized (mutex) {
+ return c.toArray();
+ }
}
@Override
public String toString() {
- return c.toString();
+ synchronized (mutex) {
+ return c.toString();
+ }
}
- }
- private static class UnmodifiableRandomAccessList<E> extends UnmodifiableList<E>
- implements RandomAccess {
- private static final long serialVersionUID = -2542308836966382001L;
+ public <T> T[] toArray(T[] array) {
+ synchronized (mutex) {
+ return c.toArray(array);
+ }
+ }
- UnmodifiableRandomAccessList(List<E> l) {
- super(l);
- }
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ synchronized (mutex) {
+ stream.defaultWriteObject();
+ }
+ }
+ }
- @Override
- public List<E> subList(int start, int end) {
- return new UnmodifiableRandomAccessList<E>(list.subList(start, end));
- }
+ static class SynchronizedRandomAccessList<E> extends SynchronizedList<E>
+ implements RandomAccess {
+ private static final long serialVersionUID = 1530674583602358482L;
- /**
- * Replaces this UnmodifiableRandomAccessList with an UnmodifiableList
- * so that JREs before 1.4 can deserialize this object without any
- * problems. This is necessary since RandomAccess API was introduced
- * only in 1.4.
- * <p>
- * @return UnmodifiableList
- *
- * @see UnmodifiableList#readResolve()
- */
- private Object writeReplace() {
- return new UnmodifiableList<E>(list);
- }
- }
-
- private static class UnmodifiableList<E> extends UnmodifiableCollection<E>
- implements List<E> {
- private static final long serialVersionUID = -283967356065247728L;
-
- final List<E> list;
-
- UnmodifiableList(List<E> l) {
- super(l);
- list = l;
- }
-
- public void add(int location, E object) {
- throw new UnsupportedOperationException();
- }
-
- public boolean addAll(int location, Collection<? extends E> collection) {
- throw new UnsupportedOperationException();
- }
+ SynchronizedRandomAccessList(List<E> l) {
+ super(l);
+ }
- @Override
- public boolean equals(Object object) {
- return list.equals(object);
- }
+ SynchronizedRandomAccessList(List<E> l, Object mutex) {
+ super(l, mutex);
+ }
- public E get(int location) {
- return list.get(location);
- }
+ @Override
+ public List<E> subList(int start, int end) {
+ synchronized (mutex) {
+ return new SynchronizedRandomAccessList<E>(list.subList(start,
+ end), mutex);
+ }
+ }
- @Override
- public int hashCode() {
- return list.hashCode();
- }
+ /**
+ * Replaces this SynchronizedRandomAccessList with a SynchronizedList so
+ * that JREs before 1.4 can deserialize this object without any
+ * problems. This is necessary since RandomAccess API was introduced
+ * only in 1.4.
+ * <p>
+ *
+ * @return SynchronizedList
+ *
+ * @see SynchronizedList#readResolve()
+ */
+ private Object writeReplace() {
+ return new SynchronizedList<E>(list);
+ }
+ }
- public int indexOf(Object object) {
- return list.indexOf(object);
- }
-
- public int lastIndexOf(Object object) {
- return list.lastIndexOf(object);
- }
-
- public ListIterator<E> listIterator() {
- return listIterator(0);
- }
-
- public ListIterator<E> listIterator(final int location) {
- return new ListIterator<E>() {
- ListIterator<E> iterator = list.listIterator(location);
-
- public void add(E object) {
- throw new UnsupportedOperationException();
- }
-
- public boolean hasNext() {
- return iterator.hasNext();
- }
-
- public boolean hasPrevious() {
- return iterator.hasPrevious();
- }
-
- public E next() {
- return iterator.next();
- }
-
- public int nextIndex() {
- return iterator.nextIndex();
- }
-
- public E previous() {
- return iterator.previous();
- }
-
- public int previousIndex() {
- return iterator.previousIndex();
- }
-
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- public void set(E object) {
- throw new UnsupportedOperationException();
- }
- };
- }
-
- public E remove(int location) {
- throw new UnsupportedOperationException();
- }
-
- public E set(int location, E object) {
- throw new UnsupportedOperationException();
- }
-
- public List<E> subList(int start, int end) {
- return new UnmodifiableList<E>(list.subList(start, end));
- }
-
- /**
- * Resolves UnmodifiableList instances to UnmodifiableRandomAccessList
- * instances if the underlying list is a Random Access list.
- * <p>
- * This is necessary since UnmodifiableRandomAccessList instances are
- * replaced with UnmodifiableList instances during serialization for
- * compliance with JREs before 1.4.
- * <p>
- *
- * @return an UnmodifiableList instance if the underlying list
- * implements RandomAccess interface, or this same object if
- * not.
- *
- * @see UnmodifiableRandomAccessList#writeReplace()
- */
- private Object readResolve() {
- if (list instanceof RandomAccess) {
- return new UnmodifiableRandomAccessList<E>(list);
- }
- return this;
- }
- }
+ static class SynchronizedList<E> extends SynchronizedCollection<E>
+ implements List<E> {
+ private static final long serialVersionUID = -7754090372962971524L;
- private static class UnmodifiableMap<K, V> implements Map<K, V>, Serializable {
- private static final long serialVersionUID = -1034234728574286014L;
+ final List<E> list;
- private final Map<K, V> m;
+ SynchronizedList(List<E> l) {
+ super(l);
+ list = l;
+ }
- private static class UnmodifiableEntrySet<K, V> extends UnmodifiableSet<Map.Entry<K, V>> {
- private static final long serialVersionUID = 7854390611657943733L;
+ SynchronizedList(List<E> l, Object mutex) {
+ super(l, mutex);
+ list = l;
+ }
- private static class UnmodifiableMapEntry<K, V> implements Map.Entry<K, V> {
- Map.Entry<K, V> mapEntry;
+ public void add(int location, E object) {
+ synchronized (mutex) {
+ list.add(location, object);
+ }
+ }
- UnmodifiableMapEntry(Map.Entry<K, V> entry) {
- mapEntry = entry;
- }
+ public boolean addAll(int location, Collection<? extends E> collection) {
+ synchronized (mutex) {
+ return list.addAll(location, collection);
+ }
+ }
- @Override
- public boolean equals(Object object) {
- return mapEntry.equals(object);
- }
+ @Override
+ public boolean equals(Object object) {
+ synchronized (mutex) {
+ return list.equals(object);
+ }
+ }
- public K getKey() {
- return mapEntry.getKey();
- }
-
- public V getValue() {
- return mapEntry.getValue();
- }
+ public E get(int location) {
+ synchronized (mutex) {
+ return list.get(location);
+ }
+ }
- @Override
- public int hashCode() {
- return mapEntry.hashCode();
- }
+ @Override
+ public int hashCode() {
+ synchronized (mutex) {
+ return list.hashCode();
+ }
+ }
- public V setValue(V object) {
- throw new UnsupportedOperationException();
- }
+ public int indexOf(Object object) {
+ synchronized (mutex) {
+ return list.indexOf(object);
+ }
+ }
- @Override
- public String toString() {
- return mapEntry.toString();
- }
- }
-
- UnmodifiableEntrySet(Set<Map.Entry<K, V>> set) {
- super(set);
- }
+ public int lastIndexOf(Object object) {
+ synchronized (mutex) {
+ return list.lastIndexOf(object);
+ }
+ }
- @Override
- public Iterator<Map.Entry<K, V>> iterator() {
- return new Iterator<Map.Entry<K, V>>() {
- Iterator<Map.Entry<K, V>> iterator = c.iterator();
+ public ListIterator<E> listIterator() {
+ synchronized (mutex) {
+ return list.listIterator();
+ }
+ }
- public boolean hasNext() {
- return iterator.hasNext();
- }
-
- public Map.Entry<K, V> next() {
- return new UnmodifiableMapEntry<K, V>(iterator.next());
- }
-
- public void remove() {
- throw new UnsupportedOperationException();
- }
- };
- }
+ public ListIterator<E> listIterator(int location) {
+ synchronized (mutex) {
+ return list.listIterator(location);
+ }
+ }
- @Override
- public Object[] toArray() {
- int length = c.size();
- Object[] result = new Object[length];
- Iterator<?> it = iterator();
- for (int i = length; --i >= 0;) {
- result[i] = it.next();
- }
- return result;
- }
+ public E remove(int location) {
+ synchronized (mutex) {
+ return list.remove(location);
+ }
+ }
- @Override
- @SuppressWarnings("unchecked")
- public <T> T[] toArray(T[] contents) {
- int size = c.size(), index = 0;
- Iterator<Map.Entry<K, V>> it = iterator();
- if (size > contents.length) {
- Class<?> ct = contents.getClass().getComponentType();
- contents = (T[])Array.newInstance(ct, size);
- }
- while (index < size) {
- contents[index++] = (T)it.next();
- }
- if (index < contents.length) {
- contents[index] = null;
- }
- return contents;
- }
- }
+ public E set(int location, E object) {
+ synchronized (mutex) {
+ return list.set(location, object);
+ }
+ }
- UnmodifiableMap(Map<K, V> map) {
- m = map;
- }
+ public List<E> subList(int start, int end) {
+ synchronized (mutex) {
+ return new SynchronizedList<E>(list.subList(start, end), mutex);
+ }
+ }
- public void clear() {
- throw new UnsupportedOperationException();
- }
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ synchronized (mutex) {
+ stream.defaultWriteObject();
+ }
+ }
- public boolean containsKey(Object key) {
- return m.containsKey(key);
- }
+ /**
+ * Resolves SynchronizedList instances to SynchronizedRandomAccessList
+ * instances if the underlying list is a Random Access list.
+ * <p>
+ * This is necessary since SynchronizedRandomAccessList instances are
+ * replaced with SynchronizedList instances during serialization for
+ * compliance with JREs before 1.4.
+ * <p>
+ *
+ * @return a SynchronizedList instance if the underlying list implements
+ * RandomAccess interface, or this same object if not.
+ *
+ * @see SynchronizedRandomAccessList#writeReplace()
+ */
+ private Object readResolve() {
+ if (list instanceof RandomAccess) {
+ return new SynchronizedRandomAccessList<E>(list, mutex);
+ }
+ return this;
+ }
+ }
- public boolean containsValue(Object value) {
- return m.containsValue(value);
- }
+ static class SynchronizedMap<K, V> implements Map<K, V>, Serializable {
+ private static final long serialVersionUID = 1978198479659022715L;
- public Set<Map.Entry<K, V>> entrySet() {
- return new UnmodifiableEntrySet<K, V>(m.entrySet());
- }
+ private final Map<K, V> m;
- @Override
- public boolean equals(Object object) {
- return m.equals(object);
- }
+ final Object mutex;
- public V get(Object key) {
- return m.get(key);
- }
+ SynchronizedMap(Map<K, V> map) {
+ m = map;
+ mutex = this;
+ }
- @Override
- public int hashCode() {
- return m.hashCode();
- }
+ SynchronizedMap(Map<K, V> map, Object mutex) {
+ m = map;
+ this.mutex = mutex;
+ }
- public boolean isEmpty() {
- return m.isEmpty();
- }
-
- public Set<K> keySet() {
- return new UnmodifiableSet<K>(m.keySet());
- }
-
- public V put(K key, V value) {
- throw new UnsupportedOperationException();
- }
-
- public void putAll(Map<? extends K, ? extends V> map) {
- throw new UnsupportedOperationException();
- }
-
- public V remove(Object key) {
- throw new UnsupportedOperationException();
- }
-
- public int size() {
- return m.size();
- }
-
- public Collection<V> values() {
- return new UnmodifiableCollection<V>(m.values());
- }
+ public void clear() {
+ synchronized (mutex) {
+ m.clear();
+ }
+ }
- @Override
- public String toString() {
- return m.toString();
- }
- }
-
- private static class UnmodifiableSet<E> extends UnmodifiableCollection<E>
- implements Set<E> {
- private static final long serialVersionUID = -9215047833775013803L;
-
- UnmodifiableSet(Set<E> set) {
- super(set);
- }
+ public boolean containsKey(Object key) {
+ synchronized (mutex) {
+ return m.containsKey(key);
+ }
+ }
- @Override
- public boolean equals(Object object) {
- return c.equals(object);
- }
+ public boolean containsValue(Object value) {
+ synchronized (mutex) {
+ return m.containsValue(value);
+ }
+ }
- @Override
- public int hashCode() {
- return c.hashCode();
- }
- }
-
- private static class UnmodifiableSortedMap<K, V> extends UnmodifiableMap<K, V>
- implements SortedMap<K, V> {
- private static final long serialVersionUID = -8806743815996713206L;
-
- private final SortedMap<K, V> sm;
-
- UnmodifiableSortedMap(SortedMap<K, V> map) {
- super(map);
- sm = map;
- }
-
- public Comparator<? super K> comparator() {
- return sm.comparator();
- }
-
- public K firstKey() {
- return sm.firstKey();
- }
-
- public SortedMap<K, V> headMap(K before) {
- return new UnmodifiableSortedMap<K, V>(sm.headMap(before));
- }
-
- public K lastKey() {
- return sm.lastKey();
- }
-
- public SortedMap<K, V> subMap(K start, K end) {
- return new UnmodifiableSortedMap<K, V>(sm.subMap(start, end));
- }
-
- public SortedMap<K, V> tailMap(K after) {
- return new UnmodifiableSortedMap<K, V>(sm.tailMap(after));
- }
- }
-
- private static class UnmodifiableSortedSet<E> extends UnmodifiableSet<E>
- implements SortedSet<E> {
- private static final long serialVersionUID = -4929149591599911165L;
-
- private final SortedSet<E> ss;
-
- UnmodifiableSortedSet(SortedSet<E> set) {
- super(set);
- ss = set;
- }
-
- public Comparator<? super E> comparator() {
- return ss.comparator();
- }
-
- public E first() {
- return ss.first();
- }
-
- public SortedSet<E> headSet(E before) {
- return new UnmodifiableSortedSet<E>(ss.headSet(before));
- }
-
- public E last() {
- return ss.last();
- }
-
- public SortedSet<E> subSet(E start, E end) {
- return new UnmodifiableSortedSet<E>(ss.subSet(start, end));
- }
-
- public SortedSet<E> tailSet(E after) {
- return new UnmodifiableSortedSet<E>(ss.tailSet(after));
- }
- }
-
- private Collections() {
- /*empty*/
- }
-
- /**
- * Performs a binary search for the specified element in the specified
- * sorted List.
- *
- * @param list
- * the sorted List to search
- * @param object
- * the element to find
- * @return the non-negative index of the element, or a negative index which
- * is the -index - 1 where the element would be inserted
- *
- * @throws ClassCastException
- * when an element in the List or the search element does not
- * implement Comparable, or cannot be compared to each other
- */
- @SuppressWarnings("unchecked")
- public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T object) {
- if (list == null) {
- throw new NullPointerException();
+ public Set<Map.Entry<K, V>> entrySet() {
+ synchronized (mutex) {
+ return new SynchronizedSet<Map.Entry<K, V>>(m.entrySet(), mutex);
+ }
}
- if(list.isEmpty()){
- return -1;
- }
-
- Comparable<T> key = (Comparable<T>)object;
- if (!(list instanceof RandomAccess)) {
- ListIterator<T> it = (ListIterator<T>)list.listIterator();
- while (it.hasNext()) {
- int result;
- if ((result = key.compareTo(it.next())) <= 0) {
- if (result == 0) {
- return it.previousIndex();
+
+ @Override
+ public boolean equals(Object object) {
+ synchronized (mutex) {
+ return m.equals(object);
+ }
+ }
+
+ public V get(Object key) {
+ synchronized (mutex) {
+ return m.get(key);
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ synchronized (mutex) {
+ return m.hashCode();
+ }
+ }
+
+ public boolean isEmpty() {
+ synchronized (mutex) {
+ return m.isEmpty();
+ }
+ }
+
+ public Set<K> keySet() {
+ synchronized (mutex) {
+ return new SynchronizedSet<K>(m.keySet(), mutex);
+ }
+ }
+
+ public V put(K key, V value) {
+ synchronized (mutex) {
+ return m.put(key, value);
+ }
+ }
+
+ public void putAll(Map<? extends K, ? extends V> map) {
+ synchronized (mutex) {
+ m.putAll(map);
+ }
+ }
+
+ public V remove(Object key) {
+ synchronized (mutex) {
+ return m.remove(key);
+ }
+ }
+
+ public int size() {
+ synchronized (mutex) {
+ return m.size();
+ }
+ }
+
+ public Collection<V> values() {
+ synchronized (mutex) {
+ return new SynchronizedCollection<V>(m.values(), mutex);
+ }
+ }
+
+ @Override
+ public String toString() {
+ synchronized (mutex) {
+ return m.toString();
+ }
+ }
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ synchronized (mutex) {
+ stream.defaultWriteObject();
+ }
+ }
+ }
+
+ static class SynchronizedSet<E> extends SynchronizedCollection<E> implements
+ Set<E> {
+ private static final long serialVersionUID = 487447009682186044L;
+
+ SynchronizedSet(Set<E> set) {
+ super(set);
+ }
+
+ SynchronizedSet(Set<E> set, Object mutex) {
+ super(set, mutex);
+ }
+
+ @Override
+ public boolean equals(Object object) {
+ synchronized (mutex) {
+ return c.equals(object);
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ synchronized (mutex) {
+ return c.hashCode();
+ }
+ }
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ synchronized (mutex) {
+ stream.defaultWriteObject();
+ }
+ }
+ }
+
+ static class SynchronizedSortedMap<K, V> extends SynchronizedMap<K, V>
+ implements SortedMap<K, V> {
+ private static final long serialVersionUID = -8798146769416483793L;
+
+ private final SortedMap<K, V> sm;
+
+ SynchronizedSortedMap(SortedMap<K, V> map) {
+ super(map);
+ sm = map;
+ }
+
+ SynchronizedSortedMap(SortedMap<K, V> map, Object mutex) {
+ super(map, mutex);
+ sm = map;
+ }
+
+ public Comparator<? super K> comparator() {
+ synchronized (mutex) {
+ return sm.comparator();
+ }
+ }
+
+ public K firstKey() {
+ synchronized (mutex) {
+ return sm.firstKey();
+ }
+ }
+
+ public SortedMap<K, V> headMap(K endKey) {
+ synchronized (mutex) {
+ return new SynchronizedSortedMap<K, V>(sm.headMap(endKey),
+ mutex);
+ }
+ }
+
+ public K lastKey() {
+ synchronized (mutex) {
+ return sm.lastKey();
+ }
+ }
+
+ public SortedMap<K, V> subMap(K startKey, K endKey) {
+ synchronized (mutex) {
+ return new SynchronizedSortedMap<K, V>(sm.subMap(startKey,
+ endKey), mutex);
+ }
+ }
+
+ public SortedMap<K, V> tailMap(K startKey) {
+ synchronized (mutex) {
+ return new SynchronizedSortedMap<K, V>(sm.tailMap(startKey),
+ mutex);
+ }
+ }
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ synchronized (mutex) {
+ stream.defaultWriteObject();
+ }
+ }
+ }
+
+ static class SynchronizedSortedSet<E> extends SynchronizedSet<E> implements
+ SortedSet<E> {
+ private static final long serialVersionUID = 8695801310862127406L;
+
+ private final SortedSet<E> ss;
+
+ SynchronizedSortedSet(SortedSet<E> set) {
+ super(set);
+ ss = set;
+ }
+
+ SynchronizedSortedSet(SortedSet<E> set, Object mutex) {
+ super(set, mutex);
+ ss = set;
+ }
+
+ public Comparator<? super E> comparator() {
+ synchronized (mutex) {
+ return ss.comparator();
+ }
+ }
+
+ public E first() {
+ synchronized (mutex) {
+ return ss.first();
+ }
+ }
+
+ public SortedSet<E> headSet(E end) {
+ synchronized (mutex) {
+ return new SynchronizedSortedSet<E>(ss.headSet(end), mutex);
+ }
+ }
+
+ public E last() {
+ synchronized (mutex) {
+ return ss.last();
+ }
+ }
+
+ public SortedSet<E> subSet(E start, E end) {
+ synchronized (mutex) {
+ return new SynchronizedSortedSet<E>(ss.subSet(start, end),
+ mutex);
+ }
+ }
+
+ public SortedSet<E> tailSet(E start) {
+ synchronized (mutex) {
+ return new SynchronizedSortedSet<E>(ss.tailSet(start), mutex);
+ }
+ }
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ synchronized (mutex) {
+ stream.defaultWriteObject();
+ }
+ }
+ }
+
+ private static class UnmodifiableCollection<E> implements Collection<E>,
+ Serializable {
+ private static final long serialVersionUID = 1820017752578914078L;
+
+ final Collection<E> c;
+
+ UnmodifiableCollection(Collection<E> collection) {
+ c = collection;
+ }
+
+ public boolean add(E object) {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean addAll(Collection<? extends E> collection) {
+ throw new UnsupportedOperationException();
+ }
+
+ public void clear() {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean contains(Object object) {
+ return c.contains(object);
+ }
+
+ public boolean containsAll(Collection<?> collection) {
+ return c.containsAll(collection);
+ }
+
+ public boolean isEmpty() {
+ return c.isEmpty();
+ }
+
+ public Iterator<E> iterator() {
+ return new Iterator<E>() {
+ Iterator<E> iterator = c.iterator();
+
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ public E next() {
+ return iterator.next();
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+
+ public boolean remove(Object object) {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean removeAll(Collection<?> collection) {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean retainAll(Collection<?> collection) {
+ throw new UnsupportedOperationException();
+ }
+
+ public int size() {
+ return c.size();
+ }
+
+ public Object[] toArray() {
+ return c.toArray();
+ }
+
+ public <T> T[] toArray(T[] array) {
+ return c.toArray(array);
+ }
+
+ @Override
+ public String toString() {
+ return c.toString();
+ }
+ }
+
+ private static class UnmodifiableRandomAccessList<E> extends
+ UnmodifiableList<E> implements RandomAccess {
+ private static final long serialVersionUID = -2542308836966382001L;
+
+ UnmodifiableRandomAccessList(List<E> l) {
+ super(l);
+ }
+
+ @Override
+ public List<E> subList(int start, int end) {
+ return new UnmodifiableRandomAccessList<E>(list.subList(start, end));
+ }
+
+ /**
+ * Replaces this UnmodifiableRandomAccessList with an UnmodifiableList
+ * so that JREs before 1.4 can deserialize this object without any
+ * problems. This is necessary since RandomAccess API was introduced
+ * only in 1.4.
+ * <p>
+ *
+ * @return UnmodifiableList
+ *
+ * @see UnmodifiableList#readResolve()
+ */
+ private Object writeReplace() {
+ return new UnmodifiableList<E>(list);
+ }
+ }
+
+ private static class UnmodifiableList<E> extends UnmodifiableCollection<E>
+ implements List<E> {
+ private static final long serialVersionUID = -283967356065247728L;
+
+ final List<E> list;
+
+ UnmodifiableList(List<E> l) {
+ super(l);
+ list = l;
+ }
+
+ public void add(int location, E object) {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean addAll(int location, Collection<? extends E> collection) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean equals(Object object) {
+ return list.equals(object);
+ }
+
+ public E get(int location) {
+ return list.get(location);
+ }
+
+ @Override
+ public int hashCode() {
+ return list.hashCode();
+ }
+
+ public int indexOf(Object object) {
+ return list.indexOf(object);
+ }
+
+ public int lastIndexOf(Object object) {
+ return list.lastIndexOf(object);
+ }
+
+ public ListIterator<E> listIterator() {
+ return listIterator(0);
+ }
+
+ public ListIterator<E> listIterator(final int location) {
+ return new ListIterator<E>() {
+ ListIterator<E> iterator = list.listIterator(location);
+
+ public void add(E object) {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ public boolean hasPrevious() {
+ return iterator.hasPrevious();
+ }
+
+ public E next() {
+ return iterator.next();
+ }
+
+ public int nextIndex() {
+ return iterator.nextIndex();
+ }
+
+ public E previous() {
+ return iterator.previous();
+ }
+
+ public int previousIndex() {
+ return iterator.previousIndex();
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ public void set(E object) {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+
+ public E remove(int location) {
+ throw new UnsupportedOperationException();
+ }
+
+ public E set(int location, E object) {
+ throw new UnsupportedOperationException();
+ }
+
+ public List<E> subList(int start, int end) {
+ return new UnmodifiableList<E>(list.subList(start, end));
+ }
+
+ /**
+ * Resolves UnmodifiableList instances to UnmodifiableRandomAccessList
+ * instances if the underlying list is a Random Access list.
+ * <p>
+ * This is necessary since UnmodifiableRandomAccessList instances are
+ * replaced with UnmodifiableList instances during serialization for
+ * compliance with JREs before 1.4.
+ * <p>
+ *
+ * @return an UnmodifiableList instance if the underlying list
+ * implements RandomAccess interface, or this same object if
+ * not.
+ *
+ * @see UnmodifiableRandomAccessList#writeReplace()
+ */
+ private Object readResolve() {
+ if (list instanceof RandomAccess) {
+ return new UnmodifiableRandomAccessList<E>(list);
+ }
+ return this;
+ }
+ }
+
+ private static class UnmodifiableMap<K, V> implements Map<K, V>,
+ Serializable {
+ private static final long serialVersionUID = -1034234728574286014L;
+
+ private final Map<K, V> m;
+
+ private static class UnmodifiableEntrySet<K, V> extends
+ UnmodifiableSet<Map.Entry<K, V>> {
+ private static final long serialVersionUID = 7854390611657943733L;
+
+ private static class UnmodifiableMapEntry<K, V> implements
+ Map.Entry<K, V> {
+ Map.Entry<K, V> mapEntry;
+
+ UnmodifiableMapEntry(Map.Entry<K, V> entry) {
+ mapEntry = entry;
+ }
+
+ @Override
+ public boolean equals(Object object) {
+ return mapEntry.equals(object);
+ }
+
+ public K getKey() {
+ return mapEntry.getKey();
+ }
+
+ public V getValue() {
+ return mapEntry.getValue();
+ }
+
+ @Override
+ public int hashCode() {
+ return mapEntry.hashCode();
+ }
+
+ public V setValue(V object) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public String toString() {
+ return mapEntry.toString();
+ }
+ }
+
+ UnmodifiableEntrySet(Set<Map.Entry<K, V>> set) {
+ super(set);
+ }
+
+ @Override
+ public Iterator<Map.Entry<K, V>> iterator() {
+ return new Iterator<Map.Entry<K, V>>() {
+ Iterator<Map.Entry<K, V>> iterator = c.iterator();
+
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ public Map.Entry<K, V> next() {
+ return new UnmodifiableMapEntry<K, V>(iterator.next());
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+
+ @Override
+ public Object[] toArray() {
+ int length = c.size();
+ Object[] result = new Object[length];
+ Iterator<?> it = iterator();
+ for (int i = length; --i >= 0;) {
+ result[i] = it.next();
+ }
+ return result;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <T> T[] toArray(T[] contents) {
+ int size = c.size(), index = 0;
+ Iterator<Map.Entry<K, V>> it = iterator();
+ if (size > contents.length) {
+ Class<?> ct = contents.getClass().getComponentType();
+ contents = (T[]) Array.newInstance(ct, size);
+ }
+ while (index < size) {
+ contents[index++] = (T) it.next();
+ }
+ if (index < contents.length) {
+ contents[index] = null;
+ }
+ return contents;
+ }
+ }
+
+ UnmodifiableMap(Map<K, V> map) {
+ m = map;
+ }
+
+ public void clear() {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean containsKey(Object key) {
+ return m.containsKey(key);
+ }
+
+ public boolean containsValue(Object value) {
+ return m.containsValue(value);
+ }
+
+ public Set<Map.Entry<K, V>> entrySet() {
+ return new UnmodifiableEntrySet<K, V>(m.entrySet());
+ }
+
+ @Override
+ public boolean equals(Object object) {
+ return m.equals(object);
+ }
+
+ public V get(Object key) {
+ return m.get(key);
+ }
+
+ @Override
+ public int hashCode() {
+ return m.hashCode();
+ }
+
+ public boolean isEmpty() {
+ return m.isEmpty();
+ }
+
+ public Set<K> keySet() {
+ return new UnmodifiableSet<K>(m.keySet());
+ }
+
+ public V put(K key, V value) {
+ throw new UnsupportedOperationException();
+ }
+
+ public void putAll(Map<? extends K, ? extends V> map) {
+ throw new UnsupportedOperationException();
+ }
+
+ public V remove(Object key) {
+ throw new UnsupportedOperationException();
+ }
+
+ public int size() {
+ return m.size();
+ }
+
+ public Collection<V> values() {
+ return new UnmodifiableCollection<V>(m.values());
+ }
+
+ @Override
+ public String toString() {
+ return m.toString();
+ }
+ }
+
+ private static class UnmodifiableSet<E> extends UnmodifiableCollection<E>
+ implements Set<E> {
+ private static final long serialVersionUID = -9215047833775013803L;
+
+ UnmodifiableSet(Set<E> set) {
+ super(set);
+ }
+
+ @Override
+ public boolean equals(Object object) {
+ return c.equals(object);
+ }
+
+ @Override
+ public int hashCode() {
+ return c.hashCode();
+ }
+ }
+
+ private static class UnmodifiableSortedMap<K, V> extends
+ UnmodifiableMap<K, V> implements SortedMap<K, V> {
+ private static final long serialVersionUID = -8806743815996713206L;
+
+ private final SortedMap<K, V> sm;
+
+ UnmodifiableSortedMap(SortedMap<K, V> map) {
+ super(map);
+ sm = map;
+ }
+
+ public Comparator<? super K> comparator() {
+ return sm.comparator();
+ }
+
+ public K firstKey() {
+ return sm.firstKey();
+ }
+
+ public SortedMap<K, V> headMap(K before) {
+ return new UnmodifiableSortedMap<K, V>(sm.headMap(before));
+ }
+
+ public K lastKey() {
+ return sm.lastKey();
+ }
+
+ public SortedMap<K, V> subMap(K start, K end) {
+ return new UnmodifiableSortedMap<K, V>(sm.subMap(start, end));
+ }
+
+ public SortedMap<K, V> tailMap(K after) {
+ return new UnmodifiableSortedMap<K, V>(sm.tailMap(after));
+ }
+ }
+
+ private static class UnmodifiableSortedSet<E> extends UnmodifiableSet<E>
+ implements SortedSet<E> {
+ private static final long serialVersionUID = -4929149591599911165L;
+
+ private final SortedSet<E> ss;
+
+ UnmodifiableSortedSet(SortedSet<E> set) {
+ super(set);
+ ss = set;
+ }
+
+ public Comparator<? super E> comparator() {
+ return ss.comparator();
+ }
+
+ public E first() {
+ return ss.first();
+ }
+
+ public SortedSet<E> headSet(E before) {
+ return new UnmodifiableSortedSet<E>(ss.headSet(before));
+ }
+
+ public E last() {
+ return ss.last();
+ }
+
+ public SortedSet<E> subSet(E start, E end) {
+ return new UnmodifiableSortedSet<E>(ss.subSet(start, end));
+ }
+
+ public SortedSet<E> tailSet(E after) {
+ return new UnmodifiableSortedSet<E>(ss.tailSet(after));
+ }
+ }
+
+ private Collections() {
+ /* empty */
+ }
+
+ /**
+ * Performs a binary search for the specified element in the specified
+ * sorted List.
+ *
+ * @param list
+ * the sorted List to search
+ * @param object
+ * the element to find
+ * @return the non-negative index of the element, or a negative index which
+ * is the -index - 1 where the element would be inserted
+ *
+ * @throws ClassCastException
+ * when an element in the List or the search element does not
+ * implement Comparable, or cannot be compared to each other
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> int binarySearch(
+ List<? extends Comparable<? super T>> list, T object) {
+ if (list == null) {
+ throw new NullPointerException();
+ }
+ if (list.isEmpty()) {
+ return -1;
+ }
+
+ Comparable<T> key = (Comparable<T>) object;
+ if (!(list instanceof RandomAccess)) {
+ ListIterator<T> it = (ListIterator<T>) list.listIterator();
+ while (it.hasNext()) {
+ int result;
+ if ((result = key.compareTo(it.next())) <= 0) {
+ if (result == 0) {
+ return it.previousIndex();
}
return -it.previousIndex() - 1;
}
- }
- return -list.size() - 1;
- }
-
- int low = 0, mid = list.size(), high = mid - 1, result = -1;
- while (low <= high) {
- mid = (low + high) >> 1;
- if ((result = key.compareTo((T)list.get(mid))) > 0) {
+ }
+ return -list.size() - 1;
+ }
+
+ int low = 0, mid = list.size(), high = mid - 1, result = -1;
+ while (low <= high) {
+ mid = (low + high) >> 1;
+ if ((result = key.compareTo((T) list.get(mid))) > 0) {
low = mid + 1;
} else if (result == 0) {
return mid;
} else {
high = mid - 1;
}
- }
- return -mid - (result < 0 ? 1 : 2);
- }
-
- /**
- * Performs a binary search for the specified element in the specified
- * sorted List using the specified Comparator.
- *
- * @param list
- * the sorted List to search
- * @param object
- * the element to find
- * @param comparator
- * the Comparator. If the comparator is <code>null</code>
- * then the search uses the objects' natural ordering.
- * @return the non-negative index of the element, or a negative index which
- * is the -index - 1 where the element would be inserted
- *
- * @throws ClassCastException
- * when an element in the list and the searched element
- * cannot be compared to each other using the Comparator
- */
- @SuppressWarnings("unchecked")
+ }
+ return -mid - (result < 0 ? 1 : 2);
+ }
+
+ /**
+ * Performs a binary search for the specified element in the specified
+ * sorted List using the specified Comparator.
+ *
+ * @param list
+ * the sorted List to search
+ * @param object
+ * the element to find
+ * @param comparator
+ * the Comparator. If the comparator is <code>null</code> then
+ * the search uses the objects' natural ordering.
+ * @return the non-negative index of the element, or a negative index which
+ * is the -index - 1 where the element would be inserted
+ *
+ * @throws ClassCastException
+ * when an element in the list and the searched element cannot
+ * be compared to each other using the Comparator
+ */
+ @SuppressWarnings("unchecked")
public static <T> int binarySearch(List<? extends T> list, T object,
- Comparator<? super T> comparator) {
- if (comparator == null) {
- return Collections.binarySearch((List<? extends Comparable<? super T>>)list, object);
- }
- if (!(list instanceof RandomAccess)) {
- ListIterator<? extends T> it = list.listIterator();
- while (it.hasNext()) {
- int result;
- if ((result = comparator.compare(object, it.next())) <= 0) {
+ Comparator<? super T> comparator) {
+ if (comparator == null) {
+ return Collections.binarySearch(
+ (List<? extends Comparable<? super T>>) list, object);
+ }
+ if (!(list instanceof RandomAccess)) {
+ ListIterator<? extends T> it = list.listIterator();
+ while (it.hasNext()) {
+ int result;
+ if ((result = comparator.compare(object, it.next())) <= 0) {
if (result == 0) {
return it.previousIndex();
}
return -it.previousIndex() - 1;
}
- }
- return -list.size() - 1;
- }
-
- int low = 0, mid = list.size(), high = mid - 1, result = -1;
- while (low <= high) {
- mid = (low + high) >> 1;
- if ((result = comparator.compare(object, list.get(mid))) > 0) {
+ }
+ return -list.size() - 1;
+ }
+
+ int low = 0, mid = list.size(), high = mid - 1, result = -1;
+ while (low <= high) {
+ mid = (low + high) >> 1;
+ if ((result = comparator.compare(object, list.get(mid))) > 0) {
low = mid + 1;
} else if (result == 0) {
return mid;
} else {
high = mid - 1;
}
- }
- return -mid - (result < 0 ? 1 : 2);
- }
-
- /**
- * Copies the elements from the source list to the destination list.
- * @param destination
- * @param source
- *
- * @throws IndexOutOfBoundsException
- * when the destination List is smaller than the source List
- * @throws UnsupportedOperationException
- * when replacing an element in the destination list is not
- * supported
- */
- public static <T> void copy(List<? super T> destination, List<? extends T> source) {
- if (destination.size() < source.size()) {
- throw new ArrayIndexOutOfBoundsException();
- }
- Iterator<? extends T> srcIt = source.iterator();
- ListIterator<? super T> destIt = destination.listIterator();
- while (srcIt.hasNext()) {
- try {
- destIt.next();
- } catch (NoSuchElementException e) {
- throw new ArrayIndexOutOfBoundsException();
- }
- destIt.set(srcIt.next());
- }
- }
-
- /**
- * Answers an Enumeration on the specified Collection.
- *
- * @param collection
- * the Collection to enumerate
- * @return an Enumeration
- */
- public static <T> Enumeration<T> enumeration(Collection<T> collection) {
+ }
+ return -mid - (result < 0 ? 1 : 2);
+ }
+
+ /**
+ * Copies the elements from the source list to the destination list.
+ *
+ * @param destination
+ * @param source
+ *
+ * @throws IndexOutOfBoundsException
+ * when the destination List is smaller than the source List
+ * @throws UnsupportedOperationException
+ * when replacing an element in the destination list is not
+ * supported
+ */
+ public static <T> void copy(List<? super T> destination,
+ List<? extends T> source) {
+ if (destination.size() < source.size()) {
+ throw new ArrayIndexOutOfBoundsException();
+ }
+ Iterator<? extends T> srcIt = source.iterator();
+ ListIterator<? super T> destIt = destination.listIterator();
+ while (srcIt.hasNext()) {
+ try {
+ destIt.next();
+ } catch (NoSuchElementException e) {
+ throw new ArrayIndexOutOfBoundsException();
+ }
+ destIt.set(srcIt.next());
+ }
+ }
+
+ /**
+ * Answers an Enumeration on the specified Collection.
+ *
+ * @param collection
+ * the Collection to enumerate
+ * @return an Enumeration
+ */
+ public static <T> Enumeration<T> enumeration(Collection<T> collection) {
final Collection<T> c = collection;
- return new Enumeration<T>() {
- Iterator<T> it = c.iterator();
+ return new Enumeration<T>() {
+ Iterator<T> it = c.iterator();
+
+ public boolean hasMoreElements() {
+ return it.hasNext();
+ }
+
+ public T nextElement() {
+ return it.next();
+ }
+ };
+ }
- public boolean hasMoreElements() {
- return it.hasNext();
- }
-
- public T nextElement() {
- return it.next();
- }
- };
- }
-
- /**
- * Fills the specified List with the specified element.
- *
- * @param list
- * the List to fill
- * @param object
- * the fill element
- *
- * @throws UnsupportedOperationException
- * when replacing an element in the List is not supported
- */
- public static <T> void fill(List<? super T> list, T object) {
+ /**
+ * Fills the specified List with the specified element.
+ *
+ * @param list
+ * the List to fill
+ * @param object
+ * the fill element
+ *
+ * @throws UnsupportedOperationException
+ * when replacing an element in the List is not supported
+ */
+ public static <T> void fill(List<? super T> list, T object) {
ListIterator<? super T> it = list.listIterator();
- while (it.hasNext()) {
- it.next();
- it.set(object);
- }
- }
-
- /**
- * Searches the specified Collection for the maximum element.
- *
- * @param collection
- * the Collection to search
- * @return the maximum element in the Collection
- *
- * @throws ClassCastException
- * when an element in the Collection does not implement
- * Comparable or elements cannot be compared to each other
- */
- public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> collection) {
+ while (it.hasNext()) {
+ it.next();
+ it.set(object);
+ }
+ }
+
+ /**
+ * Searches the specified Collection for the maximum element.
+ *
+ * @param collection
+ * the Collection to search
+ * @return the maximum element in the Collection
+ *
+ * @throws ClassCastException
+ * when an element in the Collection does not implement
+ * Comparable or elements cannot be compared to each other
+ */
+ public static <T extends Object & Comparable<? super T>> T max(
+ Collection<? extends T> collection) {
Iterator<? extends T> it = collection.iterator();
- T max = it.next();
- while (it.hasNext()) {
- T next = it.next();
- if (max.compareTo(next) < 0) {
+ T max = it.next();
+ while (it.hasNext()) {
+ T next = it.next();
+ if (max.compareTo(next) < 0) {
max = next;
}
- }
- return max;
- }
-
- /**
- * Searches the specified Collection for the maximum element using the
- * specified Comparator.
- *
- * @param collection
- * the Collection to search
- * @param comparator
- * the Comparator
- * @return the maximum element in the Collection
- *
- * @throws ClassCastException
- * when elements in the Collection cannot be compared to each
- * other using the Comparator
- */
- public static <T> T max(Collection<? extends T> collection, Comparator<? super T> comparator) {
- Iterator<? extends T> it = collection.iterator();
- T max = it.next();
- while (it.hasNext()) {
- T next = it.next();
- if (comparator.compare(max, next) < 0) {
+ }
+ return max;
+ }
+
+ /**
+ * Searches the specified Collection for the maximum element using the
+ * specified Comparator.
+ *
+ * @param collection
+ * the Collection to search
+ * @param comparator
+ * the Comparator
+ * @return the maximum element in the Collection
+ *
+ * @throws ClassCastException
+ * when elements in the Collection cannot be compared to each
+ * other using the Comparator
+ */
+ public static <T> T max(Collection<? extends T> collection,
+ Comparator<? super T> comparator) {
+ Iterator<? extends T> it = collection.iterator();
+ T max = it.next();
+ while (it.hasNext()) {
+ T next = it.next();
+ if (comparator.compare(max, next) < 0) {
max = next;
}
- }
- return max;
- }
-
- /**
- * Searches the specified Collection for the minimum element.
- *
- * @param collection
- * the Collection to search
- * @return the minimum element in the Collection
- *
- * @throws ClassCastException
- * when an element in the Collection does not implement
- * Comparable or elements cannot be compared to each other
- */
- public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> collection) {
- Iterator<? extends T> it = collection.iterator();
- T min = it.next();
- while (it.hasNext()) {
- T next = it.next();
- if (min.compareTo(next) > 0) {
+ }
+ return max;
+ }
+
+ /**
+ * Searches the specified Collection for the minimum element.
+ *
+ * @param collection
+ * the Collection to search
+ * @return the minimum element in the Collection
+ *
+ * @throws ClassCastException
+ * when an element in the Collection does not implement
+ * Comparable or elements cannot be compared to each other
+ */
+ public static <T extends Object & Comparable<? super T>> T min(
+ Collection<? extends T> collection) {
+ Iterator<? extends T> it = collection.iterator();
+ T min = it.next();
+ while (it.hasNext()) {
+ T next = it.next();
+ if (min.compareTo(next) > 0) {
min = next;
}
- }
- return min;
- }
-
- /**
- * Searches the specified Collection for the minimum element using the
- * specified Comparator.
- *
- * @param collection
- * the Collection to search
- * @param comparator
- * the Comparator
- * @return the minimum element in the Collection
- *
- * @throws ClassCastException
- * when elements in the Collection cannot be compared to each
- * other using the Comparator
- */
- public static <T> T min(Collection<? extends T> collection, Comparator<? super T> comparator) {
- Iterator<? extends T> it = collection.iterator();
- T min = it.next();
- while (it.hasNext()) {
- T next = it.next();
- if (comparator.compare(min, next) > 0) {
+ }
+ return min;
+ }
+
+ /**
+ * Searches the specified Collection for the minimum element using the
+ * specified Comparator.
+ *
+ * @param collection
+ * the Collection to search
+ * @param comparator
+ * the Comparator
+ * @return the minimum element in the Collection
+ *
+ * @throws ClassCastException
+ * when elements in the Collection cannot be compared to each
+ * other using the Comparator
+ */
+ public static <T> T min(Collection<? extends T> collection,
+ Comparator<? super T> comparator) {
+ Iterator<? extends T> it = collection.iterator();
+ T min = it.next();
+ while (it.hasNext()) {
+ T next = it.next();
+ if (comparator.compare(min, next) > 0) {
min = next;
}
- }
- return min;
- }
-
- /**
- * Answers a List containing the specified number of the specified element.
- * The list cannot be modified.
- *
- * @param length
- * the size of the returned List
- * @param object
- * the element
- * @return a List containing <code>length</code> copies of the element
- *
- * @throws IllegalArgumentException
- * when <code>length < 0</code>
- */
- public static <T> List<T> nCopies(final int length, T object) {
- return new CopiesList<T>(length, object);
- }
-
- /**
- * Returns the supplied <code>List</code> with the order of its contained
- * elements reversed.
- *
- * @param list
- * the List to reverse
- *
- * @throws UnsupportedOperationException
- * when replacing an element in the List is not supported
- */
- @SuppressWarnings("unchecked")
+ }
+ return min;
+ }
+
+ /**
+ * Answers a List containing the specified number of the specified element.
+ * The list cannot be modified.
+ *
+ * @param length
+ * the size of the returned List
+ * @param object
+ * the element
+ * @return a List containing <code>length</code> copies of the element
+ *
+ * @throws IllegalArgumentException
+ * when <code>length < 0</code>
+ */
+ public static <T> List<T> nCopies(final int length, T object) {
+ return new CopiesList<T>(length, object);
+ }
+
+ /**
+ * Returns the supplied <code>List</code> with the order of its contained
+ * elements reversed.
+ *
+ * @param list
+ * the List to reverse
+ *
+ * @throws UnsupportedOperationException
+ * when replacing an element in the List is not supported
+ */
+ @SuppressWarnings("unchecked")
public static void reverse(List<?> list) {
- int size = list.size();
- ListIterator<Object> front = (ListIterator<Object>)list.listIterator();
- ListIterator<Object> back = (ListIterator<Object>)list.listIterator(size);
- for (int i = 0; i < size / 2; i++) {
- Object frontNext = front.next();
+ int size = list.size();
+ ListIterator<Object> front = (ListIterator<Object>) list.listIterator();
+ ListIterator<Object> back = (ListIterator<Object>) list
+ .listIterator(size);
+ for (int i = 0; i < size / 2; i++) {
+ Object frontNext = front.next();
Object backPrev = back.previous();
- front.set(backPrev);
- back.set(frontNext);
- }
- }
+ front.set(backPrev);
+ back.set(frontNext);
+ }
+ }
- /**
+ /**
* <p>
* A Comparator which reverses the natural order of the elements. The
* <code>Comparator</code> that's returned is {@link Serializable}.
@@ -1723,17 +1750,18 @@
/**
* <p>
* Returns a {@link Comparator} that reverses the order of the
- * <code>Comparator</code> passed. If the <code>Comparator</code>
- * passed is <code>null</code>, then this method is equivalent to
+ * <code>Comparator</code> passed. If the <code>Comparator</code> passed
+ * is <code>null</code>, then this method is equivalent to
* {@link #reverseOrder()}.
* </p>
*
* <p>
- * The <code>Comparator</code> that's returned is {@link Serializable} if the
- * <code>Comparator</code> passed is serializable or <code>null</code>.
+ * The <code>Comparator</code> that's returned is {@link Serializable} if
+ * the <code>Comparator</code> passed is serializable or <code>null</code>.
* </p>
*
- * @param c The <code>Comparator</code> to reverse or <code>null</code>.
+ * @param c
+ * The <code>Comparator</code> to reverse or <code>null</code>.
* @return A <code>Comparator</code> instance.
* @see Comparator
* @since 1.5
@@ -1745,112 +1773,113 @@
return new ReverseComparatorWithComparator<T>(c);
}
- /**
- * Moves every element of the List to a random new position in the list.
- *
- * @param list
- * the List to shuffle
- *
- * @throws UnsupportedOperationException
- * when replacing an element in the List is not supported
- */
- public static void shuffle(List<?> list) {
- shuffle(list, new Random());
- }
-
- /**
- * Moves every element of the List to a random new position in the list
- * using the specified random number generator.
- *
- * @param list
- * the List to shuffle
- * @param random
- * the random number generator
- *
- * @throws UnsupportedOperationException
- * when replacing an element in the List is not supported
- */
- @SuppressWarnings("unchecked")
+ /**
+ * Moves every element of the List to a random new position in the list.
+ *
+ * @param list
+ * the List to shuffle
+ *
+ * @throws UnsupportedOperationException
[... 1606 lines stripped ...]