Widened, untabified, added equals() and hashCode() implementation to

SynchronizedIntSet.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2745 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
samskivert
2010-02-10 01:17:23 +00:00
parent d4678372be
commit 0468902779
+82 -89
View File
@@ -23,32 +23,29 @@ package com.samskivert.util;
import java.util.*; import java.util.*;
/** /**
* Provides functionality for the samskivert collections that the * Provides functionality for the samskivert collections that the <code>java.util</code> class of
* <code>java.util</code> class of the same name provides for the standard * the same name provides for the standard Java collections. Collections-related functionality that
* Java collections. Collections-related functionality that is different * is different from the standard support provided for the Java collections should go into {@link
* from the standard support provided for the Java collections should go * CollectionUtil}.
* into {@link CollectionUtil}.
*/ */
public class Collections public class Collections
{ {
/** /**
* Returns an Iterator that iterates over all the elements contained within * Returns an Iterator that iterates over all the elements contained within the Iterators
* the Iterators within the specified Iterable. * within the specified Iterable.
* *
* @param metaIterable an iterable of Iterators. * @param metaIterable an iterable of Iterators.
*/ */
public static <T> Iterator<T> getMetaIterator ( public static <T> Iterator<T> getMetaIterator (Iterable<Iterator<T>> metaIterable)
Iterable<Iterator<T>> metaIterable)
{ {
return new MetaIterator<T>(metaIterable); return new MetaIterator<T>(metaIterable);
} }
/** /**
* Get an Iterator over the supplied Collection that returns the * Get an Iterator over the supplied Collection that returns the elements in their natural
* elements in their natural order. * order.
*/ */
public static <T extends Comparable<? super T>> public static <T extends Comparable<? super T>> Iterator<T> getSortedIterator (Iterable<T> coll)
Iterator<T> getSortedIterator (Iterable<T> coll)
{ {
return getSortedIterator(coll.iterator(), new Comparator<T>() { return getSortedIterator(coll.iterator(), new Comparator<T>() {
public int compare (T o1, T o2) { public int compare (T o1, T o2) {
@@ -65,21 +62,19 @@ public class Collections
} }
/** /**
* Get an Iterator over the supplied Collection that returns the * Get an Iterator over the supplied Collection that returns the elements in the order dictated
* elements in the order dictated by the supplied Comparator. * by the supplied Comparator.
*/ */
public static <T> Iterator<T> getSortedIterator ( public static <T> Iterator<T> getSortedIterator (Iterable<T> coll, Comparator<T> comparator)
Iterable<T> coll, Comparator<T> comparator)
{ {
return getSortedIterator(coll.iterator(), comparator); return getSortedIterator(coll.iterator(), comparator);
} }
/** /**
* Get an Iterator that returns the same elements returned by * Get an Iterator that returns the same elements returned by the supplied Iterator, but in
* the supplied Iterator, but in their natural order. * their natural order.
*/ */
public static <T extends Comparable<? super T>> public static <T extends Comparable<? super T>> Iterator<T> getSortedIterator (Iterator<T> itr)
Iterator<T> getSortedIterator (Iterator<T> itr)
{ {
return getSortedIterator(itr, new Comparator<T>() { return getSortedIterator(itr, new Comparator<T>() {
public int compare (T o1, T o2) { public int compare (T o1, T o2) {
@@ -96,12 +91,10 @@ public class Collections
} }
/** /**
* Get an Iterator that returns the same elements returned by * Get an Iterator that returns the same elements returned by the supplied Iterator, but in the
* the supplied Iterator, but in the order dictated by the supplied * order dictated by the supplied Comparator.
* Comparator.
*/ */
public static <T> Iterator<T> getSortedIterator ( public static <T> Iterator<T> getSortedIterator (Iterator<T> itr, Comparator<T> comparator)
Iterator<T> itr, Comparator<T> comparator)
{ {
SortableArrayList<T> list = new SortableArrayList<T>(); SortableArrayList<T> list = new SortableArrayList<T>();
CollectionUtil.addAll(list, itr); CollectionUtil.addAll(list, itr);
@@ -110,11 +103,10 @@ public class Collections
} }
/** /**
* Get an Iterator over the supplied Iterable that returns * Get an Iterator over the supplied Iterable that returns the elements in a completely random
* the elements in a completely random order. Normally Iterators * order. Normally Iterators return elements in an undefined order, but it is usually the same
* return elements in an undefined order, but it is usually the same * between different invocations as long as the underlying Iterable has not changed. This
* between different invocations as long as the underlying Iterable * method mixes things up.
* has not changed. This method mixes things up.
*/ */
public static <T> Iterator<T> getRandomIterator (Iterable<T> c) public static <T> Iterator<T> getRandomIterator (Iterable<T> c)
{ {
@@ -122,8 +114,8 @@ public class Collections
} }
/** /**
* Get an Iterator that returns the same elements returned by * Get an Iterator that returns the same elements returned by the supplied Iterator, but in a
* the supplied Iterator, but in a completely random order. * completely random order.
*/ */
public static <T> Iterator<T> getRandomIterator (Iterator<T> itr) public static <T> Iterator<T> getRandomIterator (Iterator<T> itr)
{ {
@@ -134,8 +126,7 @@ public class Collections
} }
/** /**
* Get an Iterator that returns the elements in the supplied * Get an Iterator that returns the elements in the supplied Iterable but blocks removal.
* Iterable but blocks removal.
*/ */
public static <T> Iterator<T> getUnmodifiableIterator (Iterable<T> c) public static <T> Iterator<T> getUnmodifiableIterator (Iterable<T> c)
{ {
@@ -143,11 +134,9 @@ public class Collections
} }
/** /**
* Get an iterator that returns the same elements as the supplied * Get an iterator that returns the same elements as the supplied iterator but blocks removal.
* iterator but blocks removal.
*/ */
public static <T> Iterator<T> getUnmodifiableIterator ( public static <T> Iterator<T> getUnmodifiableIterator (final Iterator<T> itr)
final Iterator<T> itr)
{ {
return new Iterator<T>() { return new Iterator<T>() {
public boolean hasNext () { public boolean hasNext () {
@@ -164,13 +153,12 @@ public class Collections
} }
/** /**
* Returns a synchronized (thread-safe) int map backed by the * Returns a synchronized (thread-safe) int map backed by the specified int map. In order to
* specified int map. In order to guarantee serial access, it is * guarantee serial access, it is critical that <strong>all</strong> access to the backing int
* critical that <strong>all</strong> access to the backing int map is * map is accomplished through the returned int map.
* accomplished through the returned int map.
* *
* <p> It is imperative that the user manually synchronize on the * <p> It is imperative that the user manually synchronize on the returned int map when
* returned int map when iterating over any of its collection views: * iterating over any of its collection views:
* *
* <pre> * <pre>
* IntMap m = Collections.synchronizedIntMap(new HashIntMap()); * IntMap m = Collections.synchronizedIntMap(new HashIntMap());
@@ -184,11 +172,9 @@ public class Collections
* } * }
* </pre> * </pre>
* *
* Failure to follow this advice may result in non-deterministic * Failure to follow this advice may result in non-deterministic behavior.
* behavior.
* *
* <p> The returned map will be serializable if the specified map is * <p> The returned map will be serializable if the specified map is serializable.
* serializable.
* *
* @param m the int map to be "wrapped" in a synchronized int map. * @param m the int map to be "wrapped" in a synchronized int map.
* *
@@ -199,13 +185,12 @@ public class Collections
} }
/** /**
* Returns a synchronized (thread-safe) int set backed by the * Returns a synchronized (thread-safe) int set backed by the specified int set. In order to
* specified int set. In order to guarantee serial access, it is * guarantee serial access, it is critical that <strong>all</strong> access to the backing int
* critical that <strong>all</strong> access to the backing int map is * map is accomplished through the returned int map.
* accomplished through the returned int map.
* *
* <p> It is imperative that the user manually synchronize on the * <p> It is imperative that the user manually synchronize on the returned int map when
* returned int map when iterating over any of its collection views: * iterating over any of its collection views:
* *
* <pre> * <pre>
* IntSet s = Collections.synchronizedIntSet(new ArrayIntSet()); * IntSet s = Collections.synchronizedIntSet(new ArrayIntSet());
@@ -217,11 +202,9 @@ public class Collections
* } * }
* </pre> * </pre>
* *
* Failure to follow this advice may result in non-deterministic * Failure to follow this advice may result in non-deterministic behavior.
* behavior.
* *
* <p> The returned set will be serializable if the specified set is * <p> The returned set will be serializable if the specified set is serializable.
* serializable.
* *
* @param s the int set to be "wrapped" in a synchronized int set. * @param s the int set to be "wrapped" in a synchronized int set.
* *
@@ -239,6 +222,10 @@ public class Collections
private IntMap<V> m; // Backing Map private IntMap<V> m; // Backing Map
private Object mutex; // Object on which to synchronize private Object mutex; // Object on which to synchronize
private transient IntSet keySet = null;
private transient Set<Map.Entry<Integer,V>> entrySet = null;
private transient Collection<V> values = null;
SynchronizedIntMap (IntMap<V> m) { SynchronizedIntMap (IntMap<V> m) {
if (m == null) { if (m == null) {
throw new NullPointerException(); throw new NullPointerException();
@@ -307,27 +294,24 @@ public class Collections
synchronized(mutex) {m.clear();} synchronized(mutex) {m.clear();}
} }
private transient IntSet keySet = null;
private transient Set<Map.Entry<Integer,V>> entrySet = null;
private transient Collection<V> values = null;
public Set<Integer> keySet () { public Set<Integer> keySet () {
return intKeySet(); return intKeySet();
} }
public IntSet intKeySet () { public IntSet intKeySet () {
synchronized(mutex) { synchronized(mutex) {
if (keySet==null) if (keySet == null) {
keySet = new SynchronizedIntSet(m.intKeySet(), mutex); keySet = new SynchronizedIntSet(m.intKeySet(), mutex);
}
return keySet; return keySet;
} }
} }
public Set<Map.Entry<Integer,V>> entrySet () { public Set<Map.Entry<Integer,V>> entrySet () {
synchronized(mutex) { synchronized(mutex) {
if (entrySet==null) if (entrySet == null) {
entrySet = new SynchronizedSet<Map.Entry<Integer,V>>( entrySet = new SynchronizedSet<Map.Entry<Integer,V>>(m.entrySet(), mutex);
m.entrySet(), mutex); }
return entrySet; return entrySet;
} }
} }
@@ -340,8 +324,9 @@ public class Collections
public Collection<V> values () { public Collection<V> values () {
synchronized(mutex) { synchronized(mutex) {
if (values==null) if (values == null) {
values = new SynchronizedCollection<V>(m.values(), mutex); values = new SynchronizedCollection<V>(m.values(), mutex);
}
return values; return values;
} }
} }
@@ -363,13 +348,12 @@ public class Collections
} }
/** /**
* I wish I could use this from the <code>java.util.Collections</code> * I wish I could use this from the <code>java.util.Collections</code> class, but those crazy
* class, but those crazy kids at Sun are always using private and * kids at Sun are always using private and default access and pointlessly preventing people
* default access and pointlessly preventing people from properly * from properly reusing their code. Yay!
* reusing their code. Yay!
*/ */
protected static class SynchronizedSet<E> protected static class SynchronizedSet<E> extends SynchronizedCollection<E> implements Set<E>
extends SynchronizedCollection<E> implements Set<E> { {
SynchronizedSet (Set<E> s) { SynchronizedSet (Set<E> s) {
super(s); super(s);
} }
@@ -413,32 +397,41 @@ public class Collections
} }
public Interator interator () { public Interator interator () {
// must be manually sync'd by user return _i.interator(); // must be manually sync'd by user
return _i.interator();
} }
public int[] toIntArray () { public int[] toIntArray () {
synchronized(mutex) {return _i.toIntArray();} synchronized(mutex) {return _i.toIntArray();}
} }
@Override
public boolean equals (Object o) {
synchronized(mutex) {return _i.equals(o);}
}
@Override
public int hashCode () {
synchronized(mutex) {return _i.hashCode();}
}
/** Properly casted reference to our backing set. */ /** Properly casted reference to our backing set. */
protected IntSet _i; protected IntSet _i;
} }
/** /**
* I wish I could use this from the <code>java.util.Collections</code> * I wish I could use this from the <code>java.util.Collections</code> class, but those crazy
* class, but those crazy kids at Sun are always using private and * kids at Sun are always using private and default access and pointlessly preventing people
* default access and pointlessly preventing people from properly * from properly reusing their code. Yay!
* reusing their code. Yay!
*/ */
protected static class SynchronizedCollection<E> protected static class SynchronizedCollection<E> implements Collection<E>
implements Collection<E> { {
Collection<E> c; // Backing Collection protected Collection<E> c; // Backing Collection
Object mutex; // Object on which to synchronize protected Object mutex; // Object on which to synchronize
SynchronizedCollection (Collection<E> c) { SynchronizedCollection (Collection<E> c) {
if (c==null) if (c==null) {
throw new NullPointerException(); throw new NullPointerException();
}
this.c = c; this.c = c;
mutex = this; mutex = this;
} }
@@ -496,14 +489,14 @@ public class Collections
} }
/** /**
* An iterator that iterates over the union of the iterators provided by a * An iterator that iterates over the union of the iterators provided by a collection of
* collection of iterators. * iterators.
*/ */
protected static class MetaIterator<T> implements Iterator<T> protected static class MetaIterator<T> implements Iterator<T>
{ {
/** /**
* @param iterable a Iterable containing more Iterables * @param iterable a Iterable containing more Iterables whose elements we are to iterate
* whose elements we are to iterate over. * over.
*/ */
public MetaIterator (Iterable<Iterator<T>> iterable) public MetaIterator (Iterable<Iterator<T>> iterable)
{ {