public class Test101 {
static class CheckedEntrySet<K,V> implements Set<Map.Entry<K,V>> {
Set<Map.Entry<K,V>> s;
Class<V> valueType;
CheckedEntrySet(Set<Map.Entry<K, V>> s, Class<V> valueType) {
this.s = s;
this.valueType = valueType;
}
}
public static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K, V> m,
Class<K> keyType,
Class<V> valueType) {
return new CheckedSortedMap<K,V>(m, keyType, valueType);
}
/**
* @serial include
*/
static class CheckedSortedMap<K,V> extends CheckedMap<K,V>
implements SortedMap<K,V>, Serializable
{
private static final long serialVersionUID = 1599671320688067438L;
private SortedMap<K, V> sm;
CheckedSortedMap(SortedMap<K, V> m,
Class<K> keyType, Class<V> valueType) {
super(m, keyType, valueType);
sm = m;
}
public Comparator<? super K> comparator() { return sm.comparator(); }
public K firstKey() { return sm.firstKey(); }
public K lastKey() { return sm.lastKey(); }
public SortedMap<K,V> subMap(K fromKey, K toKey) {
return new CheckedSortedMap<K,V>(sm.subMap(fromKey, toKey),
keyType, valueType);
}
public SortedMap<K,V> headMap(K toKey) {
return new CheckedSortedMap<K,V>(sm.headMap(toKey),
keyType, valueType);
}
public SortedMap<K,V> tailMap(K fromKey) {
return new CheckedSortedMap<K,V>(sm.tailMap(fromKey),
keyType, valueType);
}
}
// Miscellaneous
/**
* The empty set (immutable). This set is serializable.
*
* @see #emptySet()
*/
public static final Set EMPTY_SET = new EmptySet();
/**
* Returns the empty set (immutable). This set is serializable.
* Unlike the like-named field, this method is parameterized.
*
* <p>This example illustrates the type-safe way to obtain an empty set:
* <pre>
* Set<String> s = Collections.emptySet();
* </pre>
* Implementation note: Implementations of this method need not
* create a separate <tt>Set</tt> object for each call. Using this
* method is likely to have comparable cost to using the like-named
* field. (Unlike this method, the field does not provide type safety.)
*
* @see #EMPTY_SET
* @since 1.5
*/
public static final <T> Set<T> emptySet() {
return (Set<T>) EMPTY_SET;
}
/**
* @serial include
*/
private static class EmptySet extends AbstractSet<Object> implements Serializable {
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 1582296315990362920L;
public Iterator<Object> iterator() {
return new Iterator<Object>() {
public boolean hasNext() {
return false;
}
public Object next() {
throw new NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
public int size() {return 0;}
public boolean contains(Object obj) {return false;}
// Preserves singleton property
private Object readResolve() {
return EMPTY_SET;
}
}
/**
* The empty list (immutable). This list is serializable.
*
* @see #emptyList()
*/
public static final List EMPTY_LIST = new EmptyList();
/**
* Returns the empty list (immutable). This list is serializable.
*
* <p>This example illustrates the type-safe way to obtain an empty list:
* <pre>
* List<String> s = Collections.emptyList();
* </pre>
* Implementation note: Implementations of this method need not
* create a separate <tt>List</tt> object for each call. Using this
* method is likely to have comparable cost to using the like-named
* field. (Unlike this method, the field does not provide type safety.)
*
* @see #EMPTY_LIST
* @since 1.5
*/
public static final <T> List<T> emptyList() {
return (List<T>) EMPTY_LIST;
}
/**
* @serial include
*/
private static class EmptyList
extends AbstractList<Object>
implements RandomAccess, Serializable {
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 8842843931221139166L;
public int size() {return 0;}
public boolean contains(Object obj) {return false;}
public Object get(int index) {
throw new IndexOutOfBoundsException("Index: "+index);
}
// Preserves singleton property
private Object readResolve() {
return EMPTY_LIST;
}
}
/**
* The empty map (immutable). This map is serializable.
*
* @see #emptyMap()
* @since 1.3
*/
public static final Map EMPTY_MAP = new EmptyMap();
/**
* Returns the empty map (immutable). This map is serializable.
*
* <p>This example illustrates the type-safe way to obtain an empty set:
* <pre>
* Map<String, Date> s = Collections.emptyMap();
* </pre>
* Implementation note: Implementations of this method need not
* create a separate <tt>Map</tt> object for each call. Using this
* method is likely to have comparable cost to using the like-named
* field. (Unlike this method, the field does not provide type safety.)
*
* @see #EMPTY_MAP
* @since 1.5
*/
public static final <K,V> Map<K,V> emptyMap() {
return (Map<K,V>) EMPTY_MAP;
}
private static class EmptyMap
extends AbstractMap<Object,Object>
implements Serializable {
private static final long serialVersionUID = 6428348081105594320L;
public int size() {return 0;}
public boolean isEmpty() {return true;}
public boolean containsKey(Object key) {return false;}
public boolean containsValue(Object value) {return false;}
public Object get(Object key) {return null;}
public Set<Object> keySet() {return Collections.<Object>emptySet();}
public Collection<Object> values() {return Collections.<Object>emptySet();}
public Set<Map.Entry<Object,Object>> entrySet() {
return Collections.emptySet();
}
public boolean equals(Object o) {
return (o instanceof Map) && ((Map)o).size()==0;
}
public int hashCode() {return 0;}
// Preserves singleton property
private Object readResolve() {
return EMPTY_MAP;
}
}
/**
* Returns an immutable set containing only the specified object.
* The returned set is serializable.
*
* @param o the sole object to be stored in the returned set.
* @return an immutable set containing only the specified object.
*/
public static <T> Set<T> singleton(T o) {
return new SingletonSet<T>(o);
}
/**
* @serial include
*/
private static class SingletonSet<E>
extends AbstractSet<E>
implements Serializable
{
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 3193687207550431679L;
final private E element;
SingletonSet(E o) {element = o;}
public Iterator<E> iterator() {
return new Iterator<E>() {
private boolean hasNext = true;
public boolean hasNext() {
return hasNext;
}
public E next() {
if (hasNext) {
hasNext = false;
return element;
}
throw new NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
public int size() {return 1;}
public boolean contains(Object o) {return eq(o, element);}
}
/**
* Returns an immutable list containing only the specified object.
* The returned list is serializable.
*
* @param o the sole object to be stored in the returned list.
* @return an immutable list containing only the specified object.
* @since 1.3
*/
public static <T> List<T> singletonList(T o) {
return new SingletonList<T>(o);
}
private static class SingletonList<E>
extends AbstractList<E>
implements RandomAccess, Serializable {
static final long serialVersionUID = 3093736618740652951L;
private final E element;
SingletonList(E obj) {element = obj;}
public int size() {return 1;}
public boolean contains(Object obj) {return eq(obj, element);}
public E get(int index) {
if (index != 0)
throw new IndexOutOfBoundsException("Index: "+index+", Size: 1");
return element;
}
}
/**
* Returns an immutable map, mapping only the specified key to the
* specified value. The returned map is serializable.
*
* @param key the sole key to be stored in the returned map.
* @param value the value to which the returned map maps <tt>key</tt>.
* @return an immutable map containing only the specified key-value
* mapping.
* @since 1.3
*/
public static <K,V> Map<K,V> singletonMap(K key, V value) {
return new SingletonMap<K,V>(key, value);
}
private static class SingletonMap<K,V>
extends AbstractMap<K,V>
implements Serializable {
private static final long serialVersionUID = -6979724477215052911L;
private final K k;
private final V v;
SingletonMap(K key, V value) {
k = key;
v = value;
}
public int size() {return 1;}
public boolean isEmpty() {return false;}
public boolean containsKey(Object key) {return eq(key, k);}
public boolean containsValue(Object value) {return eq(value, v);}
public V get(Object key) {return (eq(key, k) ? v : null);}
private transient Set<K> keySet = null;
private transient Set<Map.Entry<K,V>> entrySet = null;
private transient Collection<V> values = null;
public Set<K> keySet() {
if (keySet==null)
keySet = singleton(k);
return keySet;
}
public Set<Map.Entry<K,V>> entrySet() {
if (entrySet==null)
entrySet = singleton((Map.Entry<K,V>)new ImmutableEntry<K,V>(k, v));
return entrySet;
}
public Collection<V> values() {
if (values==null)
values = singleton(v);
return values;
}
private static class ImmutableEntry<K,V>
implements Map.Entry<K,V> {
final K k;
final V v;
ImmutableEntry(K key, V value) {
k = key;
v = value;
}
public K getKey() {return k;}
public V getValue() {return v;}
public V setValue(V value) {
throw new UnsupportedOperationException();
}
public boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry e = (Map.Entry)o;
return eq(e.getKey(), k) && eq(e.getValue(), v);
}
public int hashCode() {
return ((k==null ? 0 : k.hashCode()) ^
(v==null ? 0 : v.hashCode()));
}
public String toString() {
return k+"="+v;
}
}
}
/**
* Returns an immutable list consisting of <tt>n</tt> copies of the
* specified object. The newly allocated data object is tiny (it contains
* a single reference to the data object). This method is useful in
* combination with the <tt>List.addAll</tt> method to grow lists.
* The returned list is serializable.
*
* @param n the number of elements in the returned list.
* @param o the element to appear repeatedly in the returned list.
* @return an immutable list consisting of <tt>n</tt> copies of the
* specified object.
* @throws IllegalArgumentException if n < 0.
* @see List#addAll(Collection)
* @see List#addAll(int, Collection)
*/
public static <T> List<T> nCopies(int n, T o) {
return new CopiesList<T>(n, o);
}
/**
* @serial include
*/
private static class CopiesList<E>
extends AbstractList<E>
implements RandomAccess, Serializable
{
static final long serialVersionUID = 2739099268398711800L;
int n;
E element;
CopiesList(int n, E o) {
if (n < 0)
throw new IllegalArgumentException("List length = " + n);
this.n = n;
element = o;
}
public int size() {
return n;
}
public boolean contains(Object obj) {
return n != 0 && eq(obj, element);
}
public E get(int index) {
if (index<0 || index>=n)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+n);
return element;
}
}
/**
* Returns a comparator that imposes the reverse of the <i>natural
* ordering</i> on a collection of objects that implement the
* <tt>Comparable</tt> interface. (The natural ordering is the ordering
* imposed by the objects' own <tt>compareTo</tt> method.) This enables a
* simple idiom for sorting (or maintaining) collections (or arrays) of
* objects that implement the <tt>Comparable</tt> interface in
* reverse-natural-order. For example, suppose a is an array of
* strings. Then: <pre>
* Arrays.sort(a, Collections.reverseOrder());
* </pre> sorts the array in reverse-lexicographic (alphabetical) order.<p>
*
* The returned comparator is serializable.
*
* @return a comparator that imposes the reverse of the <i>natural
* ordering</i> on a collection of objects that implement
* the <tt>Comparable</tt> interface.
* @see Comparable
*/
public static <T> Comparator<T> reverseOrder() {
return (Comparator<T>) REVERSE_ORDER;
}
private static final Comparator REVERSE_ORDER = new ReverseComparator();
/**
* @serial include
*/
private static class ReverseComparator<T>
implements Comparator<Comparable<Object>>, Serializable {
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 7207038068494060240L;
public int compare(Comparable<Object> c1, Comparable<Object> c2) {
return c2.compareTo(c1);
}
}
/**
* Returns a comparator that imposes the reverse ordering of the specified
* comparator. If the specified comparator is null, this method is
* equivalent to {@link #reverseOrder()} (in other words, it returns a
* comparator that imposes the reverse of the <i>natural ordering</i> on a
* collection of objects that implement the Comparable interface).
*
* <p>The returned comparator is serializable (assuming the specified
* comparator is also serializable or null).
*
* @return a comparator that imposes the reverse ordering of the
* specified comparator.
* @since 1.5
*/
public static <T> Comparator<T> reverseOrder(Comparator<T> cmp) {
if (cmp == null)
return new ReverseComparator(); // Unchecked warning!!
return new ReverseComparator2<T>(cmp);
}
/**
* @serial include
*/
private static class ReverseComparator2<T> implements Comparator<T>,
Serializable
{
private static final long serialVersionUID = 4374092139857L;
/**
* The comparator specified in the static factory. This will never
* be null, as the static factory returns a ReverseComparator
* instance if its argument is null.
*
* @serial
*/
private Comparator<T> cmp;
ReverseComparator2(Comparator<T> cmp) {
assert cmp != null;
this.cmp = cmp;
}
public int compare(T t1, T t2) {
return cmp.compare(t2, t1);
}
}
/**
* Returns an enumeration over the specified collection. This provides
* interoperability with legacy APIs that require an enumeration
* as input.
*
* @param c the collection for which an enumeration is to be returned.
* @return an enumeration over the specified collection.
* @see Enumeration
*/
public static <T> Enumeration<T> enumeration(final Collection<T> c) {
return new Enumeration<T>() {
Iterator<T> i = c.iterator();
public boolean hasMoreElements() {
return i.hasNext();
}
public T nextElement() {
return i.next();
}
};
}
/**
* Returns an array list containing the elements returned by the
* specified enumeration in the order they are returned by the
* enumeration. This method provides interoperability between
* legacy APIs that return enumerations and new APIs that require
* collections.
*
* @param e enumeration providing elements for the returned
* array list
* @return an array list containing the elements returned
* by the specified enumeration.
* @since 1.4
* @see Enumeration
* @see ArrayList
*/
public static <T> ArrayList<T> list(Enumeration<T> e) {
ArrayList<T> l = new ArrayList<T>();
while (e.hasMoreElements())
l.add(e.nextElement());
return l;
}
/**
* Returns true if the specified arguments are equal, or both null.
*/
private static boolean eq(Object o1, Object o2) {
return (o1==null ? o2==null : o1.equals(o2));
}
/**
* Returns the number of elements in the specified collection equal to the
* specified object. More formally, returns the number of elements
* <tt>e</tt> in the collection such that
* <tt>(o == null ? e == null : o.equals(e))</tt>.
*
* @param c the collection in which to determine the frequency
* of <tt>o</tt>
* @param o the object whose frequency is to be determined
* @throws NullPointerException if <tt>c</tt> is null
* @since 1.5
*/
public static int frequency(Collection<?> c, Object o) {
int result = 0;
if (o == null) {
for (Object e : c)
if (e == null)
result++;
} else {
for (Object e : c)
if (o.equals(e))
result++;
}
return result;
}
/**
* Returns <tt>true</tt> if the two specified collections have no
* elements in common.
*
* <p>Care must be exercised if this method is used on collections that
* do not comply with the general contract for <tt>Collection</tt>.
* Implementations may elect to iterate over either collection and test
* for containment in the other collection (or to perform any equivalent
* computation). If either collection uses a nonstandard equality test
* (as does a {@link SortedSet} whose ordering is not <i>compatible with
* equals</i>, or the key set of an {@link IdentityHashMap}), both
* collections must use the same nonstandard equality test, or the
* result of this method is undefined.
*
* <p>Note that it is permissible to pass the same collection in both
* parameters, in which case the method will return true if and only if
* the collection is empty.
*
* @param c1 a collection
* @param c2 a collection
* @throws NullPointerException if either collection is null
* @since 1.5
*/
public static boolean disjoint(Collection<?> c1, Collection<?> c2) {
/*
* We're going to iterate through c1 and test for inclusion in c2.
* If c1 is a Set and c2 isn't, swap the collections. Otherwise,
* place the shorter collection in c1. Hopefully this heuristic
* will minimize the cost of the operation.
*/
if ((c1 instanceof Set) && !(c2 instanceof Set) ||
(c1.size() > c2.size())) {
Collection<?> tmp = c1;
c1 = c2;
c2 = tmp;
}
for (Object e : c1)
if (c2.contains(e))
return false;
return true;
}
/**
* Adds all of the specified elements to the specified collection.
* Elements to be added may be specified individually or as an array.
* The behavior of this convenience method is identical to that of
* <tt>c.addAll(Arrays.asList(elements))</tt>, but this method is likely
* to run significantly faster under most implementations.
*
* <p>When elements are specified individually, this method provides a
* convenient way to add a few elements to an existing collection:
* <pre>
* Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon");
* </pre>
*
* @param c the collection into which <tt>elements</tt> are to be inserted
* @param a the elements to insert into <tt>c</tt>
* @return <tt>true</tt> if the collection changed as a result of the call
* @throws UnsupportedOperationException if <tt>c</tt> does not support
* the <tt>add</tt> method
* @throws NullPointerException if <tt>elements</tt> contains one or more
* null values and <tt>c</tt> does not support null elements, or
* if <tt>c</tt> or <tt>elements</tt> are <tt>null</tt>
* @throws IllegalArgumentException if some aspect of a value in
* <tt>elements</tt> prevents it from being added to <tt>c</tt>
* @see Collection#addAll(Collection)
* @since 1.5
*/
public static <T> boolean addAll(Collection<? super T> c, T... a) {
boolean result = false;
for (T e : a)
result |= c.add(e);
return result;
}
}