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.*;
/**
* Provides functionality for the samskivert collections that the
* <code>java.util</code> class of the same name provides for the standard
* Java collections. Collections-related functionality that is different
* from the standard support provided for the Java collections should go
* into {@link CollectionUtil}.
* Provides functionality for the samskivert collections that the <code>java.util</code> class of
* the same name provides for the standard Java collections. Collections-related functionality that
* is different from the standard support provided for the Java collections should go into {@link
* CollectionUtil}.
*/
public class Collections
{
/**
* Returns an Iterator that iterates over all the elements contained within
* the Iterators within the specified Iterable.
* Returns an Iterator that iterates over all the elements contained within the Iterators
* within the specified Iterable.
*
* @param metaIterable an iterable of Iterators.
*/
public static <T> Iterator<T> getMetaIterator (
Iterable<Iterator<T>> metaIterable)
public static <T> Iterator<T> getMetaIterator (Iterable<Iterator<T>> metaIterable)
{
return new MetaIterator<T>(metaIterable);
}
/**
* Get an Iterator over the supplied Collection that returns the
* elements in their natural order.
* Get an Iterator over the supplied Collection that returns the elements in their natural
* order.
*/
public static <T extends Comparable<? super T>>
Iterator<T> getSortedIterator (Iterable<T> coll)
public static <T extends Comparable<? super T>> Iterator<T> getSortedIterator (Iterable<T> coll)
{
return getSortedIterator(coll.iterator(), new Comparator<T>() {
public int compare (T o1, T o2) {
@@ -65,21 +62,19 @@ public class Collections
}
/**
* Get an Iterator over the supplied Collection that returns the
* elements in the order dictated by the supplied Comparator.
* Get an Iterator over the supplied Collection that returns the elements in the order dictated
* by the supplied Comparator.
*/
public static <T> Iterator<T> getSortedIterator (
Iterable<T> coll, Comparator<T> comparator)
public static <T> Iterator<T> getSortedIterator (Iterable<T> coll, Comparator<T> comparator)
{
return getSortedIterator(coll.iterator(), comparator);
}
/**
* Get an Iterator that returns the same elements returned by
* the supplied Iterator, but in their natural order.
* Get an Iterator that returns the same elements returned by the supplied Iterator, but in
* their natural order.
*/
public static <T extends Comparable<? super T>>
Iterator<T> getSortedIterator (Iterator<T> itr)
public static <T extends Comparable<? super T>> Iterator<T> getSortedIterator (Iterator<T> itr)
{
return getSortedIterator(itr, new Comparator<T>() {
public int compare (T o1, T o2) {
@@ -96,12 +91,10 @@ public class Collections
}
/**
* Get an Iterator that returns the same elements returned by
* the supplied Iterator, but in the order dictated by the supplied
* Comparator.
* Get an Iterator that returns the same elements returned by the supplied Iterator, but in the
* order dictated by the supplied Comparator.
*/
public static <T> Iterator<T> getSortedIterator (
Iterator<T> itr, Comparator<T> comparator)
public static <T> Iterator<T> getSortedIterator (Iterator<T> itr, Comparator<T> comparator)
{
SortableArrayList<T> list = new SortableArrayList<T>();
CollectionUtil.addAll(list, itr);
@@ -110,11 +103,10 @@ public class Collections
}
/**
* Get an Iterator over the supplied Iterable that returns
* the elements in a completely random order. Normally Iterators
* 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 method mixes things up.
* Get an Iterator over the supplied Iterable that returns the elements in a completely random
* order. Normally Iterators 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
* method mixes things up.
*/
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
* the supplied Iterator, but in a completely random order.
* Get an Iterator that returns the same elements returned by the supplied Iterator, but in a
* completely random order.
*/
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
* Iterable but blocks removal.
* Get an Iterator that returns the elements in the supplied Iterable but blocks removal.
*/
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
* iterator but blocks removal.
* Get an iterator that returns the same elements as the supplied iterator but blocks removal.
*/
public static <T> Iterator<T> getUnmodifiableIterator (
final Iterator<T> itr)
public static <T> Iterator<T> getUnmodifiableIterator (final Iterator<T> itr)
{
return new Iterator<T>() {
public boolean hasNext () {
@@ -164,13 +153,12 @@ public class Collections
}
/**
* Returns a synchronized (thread-safe) int map backed by the
* specified int map. In order to guarantee serial access, it is
* critical that <strong>all</strong> access to the backing int map is
* accomplished through the returned int map.
* Returns a synchronized (thread-safe) int map backed by the specified int map. In order to
* guarantee serial access, it is critical that <strong>all</strong> access to the backing int
* map is accomplished through the returned int map.
*
* <p> It is imperative that the user manually synchronize on the
* returned int map when iterating over any of its collection views:
* <p> It is imperative that the user manually synchronize on the returned int map when
* iterating over any of its collection views:
*
* <pre>
* IntMap m = Collections.synchronizedIntMap(new HashIntMap());
@@ -184,11 +172,9 @@ public class Collections
* }
* </pre>
*
* Failure to follow this advice may result in non-deterministic
* behavior.
* Failure to follow this advice may result in non-deterministic behavior.
*
* <p> The returned map will be serializable if the specified map is
* serializable.
* <p> The returned map will be serializable if the specified map is serializable.
*
* @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
* specified int set. In order to guarantee serial access, it is
* critical that <strong>all</strong> access to the backing int map is
* accomplished through the returned int map.
* Returns a synchronized (thread-safe) int set backed by the specified int set. In order to
* guarantee serial access, it is critical that <strong>all</strong> access to the backing int
* map is accomplished through the returned int map.
*
* <p> It is imperative that the user manually synchronize on the
* returned int map when iterating over any of its collection views:
* <p> It is imperative that the user manually synchronize on the returned int map when
* iterating over any of its collection views:
*
* <pre>
* IntSet s = Collections.synchronizedIntSet(new ArrayIntSet());
@@ -217,11 +202,9 @@ public class Collections
* }
* </pre>
*
* Failure to follow this advice may result in non-deterministic
* behavior.
* Failure to follow this advice may result in non-deterministic behavior.
*
* <p> The returned set will be serializable if the specified set is
* serializable.
* <p> The returned set will be serializable if the specified set is serializable.
*
* @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 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) {
if (m == null) {
throw new NullPointerException();
@@ -307,27 +294,24 @@ public class Collections
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 () {
return intKeySet();
}
public IntSet intKeySet () {
synchronized(mutex) {
if (keySet==null)
if (keySet == null) {
keySet = new SynchronizedIntSet(m.intKeySet(), mutex);
}
return keySet;
}
}
public Set<Map.Entry<Integer,V>> entrySet () {
synchronized(mutex) {
if (entrySet==null)
entrySet = new SynchronizedSet<Map.Entry<Integer,V>>(
m.entrySet(), mutex);
if (entrySet == null) {
entrySet = new SynchronizedSet<Map.Entry<Integer,V>>(m.entrySet(), mutex);
}
return entrySet;
}
}
@@ -340,8 +324,9 @@ public class Collections
public Collection<V> values () {
synchronized(mutex) {
if (values==null)
if (values == null) {
values = new SynchronizedCollection<V>(m.values(), mutex);
}
return values;
}
}
@@ -363,13 +348,12 @@ public class Collections
}
/**
* I wish I could use this from the <code>java.util.Collections</code>
* class, but those crazy kids at Sun are always using private and
* default access and pointlessly preventing people from properly
* reusing their code. Yay!
* I wish I could use this from the <code>java.util.Collections</code> class, but those crazy
* kids at Sun are always using private and default access and pointlessly preventing people
* from properly reusing their code. Yay!
*/
protected static class SynchronizedSet<E>
extends SynchronizedCollection<E> implements Set<E> {
protected static class SynchronizedSet<E> extends SynchronizedCollection<E> implements Set<E>
{
SynchronizedSet (Set<E> s) {
super(s);
}
@@ -413,32 +397,41 @@ public class Collections
}
public Interator interator () {
// must be manually sync'd by user
return _i.interator();
return _i.interator(); // must be manually sync'd by user
}
public int[] 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. */
protected IntSet _i;
}
/**
* I wish I could use this from the <code>java.util.Collections</code>
* class, but those crazy kids at Sun are always using private and
* default access and pointlessly preventing people from properly
* reusing their code. Yay!
* I wish I could use this from the <code>java.util.Collections</code> class, but those crazy
* kids at Sun are always using private and default access and pointlessly preventing people
* from properly reusing their code. Yay!
*/
protected static class SynchronizedCollection<E>
implements Collection<E> {
Collection<E> c; // Backing Collection
Object mutex; // Object on which to synchronize
protected static class SynchronizedCollection<E> implements Collection<E>
{
protected Collection<E> c; // Backing Collection
protected Object mutex; // Object on which to synchronize
SynchronizedCollection (Collection<E> c) {
if (c==null)
if (c==null) {
throw new NullPointerException();
}
this.c = c;
mutex = this;
}
@@ -496,14 +489,14 @@ public class Collections
}
/**
* An iterator that iterates over the union of the iterators provided by a
* collection of iterators.
* An iterator that iterates over the union of the iterators provided by a collection of
* iterators.
*/
protected static class MetaIterator<T> implements Iterator<T>
{
/**
* @param iterable a Iterable containing more Iterables
* whose elements we are to iterate over.
* @param iterable a Iterable containing more Iterables whose elements we are to iterate
* over.
*/
public MetaIterator (Iterable<Iterator<T>> iterable)
{