From 0468902779ce8c59f05ce8a5d0cf2527655ae187 Mon Sep 17 00:00:00 2001 From: samskivert Date: Wed, 10 Feb 2010 01:17:23 +0000 Subject: [PATCH] Widened, untabified, added equals() and hashCode() implementation to SynchronizedIntSet. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2745 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/Collections.java | 339 +++++++++--------- 1 file changed, 166 insertions(+), 173 deletions(-) diff --git a/src/java/com/samskivert/util/Collections.java b/src/java/com/samskivert/util/Collections.java index caa657b9..56f831ff 100644 --- a/src/java/com/samskivert/util/Collections.java +++ b/src/java/com/samskivert/util/Collections.java @@ -23,32 +23,29 @@ package com.samskivert.util; import java.util.*; /** - * Provides functionality for the samskivert collections that the - * java.util 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 java.util 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 Iterator getMetaIterator ( - Iterable> metaIterable) + public static Iterator getMetaIterator (Iterable> metaIterable) { return new MetaIterator(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 > - Iterator getSortedIterator (Iterable coll) + public static > Iterator getSortedIterator (Iterable coll) { return getSortedIterator(coll.iterator(), new Comparator() { 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 Iterator getSortedIterator ( - Iterable coll, Comparator comparator) + public static Iterator getSortedIterator (Iterable coll, Comparator 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 > - Iterator getSortedIterator (Iterator itr) + public static > Iterator getSortedIterator (Iterator itr) { return getSortedIterator(itr, new Comparator() { 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 Iterator getSortedIterator ( - Iterator itr, Comparator comparator) + public static Iterator getSortedIterator (Iterator itr, Comparator comparator) { SortableArrayList list = new SortableArrayList(); 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 Iterator getRandomIterator (Iterable 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 Iterator getRandomIterator (Iterator 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 Iterator getUnmodifiableIterator (Iterable 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 Iterator getUnmodifiableIterator ( - final Iterator itr) + public static Iterator getUnmodifiableIterator (final Iterator itr) { return new Iterator() { 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 all 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 all access to the backing int + * map is accomplished through the returned int map. * - *

It is imperative that the user manually synchronize on the - * returned int map when iterating over any of its collection views: + *

It is imperative that the user manually synchronize on the returned int map when + * iterating over any of its collection views: * *

      *  IntMap m = Collections.synchronizedIntMap(new HashIntMap());
@@ -184,28 +172,25 @@ public class Collections
      *  }
      * 
* - * Failure to follow this advice may result in non-deterministic - * behavior. + * Failure to follow this advice may result in non-deterministic behavior. * - *

The returned map will be serializable if the specified map is - * serializable. + *

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. * * @return a synchronized view of the specified int map. */ public static IntMap synchronizedIntMap (IntMap m) { - return new SynchronizedIntMap(m); + return new SynchronizedIntMap(m); } /** - * Returns a synchronized (thread-safe) int set backed by the - * specified int set. In order to guarantee serial access, it is - * critical that all 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 all access to the backing int + * map is accomplished through the returned int map. * - *

It is imperative that the user manually synchronize on the - * returned int map when iterating over any of its collection views: + *

It is imperative that the user manually synchronize on the returned int map when + * iterating over any of its collection views: * *

      *  IntSet s = Collections.synchronizedIntSet(new ArrayIntSet());
@@ -217,11 +202,9 @@ public class Collections
      *  }
      * 
* - * Failure to follow this advice may result in non-deterministic - * behavior. + * Failure to follow this advice may result in non-deterministic behavior. * - *

The returned set will be serializable if the specified set is - * serializable. + *

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. * @@ -236,10 +219,14 @@ public class Collections */ protected static class SynchronizedIntMap implements IntMap { - private IntMap m; // Backing Map + private IntMap m; // Backing Map private Object mutex; // Object on which to synchronize - SynchronizedIntMap(IntMap m) { + private transient IntSet keySet = null; + private transient Set> entrySet = null; + private transient Collection values = null; + + SynchronizedIntMap (IntMap m) { if (m == null) { throw new NullPointerException(); } @@ -247,7 +234,7 @@ public class Collections mutex = this; } - SynchronizedIntMap(IntMap m, Object mutex) { + SynchronizedIntMap(IntMap m, Object mutex) { if (m == null) { throw new NullPointerException(); } @@ -255,135 +242,132 @@ public class Collections this.mutex = mutex; } - public int size() { - synchronized(mutex) {return m.size();} + public int size () { + synchronized(mutex) {return m.size();} } - public boolean isEmpty(){ - synchronized(mutex) {return m.isEmpty();} + public boolean isEmpty (){ + synchronized(mutex) {return m.isEmpty();} } - public boolean containsKey(int key) { - synchronized(mutex) {return m.containsKey(key);} + public boolean containsKey (int key) { + synchronized(mutex) {return m.containsKey(key);} } - public boolean containsKey(Object key) { - synchronized(mutex) {return m.containsKey(key);} + public boolean containsKey (Object key) { + synchronized(mutex) {return m.containsKey(key);} } - public boolean containsValue(Object value){ - synchronized(mutex) {return m.containsValue(value);} + public boolean containsValue (Object value){ + synchronized(mutex) {return m.containsValue(value);} } - public V get(int key) { - synchronized(mutex) {return m.get(key);} + public V get (int key) { + synchronized(mutex) {return m.get(key);} } - public V get(Object key) { - synchronized(mutex) {return m.get(key);} + public V get (Object key) { + synchronized(mutex) {return m.get(key);} } - public V put(int key, V value) { - synchronized(mutex) {return m.put(key, value);} + public V put (int key, V value) { + synchronized(mutex) {return m.put(key, value);} } - public V put(Integer key, V value) { - synchronized(mutex) {return m.put(key, value);} + public V put (Integer key, V value) { + synchronized(mutex) {return m.put(key, value);} } - public V remove(int key) { - synchronized(mutex) {return m.remove(key);} + public V remove (int key) { + synchronized(mutex) {return m.remove(key);} } - public V remove(Object key) { - synchronized(mutex) {return m.remove(key);} + public V remove (Object key) { + synchronized(mutex) {return m.remove(key);} } - public void putAll(Map map) { - synchronized(mutex) {m.putAll(map);} + public void putAll (Map map) { + synchronized(mutex) {m.putAll(map);} } - public void clear() { - synchronized(mutex) {m.clear();} + public void clear () { + synchronized(mutex) {m.clear();} } - private transient IntSet keySet = null; - private transient Set> entrySet = null; - private transient Collection values = null; - - public Set keySet() { + public Set keySet () { return intKeySet(); - } + } public IntSet intKeySet () { synchronized(mutex) { - if (keySet==null) + if (keySet == null) { keySet = new SynchronizedIntSet(m.intKeySet(), mutex); + } return keySet; } } - public Set> entrySet() { + public Set> entrySet () { synchronized(mutex) { - if (entrySet==null) - entrySet = new SynchronizedSet>( - m.entrySet(), mutex); + if (entrySet == null) { + entrySet = new SynchronizedSet>(m.entrySet(), mutex); + } return entrySet; } - } + } - public Set> intEntrySet() { + public Set> intEntrySet () { synchronized(mutex) { return new SynchronizedSet>(m.intEntrySet(), mutex); } - } + } - public Collection values() { + public Collection values () { synchronized(mutex) { - if (values==null) + if (values == null) { values = new SynchronizedCollection(m.values(), mutex); + } return values; } } - @Override - public boolean equals(Object o) { + @Override + public boolean equals (Object o) { synchronized(mutex) {return m.equals(o);} } - @Override - public int hashCode() { + @Override + public int hashCode () { synchronized(mutex) {return m.hashCode();} } - @Override - public String toString() { - synchronized(mutex) {return m.toString();} + @Override + public String toString () { + synchronized(mutex) {return m.toString();} } } /** - * I wish I could use this from the java.util.Collections - * 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 java.util.Collections 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 - extends SynchronizedCollection implements Set { - SynchronizedSet(Set s) { + protected static class SynchronizedSet extends SynchronizedCollection implements Set + { + SynchronizedSet (Set s) { super(s); } - SynchronizedSet(Set s, Object mutex) { + SynchronizedSet(Set s, Object mutex) { super(s, mutex); } - @Override - public boolean equals(Object o) { - synchronized(mutex) {return c.equals(o);} + @Override + public boolean equals (Object o) { + synchronized(mutex) {return c.equals(o);} } - @Override - public int hashCode() { - synchronized(mutex) {return c.hashCode();} + @Override + public int hashCode () { + synchronized(mutex) {return c.hashCode();} } } @@ -413,97 +397,106 @@ 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 java.util.Collections - * 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 java.util.Collections 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 - implements Collection { - Collection c; // Backing Collection - Object mutex; // Object on which to synchronize + protected static class SynchronizedCollection implements Collection + { + protected Collection c; // Backing Collection + protected Object mutex; // Object on which to synchronize - SynchronizedCollection(Collection c) { - if (c==null) + SynchronizedCollection (Collection c) { + if (c==null) { throw new NullPointerException(); - this.c = c; + } + this.c = c; mutex = this; } - SynchronizedCollection(Collection c, Object mutex) { - this.c = c; + SynchronizedCollection(Collection c, Object mutex) { + this.c = c; this.mutex = mutex; } - public int size() { - synchronized(mutex) {return c.size();} + public int size () { + synchronized(mutex) {return c.size();} } - public boolean isEmpty() { - synchronized(mutex) {return c.isEmpty();} + public boolean isEmpty () { + synchronized(mutex) {return c.isEmpty();} } - public boolean contains(Object o) { - synchronized(mutex) {return c.contains(o);} + public boolean contains (Object o) { + synchronized(mutex) {return c.contains(o);} } - public Object[] toArray() { - synchronized(mutex) {return c.toArray();} + public Object[] toArray () { + synchronized(mutex) {return c.toArray();} } - public T[] toArray(T[] a) { - synchronized(mutex) {return c.toArray(a);} + public T[] toArray (T[] a) { + synchronized(mutex) {return c.toArray(a);} } - public Iterator iterator() { + public Iterator iterator () { return c.iterator(); // Must be manually synched by user! } - public boolean add(E o) { - synchronized(mutex) {return c.add(o);} + public boolean add (E o) { + synchronized(mutex) {return c.add(o);} } - public boolean remove(Object o) { - synchronized(mutex) {return c.remove(o);} + public boolean remove (Object o) { + synchronized(mutex) {return c.remove(o);} } - public boolean containsAll(Collection coll) { - synchronized(mutex) {return c.containsAll(coll);} + public boolean containsAll (Collection coll) { + synchronized(mutex) {return c.containsAll(coll);} } - public boolean addAll(Collection coll) { - synchronized(mutex) {return c.addAll(coll);} + public boolean addAll (Collection coll) { + synchronized(mutex) {return c.addAll(coll);} } - public boolean removeAll(Collection coll) { - synchronized(mutex) {return c.removeAll(coll);} + public boolean removeAll (Collection coll) { + synchronized(mutex) {return c.removeAll(coll);} } - public boolean retainAll(Collection coll) { - synchronized(mutex) {return c.retainAll(coll);} + public boolean retainAll (Collection coll) { + synchronized(mutex) {return c.retainAll(coll);} } - public void clear() { - synchronized(mutex) {c.clear();} + public void clear () { + synchronized(mutex) {c.clear();} } - @Override - public String toString() { - synchronized(mutex) {return c.toString();} + @Override + public String toString () { + synchronized(mutex) {return c.toString();} } } /** - * 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 implements Iterator { /** - * @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> iterable) {