/* Copyright (c) 2008-2010, Avian Contributors Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. There is NO WARRANTY for this software. See license.txt for details. */ package java.util; public class Collections { private Collections() { } public static void shuffle(List list, Random random) { Object[] array = toArray(list, new Object[list.size()]); for (int i = 0; i < array.length; ++i) { int j = random.nextInt(array.length); Object tmp = array[i]; array[i] = array[j]; array[j] = tmp; } list.clear(); for (int i = 0; i < array.length; ++i) { list.add(array[i]); } } public static void shuffle(List list) { shuffle(list, new Random()); } static T[] toArray(Collection collection, T[] array) { Class c = array.getClass().getComponentType(); if (array.length < collection.size()) { array = (T[]) java.lang.reflect.Array.newInstance(c, collection.size()); } int i = 0; for (Object o: collection) { if (c.isInstance(o)) { array[i++] = (T) o; } else { throw new ArrayStoreException(); } } return array; } public static final List EMPTY_LIST = new UnmodifiableList(new ArrayList(0)); public static final List emptyList() { return EMPTY_LIST; } public static final Map emptyMap() { return (Map) new UnmodifiableMap( new HashMap(0)); } public static final Set emptySet() { return (Set) new UnmodifiableSet( new HashSet(0)); } static String toString(Collection c) { StringBuilder sb = new StringBuilder(); sb.append("["); for (Iterator it = c.iterator(); it.hasNext();) { sb.append(it.next()); if (it.hasNext()) { sb.append(","); } } sb.append("]"); return sb.toString(); } static String toString(Map m) { StringBuilder sb = new StringBuilder(); sb.append("{"); for (Iterator it = m.entrySet().iterator(); it.hasNext();) { Map.Entry e = it.next(); sb.append(e.getKey()) .append("=") .append(e.getValue()); if (it.hasNext()) { sb.append(","); } } sb.append("}"); return sb.toString(); } public static Enumeration enumeration(Collection c) { return new IteratorEnumeration (c.iterator()); } public static Comparator reverseOrder(Comparator cmp) { return new ReverseComparator(cmp); } static class IteratorEnumeration implements Enumeration { private final Iterator it; public IteratorEnumeration(Iterator it) { this.it = it; } public T nextElement() { return it.next(); } public boolean hasMoreElements() { return it.hasNext(); } } static class SynchronizedCollection implements Collection { protected final Object lock; protected final Collection collection; public SynchronizedCollection(Object lock, Collection collection) { this.lock = lock; this.collection = collection; } public int size() { synchronized (lock) { return collection.size(); } } public boolean isEmpty() { return size() == 0; } public boolean contains(Object e) { synchronized (lock) { return collection.contains(e); } } public boolean add(T e) { synchronized (lock) { return collection.add(e); } } public boolean addAll(Collection collection) { synchronized (lock) { return this.collection.addAll(collection); } } public boolean remove(Object e) { synchronized (lock) { return collection.remove((T)e); } } public Object[] toArray() { return toArray(new Object[size()]); } public T[] toArray(T[] array) { synchronized (lock) { return collection.toArray(array); } } public void clear() { synchronized (lock) { collection.clear(); } } public Iterator iterator() { return new SynchronizedIterator(lock, collection.iterator()); } } static class SynchronizedMap implements Map { protected final Object lock; protected final Map map; SynchronizedMap(Map map) { this.map = map; this.lock = this; } SynchronizedMap(Object lock, Map map) { this.lock = lock; this.map = map; } public void clear() { synchronized (lock) { map.clear(); } } public boolean containsKey(Object key) { synchronized (lock) { return map.containsKey(key); } } public boolean containsValue(Object value) { synchronized (lock) { return map.containsValue(value); } } public Set> entrySet() { synchronized (lock) { return new SynchronizedSet>(lock, map.entrySet()); } } public V get(Object key) { synchronized (lock) { return map.get(key); } } public boolean isEmpty() { synchronized (lock) { return map.isEmpty(); } } public Set keySet() { synchronized (lock) { return new SynchronizedSet(lock, map.keySet()); } } public V put(K key, V value) { synchronized (lock) { return map.put(key, value); } } public void putAll(Map elts) { synchronized (lock) { map.putAll(elts); } } public V remove(Object key) { synchronized (lock) { return map.remove(key); } } public int size() { synchronized (lock) { return map.size(); } } public Collection values() { synchronized (lock) { return new SynchronizedCollection(lock, map.values()); } } } public static Map synchronizedMap(Map map) { return new SynchronizedMap (map); } static class SynchronizedSet extends SynchronizedCollection implements Set { public SynchronizedSet(Object lock, Set set) { super(lock, set); } } static class SynchronizedIterator implements Iterator { private final Object lock; private final Iterator it; public SynchronizedIterator(Object lock, Iterator it) { this.lock = lock; this.it = it; } public T next() { synchronized (lock) { return it.next(); } } public boolean hasNext() { synchronized (lock) { return it.hasNext(); } } public void remove() { synchronized (lock) { it.remove(); } } } static class ArrayListIterator implements ListIterator { private final List list; private int toRemove = -1; private int index; public ArrayListIterator(List list) { this(list, 0); } public ArrayListIterator(List list, int index) { this.list = list; this.index = index - 1; } public boolean hasPrevious() { return index >= 0; } public T previous() { if (hasPrevious()) { toRemove = index; return list.get(index--); } else { throw new NoSuchElementException(); } } public T next() { if (hasNext()) { toRemove = ++index; return list.get(index); } else { throw new NoSuchElementException(); } } public boolean hasNext() { return index + 1 < list.size(); } public void remove() { if (toRemove != -1) { list.remove(toRemove); index = toRemove - 1; toRemove = -1; } else { throw new IllegalStateException(); } } } public static List unmodifiableList(List list) { return new UnmodifiableList(list); } static class UnmodifiableList implements List { private List inner; UnmodifiableList(List l) { this.inner = l; } public T get(int index) { return inner.get(index); } public T set(int index, T value) { throw new UnsupportedOperationException("not supported"); } public T remove(int index) { throw new UnsupportedOperationException("not supported"); } public boolean remove(Object o) { throw new UnsupportedOperationException("not supported"); } public boolean add(T element) { throw new UnsupportedOperationException("not supported"); } public void add(int index, T element) { throw new UnsupportedOperationException("not supported"); } public Iterator iterator() { return inner.iterator(); } public int indexOf(Object value) { return inner.indexOf(value); } public int lastIndexOf(Object value) { return inner.lastIndexOf(value); } public boolean isEmpty() { return inner.isEmpty(); } public ListIterator listIterator(int index) { return inner.listIterator(index); } public ListIterator listIterator() { return inner.listIterator(); } public int size() { return inner.size(); } public boolean contains(Object element) { return inner.contains(element); } public boolean addAll(Collection collection) { throw new UnsupportedOperationException("not supported"); } public Object[] toArray() { return inner.toArray(); } public S[] toArray(S[] array) { return inner.toArray(array); } public void clear() { throw new UnsupportedOperationException("not supported"); } } public static Map unmodifiableMap(Map m) { return new UnmodifiableMap(m); } static class UnmodifiableMap implements Map { private Map inner; UnmodifiableMap(Map m) { this.inner = m; } public void clear() { throw new UnsupportedOperationException("not supported"); } public boolean containsKey(Object key) { return inner.containsKey(key); } public boolean containsValue(Object value) { return inner.containsValue(value); } public Set> entrySet() { return unmodifiableSet(inner.entrySet()); } public V get(Object key) { return inner.get(key); } public boolean isEmpty() { return inner.isEmpty(); } public Set keySet() { return unmodifiableSet(inner.keySet()); } public V put(K key, V value) { throw new UnsupportedOperationException("not supported"); } public void putAll(Map t) { throw new UnsupportedOperationException("not supported"); } public V remove(Object key) { throw new UnsupportedOperationException("not supported"); } public int size() { return inner.size(); } public Collection values() { return unmodifiableSet((Set)inner.values()); } } static class UnmodifiableSet implements Set { private Set inner; UnmodifiableSet(Set inner) { this.inner = inner; } public boolean add(T element) { throw new UnsupportedOperationException("not supported"); } public boolean addAll(Collection collection) { throw new UnsupportedOperationException("not supported"); } public void clear() { throw new UnsupportedOperationException("not supported"); } public boolean contains(Object element) { return inner.contains(element); } public boolean isEmpty() { return inner.isEmpty(); } public Iterator iterator() { return inner.iterator(); } public boolean remove(Object element) { throw new UnsupportedOperationException("not supported"); } public int size() { return inner.size(); } public Object[] toArray() { return toArray(new Object[size()]); } public S[] toArray(S[] array) { return inner.toArray(array); } } public static Set unmodifiableSet(Set hs) { return new UnmodifiableSet(hs); } static class KeyIterator implements Iterator { private final Iterator> it; public KeyIterator(Iterator> it) { this.it = it; } public K next() { return it.next().getKey(); } public boolean hasNext() { return it.hasNext(); } public void remove() { it.remove(); } } static class ValueIterator implements Iterator { private final Iterator> it; public ValueIterator(Iterator> it) { this.it = it; } public V next() { return it.next().getValue(); } public boolean hasNext() { return it.hasNext(); } public void remove() { it.remove(); } } private static final class ReverseComparator implements Comparator { Comparator cmp; public ReverseComparator(Comparator cmp) { this.cmp = cmp; } public int compare(T o1, T o2) { return - cmp.compare(o1, o2); } } }