edu.emory.mathcs.backport.java.util.Collections Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of virtdata-lib-realer Show documentation
Show all versions of virtdata-lib-realer Show documentation
With inspiration from other libraries
/*
* Written by Dawid Kurzyniec, on the basis of public specifications and
* public domain sources from JSR 166, and released to the public domain,
* as explained at http://creativecommons.org/licenses/publicdomain.
*/
package edu.emory.mathcs.backport.java.util;
import java.io.Serializable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
/**
* Augments {@link java.util.Collections} with methods added in Java 5.0
* and higher. Adds support for dynamically typesafe collection wrappers,
* and several utility methods.
*
* @see java.util.Collections
*/
public class Collections {
private Collections() {}
public static void sort(List list) {
java.util.Collections.sort(list);
}
public static void sort(List list, Comparator c) {
java.util.Collections.sort(list, c);
}
public static int binarySearch(List list, Object key) {
return java.util.Collections.binarySearch(list, key);
}
public static int binarySearch(List list, Object key, Comparator c) {
return java.util.Collections.binarySearch(list, key, c);
}
public static void reverse(List list) {
java.util.Collections.reverse(list);
}
public static void shuffle(List list) {
java.util.Collections.shuffle(list);
}
public static void shuffle(List list, Random rnd) {
java.util.Collections.shuffle(list, rnd);
}
public static void swap(List list, int i, int j) {
java.util.Collections.swap(list, i, i);
}
public static void fill(List list, Object obj) {
java.util.Collections.fill(list, obj);
}
public static void copy(List dest, List src) {
java.util.Collections.copy(dest, src);
}
public static Object min(Collection coll) {
return java.util.Collections.min(coll);
}
public static Object min(Collection coll, Comparator comp) {
return java.util.Collections.min(coll, comp);
}
public static Object max(Collection coll) {
return java.util.Collections.max(coll);
}
public static Object max(Collection coll, Comparator comp) {
return java.util.Collections.max(coll, comp);
}
public static void rotate(List list, int distance) {
java.util.Collections.rotate(list, distance);
}
public static boolean replaceAll(List list, Object oldVal, Object newVal) {
return java.util.Collections.replaceAll(list, oldVal, newVal);
}
public static int indexOfSubList(List source, List target) {
return java.util.Collections.indexOfSubList(source, target);
}
public static int lastIndexOfSubList(List source, List target) {
return java.util.Collections.lastIndexOfSubList(source, target);
}
// unmodifiable views
public static Collection unmodifiableCollection(Collection c) {
return java.util.Collections.unmodifiableCollection(c);
}
public static Set unmodifiableSet(Set s) {
return java.util.Collections.unmodifiableSet(s);
}
public static SortedSet unmodifiableSortedSet(SortedSet s) {
return java.util.Collections.unmodifiableSortedSet(s);
}
public static List unmodifiableList(List l) {
return java.util.Collections.unmodifiableList(l);
}
public static Map unmodifiableMap(Map m) {
return java.util.Collections.unmodifiableMap(m);
}
public static SortedMap unmodifiableSortedMap(SortedMap m) {
return java.util.Collections.unmodifiableSortedMap(m);
}
// synchronized views
public static Collection synchronizedCollection(Collection c) {
return java.util.Collections.synchronizedCollection(c);
}
public static Set synchronizedSet(Set s) {
return java.util.Collections.synchronizedSet(s);
}
public static SortedSet synchronizedSortedSet(SortedSet s) {
return java.util.Collections.synchronizedSortedSet(s);
}
public static List synchronizedList(List l) {
return java.util.Collections.synchronizedList(l);
}
public static Map synchronizedMap(Map m) {
return java.util.Collections.synchronizedMap(m);
}
public static SortedMap synchronizedSortedMap(SortedMap m) {
return java.util.Collections.synchronizedSortedMap(m);
}
// checked views
public static Collection checkedCollection(Collection c, Class type) {
return new CheckedCollection(c, type);
}
public static Set checkedSet(Set s, Class type) {
return new CheckedSet(s, type);
}
public static SortedSet checkedSortedSet(SortedSet s, Class type) {
return new CheckedSortedSet(s, type);
}
public static List checkedList(List l, Class type) {
return new CheckedList(l, type);
}
public static Map checkedMap(Map m, Class keyType, Class valueType) {
return new CheckedMap(m, keyType, valueType);
}
public static SortedMap checkedSortedMap(SortedMap m, Class keyType, Class valueType) {
return new CheckedSortedMap(m, keyType, valueType);
}
// empty views
public static Set emptySet() {
return java.util.Collections.EMPTY_SET;
}
public static List emptyList() {
return java.util.Collections.EMPTY_LIST;
}
public static Map emptyMap() {
return java.util.Collections.EMPTY_MAP;
}
// singleton views
public static Set singleton(Object o) {
return java.util.Collections.singleton(o);
}
public static List singletonList(Object o) {
return java.util.Collections.singletonList(o);
}
public static Map singletonMap(Object key, Object value) {
return java.util.Collections.singletonMap(key, value);
}
// other utils
public static List nCopies(int n, Object o) {
return java.util.Collections.nCopies(n, o);
}
public static Comparator reverseOrder() {
return java.util.Collections.reverseOrder();
}
public static Comparator reverseOrder(Comparator cmp) {
return (cmp instanceof ReverseComparator)
? ((ReverseComparator)cmp).cmp
: cmp == null ? reverseOrder() : new ReverseComparator(cmp);
}
public static Enumeration enumeration(Collection c) {
return java.util.Collections.enumeration(c);
}
public static ArrayList list(Enumeration e) {
return java.util.Collections.list(e);
}
public static int frequency(Collection c, Object o) {
int freq = 0;
if (o == null) {
for (Iterator itr = c.iterator(); itr.hasNext();) {
if (itr.next() == null) freq++;
}
}
else {
for (Iterator itr = c.iterator(); itr.hasNext();) {
if (o.equals(itr.next())) freq++;
}
}
return freq;
}
public static boolean disjoint(Collection a, Collection b) {
// set.contains() is usually faster than for other collections
if (a instanceof Set && (!(b instanceof Set) || a.size() < b.size())) {
Collection tmp = a;
a = b;
b = tmp;
}
for (Iterator itr = a.iterator(); itr.hasNext();) {
if (b.contains(itr.next())) return false;
}
return true;
}
public static boolean addAll(Collection c, Object[] a) {
boolean modified = false;
for (int i=0; i a.length) {
a = base;
}
else {
// need to copy back to a
System.arraycopy(base, 0, a, 0, base.length);
if (base.length < a.length) a[base.length] = null;
}
return a;
}
}
private class EntryView implements Map.Entry, Serializable {
final Map.Entry entry;
EntryView(Map.Entry entry) {
this.entry = entry;
}
public Object getKey() { return entry.getKey(); }
public Object getValue() { return entry.getValue(); }
public int hashCode() { return entry.hashCode(); }
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Map.Entry)) return false;
Map.Entry e = (Map.Entry)o;
return eq(getKey(), e.getKey()) && eq(getValue(), e.getValue());
}
public Object setValue(Object val) {
typeCheckValue(val);
return entry.setValue(val);
}
}
}
private static boolean eq(Object o1, Object o2) {
return (o1 == null) ? o2 == null : o1.equals(o2);
}
private static class CheckedSortedMap extends CheckedMap
implements SortedMap, Serializable {
final SortedMap map;
CheckedSortedMap(SortedMap map, Class keyType, Class valueType) {
super(map, keyType, valueType);
this.map = map;
}
public Comparator comparator() { return map.comparator(); }
public Object firstKey() { return map.firstKey(); }
public Object lastKey() { return map.lastKey(); }
public SortedMap subMap(Object fromKey, Object toKey) {
return new CheckedSortedMap(map.subMap(fromKey, toKey), keyType, valueType);
}
public SortedMap headMap(Object toKey) {
return new CheckedSortedMap(map.headMap(toKey), keyType, valueType);
}
public SortedMap tailMap(Object fromKey) {
return new CheckedSortedMap(map.tailMap(fromKey), keyType, valueType);
}
}
private static class SetFromMap extends AbstractSet implements Serializable {
private final static Object PRESENT = Boolean.TRUE;
final Map map;
transient Set keySet;
SetFromMap(Map map) {
this.map = map;
this.keySet = map.keySet();
}
public int hashCode() { return keySet.hashCode(); }
public int size() { return map.size(); }
public void clear() { map.clear(); }
public boolean isEmpty() { return map.isEmpty(); }
public boolean add(Object o) { return map.put(o, PRESENT) == null; }
public boolean contains(Object o) { return map.containsKey(o); }
public boolean equals(Object o) { return o == this || keySet.equals(o); }
public boolean remove(Object o) { return map.remove(o) == PRESENT; }
public boolean removeAll(Collection c) { return keySet.removeAll(c); }
public boolean retainAll(Collection c) { return keySet.retainAll(c); }
public Iterator iterator() { return keySet.iterator(); }
public Object[] toArray() { return keySet.toArray(); }
public Object[] toArray(Object[] a) { return keySet.toArray(a); }
public boolean addAll(Collection c) {
boolean modified = false;
for (Iterator it = c.iterator(); it.hasNext();) {
modified |= (map.put(it.next(), PRESENT) == null);
}
return modified;
}
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException
{
in.defaultReadObject();
keySet = map.keySet();
}
}
private static class AsLifoQueue extends AbstractQueue
implements Queue, Serializable
{
final Deque deque;
AsLifoQueue(Deque deque) { this.deque = deque; }
public boolean add(Object e) { return deque.offerFirst(e); }
public boolean offer(Object e) { return deque.offerFirst(e); }
public Object remove() { return deque.removeFirst(); }
public Object poll() { return deque.pollFirst(); }
public Object element() { return deque.getFirst(); }
public Object peek() { return deque.peekFirst(); }
public int size() { return deque.size(); }
public void clear() { deque.clear(); }
public boolean isEmpty() { return deque.isEmpty(); }
public Object[] toArray() { return deque.toArray(); }
public Object[] toArray(Object[] a) { return deque.toArray(a); }
public boolean contains(Object o) { return deque.contains(o); }
public boolean remove(Object o) { return deque.remove(o); }
public Iterator iterator() { return deque.iterator(); }
public String toString() { return deque.toString(); }
public boolean containsAll(Collection c) { return deque.containsAll(c); }
public boolean removeAll(Collection c) { return deque.removeAll(c); }
public boolean retainAll(Collection c) { return deque.retainAll(c); }
}
private static class ReverseComparator implements Comparator, Serializable {
final Comparator cmp;
ReverseComparator(Comparator cmp) {
this.cmp = cmp;
}
public int compare(Object o1, Object o2) {
return cmp.compare(o2, o1);
}
public boolean equals(Object o) {
return (o == this) ||
(o instanceof ReverseComparator &&
cmp.equals(((ReverseComparator)o).cmp));
}
public int hashCode() {
return cmp.hashCode() ^ 0x10000000;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy