http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/map/SingletonMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/map/SingletonMap.java b/src/java/org/apache/commons/collections/map/SingletonMap.java index 12372f1..a3ae8c0 100644 --- a/src/java/org/apache/commons/collections/map/SingletonMap.java +++ b/src/java/org/apache/commons/collections/map/SingletonMap.java @@ -27,7 +27,6 @@ import java.util.Set; import org.apache.commons.collections.BoundedMap; import org.apache.commons.collections.KeyValue; -import org.apache.commons.collections.MapIterator; import org.apache.commons.collections.OrderedMap; import org.apache.commons.collections.OrderedMapIterator; import org.apache.commons.collections.ResettableIterator; @@ -59,16 +58,16 @@ import org.apache.commons.collections.keyvalue.TiedMapEntry; * * @author Stephen Colebourne */ -public class SingletonMap - implements OrderedMap, BoundedMap, KeyValue, Serializable, Cloneable { +public class SingletonMap<K, V> + implements OrderedMap<K, V>, BoundedMap<K, V>, KeyValue<K, V>, Serializable, Cloneable { /** Serialization version */ private static final long serialVersionUID = -8931271118676803261L; /** Singleton key */ - private final Object key; + private final K key; /** Singleton value */ - private Object value; + private V value; /** * Constructor that creates a map of <code>null</code> to <code>null</code>. @@ -84,7 +83,7 @@ public class SingletonMap * @param key the key to use * @param value the value to use */ - public SingletonMap(Object key, Object value) { + public SingletonMap(K key, V value) { super(); this.key = key; this.value = value; @@ -95,7 +94,7 @@ public class SingletonMap * * @param keyValue the key value pair to use */ - public SingletonMap(KeyValue keyValue) { + public SingletonMap(KeyValue<K, V> keyValue) { super(); this.key = keyValue.getKey(); this.value = keyValue.getValue(); @@ -106,7 +105,7 @@ public class SingletonMap * * @param mapEntry the mapEntry to use */ - public SingletonMap(Map.Entry mapEntry) { + public SingletonMap(Map.Entry<K, V> mapEntry) { super(); this.key = mapEntry.getKey(); this.value = mapEntry.getValue(); @@ -119,12 +118,12 @@ public class SingletonMap * @throws NullPointerException if the map is null * @throws IllegalArgumentException if the size is not 1 */ - public SingletonMap(Map map) { + public SingletonMap(Map<K, V> map) { super(); if (map.size() != 1) { throw new IllegalArgumentException("The map size must be 1"); } - Map.Entry entry = (Map.Entry) map.entrySet().iterator().next(); + Map.Entry<K, V> entry = map.entrySet().iterator().next(); this.key = entry.getKey(); this.value = entry.getValue(); } @@ -136,7 +135,7 @@ public class SingletonMap * * @return the key */ - public Object getKey() { + public K getKey() { return key; } @@ -145,7 +144,7 @@ public class SingletonMap * * @return the value */ - public Object getValue() { + public V getValue() { return value; } @@ -155,8 +154,8 @@ public class SingletonMap * @param value the new value to set * @return the old value */ - public Object setValue(Object value) { - Object old = this.value; + public V setValue(V value) { + V old = this.value; this.value = value; return old; } @@ -189,7 +188,7 @@ public class SingletonMap * @param key the key * @return the mapped value, null if no match */ - public Object get(Object key) { + public V get(Object key) { if (isEqualKey(key)) { return value; } @@ -247,7 +246,7 @@ public class SingletonMap * @return the value previously mapped to this key, null if none * @throws IllegalArgumentException if the key does not match */ - public Object put(Object key, Object value) { + public V put(K key, V value) { if (isEqualKey(key)) { return setValue(value); } @@ -265,21 +264,21 @@ public class SingletonMap * @throws NullPointerException if the map is null * @throws IllegalArgumentException if the key does not match */ - public void putAll(Map map) { + public void putAll(Map<? extends K, ? extends V> map) { switch (map.size()) { case 0: return; - + case 1: - Map.Entry entry = (Map.Entry) map.entrySet().iterator().next(); + Map.Entry<? extends K, ? extends V> entry = map.entrySet().iterator().next(); put(entry.getKey(), entry.getValue()); return; - + default: throw new IllegalArgumentException("The map size must be 0 or 1"); } } - + /** * Unsupported operation. * @@ -287,7 +286,7 @@ public class SingletonMap * @return the value mapped to the removed key, null if key not in map * @throws UnsupportedOperationException always */ - public Object remove(Object key) { + public V remove(Object key) { throw new UnsupportedOperationException(); } @@ -306,8 +305,8 @@ public class SingletonMap * * @return the entrySet view */ - public Set entrySet() { - Map.Entry entry = new TiedMapEntry(this, getKey()); + public Set<Map.Entry<K, V>> entrySet() { + Map.Entry<K, V> entry = new TiedMapEntry<K, V>(this, getKey()); return Collections.singleton(entry); } @@ -318,7 +317,7 @@ public class SingletonMap * * @return the keySet view */ - public Set keySet() { + public Set<K> keySet() { return Collections.singleton(key); } @@ -329,38 +328,15 @@ public class SingletonMap * * @return the values view */ - public Collection values() { - return new SingletonValues(this); + public Collection<V> values() { + return new SingletonValues<V>(this); } /** - * Gets an iterator over the map. - * Changes made to the iterator using <code>setValue</code> affect this map. - * The <code>remove</code> method is unsupported. - * <p> - * A MapIterator returns the keys in the map. It also provides convenient - * methods to get the key and value, and set the value. - * It avoids the need to create an entrySet/keySet/values object. - * It also avoids creating the Map Entry object. - * - * @return the map iterator - */ - public MapIterator mapIterator() { - return new SingletonMapIterator(this); - } - - // OrderedMap - //----------------------------------------------------------------------- - /** - * Obtains an <code>OrderedMapIterator</code> over the map. - * <p> - * A ordered map iterator is an efficient way of iterating over maps - * in both directions. - * - * @return an ordered map iterator + * {@inheritDoc} */ - public OrderedMapIterator orderedMapIterator() { - return new SingletonMapIterator(this); + public OrderedMapIterator<K, V> mapIterator() { + return new SingletonMapIterator<K, V>(this); } /** @@ -368,7 +344,7 @@ public class SingletonMap * * @return the key */ - public Object firstKey() { + public K firstKey() { return getKey(); } @@ -377,7 +353,7 @@ public class SingletonMap * * @return the key */ - public Object lastKey() { + public K lastKey() { return getKey(); } @@ -387,7 +363,7 @@ public class SingletonMap * @param key the next key * @return null always */ - public Object nextKey(Object key) { + public K nextKey(K key) { return null; } @@ -397,7 +373,7 @@ public class SingletonMap * @param key the next key * @return null always */ - public Object previousKey(Object key) { + public K previousKey(K key) { return null; } @@ -426,12 +402,12 @@ public class SingletonMap /** * SingletonMapIterator. */ - static class SingletonMapIterator implements OrderedMapIterator, ResettableIterator { - private final SingletonMap parent; + static class SingletonMapIterator<K, V> implements OrderedMapIterator<K, V>, ResettableIterator<K> { + private final SingletonMap<K, V> parent; private boolean hasNext = true; private boolean canGetSet = false; - SingletonMapIterator(SingletonMap parent) { + SingletonMapIterator(SingletonMap<K, V> parent) { super(); this.parent = parent; } @@ -440,7 +416,7 @@ public class SingletonMap return hasNext; } - public Object next() { + public K next() { if (hasNext == false) { throw new NoSuchElementException(AbstractHashedMap.NO_NEXT_ENTRY); } @@ -453,7 +429,7 @@ public class SingletonMap return (hasNext == false); } - public Object previous() { + public K previous() { if (hasNext == true) { throw new NoSuchElementException(AbstractHashedMap.NO_PREVIOUS_ENTRY); } @@ -465,21 +441,21 @@ public class SingletonMap throw new UnsupportedOperationException(); } - public Object getKey() { + public K getKey() { if (canGetSet == false) { throw new IllegalStateException(AbstractHashedMap.GETKEY_INVALID); } return parent.getKey(); } - public Object getValue() { + public V getValue() { if (canGetSet == false) { throw new IllegalStateException(AbstractHashedMap.GETVALUE_INVALID); } return parent.getValue(); } - public Object setValue(Object value) { + public V setValue(V value) { if (canGetSet == false) { throw new IllegalStateException(AbstractHashedMap.SETVALUE_INVALID); } @@ -493,9 +469,8 @@ public class SingletonMap public String toString() { if (hasNext) { return "Iterator[]"; - } else { - return "Iterator[" + getKey() + "=" + getValue() + "]"; } + return "Iterator[" + getKey() + "=" + getValue() + "]"; } } @@ -503,11 +478,11 @@ public class SingletonMap * Values implementation for the SingletonMap. * This class is needed as values is a view that must update as the map updates. */ - static class SingletonValues extends AbstractSet implements Serializable { + static class SingletonValues<V> extends AbstractSet<V> implements Serializable { private static final long serialVersionUID = -3689524741863047872L; - private final SingletonMap parent; + private final SingletonMap<?, V> parent; - SingletonValues(SingletonMap parent) { + SingletonValues(SingletonMap<?, V> parent) { super(); this.parent = parent; } @@ -524,8 +499,8 @@ public class SingletonMap public void clear() { throw new UnsupportedOperationException(); } - public Iterator iterator() { - return new SingletonIterator(parent.getValue(), false); + public Iterator<V> iterator() { + return new SingletonIterator<V>(parent.getValue(), false); } } @@ -535,10 +510,10 @@ public class SingletonMap * * @return a shallow clone */ - public Object clone() { + @SuppressWarnings("unchecked") + public SingletonMap<K, V> clone() { try { - SingletonMap cloned = (SingletonMap) super.clone(); - return cloned; + return (SingletonMap<K, V>) super.clone(); } catch (CloneNotSupportedException ex) { throw new InternalError(); } @@ -550,6 +525,7 @@ public class SingletonMap * @param obj the object to compare to * @return true if equal */ + @SuppressWarnings("unchecked") public boolean equals(Object obj) { if (obj == this) { return true;
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/map/StaticBucketMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/map/StaticBucketMap.java b/src/java/org/apache/commons/collections/map/StaticBucketMap.java index 461ff10..7486d70 100644 --- a/src/java/org/apache/commons/collections/map/StaticBucketMap.java +++ b/src/java/org/apache/commons/collections/map/StaticBucketMap.java @@ -101,12 +101,12 @@ import org.apache.commons.collections.KeyValue; * @author Janek Bogucki * @author Kazuya Ujihara */ -public final class StaticBucketMap implements Map { +public final class StaticBucketMap<K, V> implements Map<K, V> { /** The default number of buckets to use */ private static final int DEFAULT_BUCKETS = 255; /** The array of buckets, where the actual data is held */ - private Node[] buckets; + private Node<K, V>[] buckets; /** The matching array of locks */ private Lock[] locks; @@ -127,6 +127,7 @@ public final class StaticBucketMap implements Map { * * @param numBuckets the number of buckets for this map */ + @SuppressWarnings("unchecked") public StaticBucketMap(int numBuckets) { int size = Math.max(17, numBuckets); @@ -202,11 +203,11 @@ public final class StaticBucketMap implements Map { * @param key the key to retrieve * @return the associated value */ - public Object get(final Object key) { + public V get(final Object key) { int hash = getHash(key); synchronized (locks[hash]) { - Node n = buckets[hash]; + Node<K, V> n = buckets[hash]; while (n != null) { if (n.key == key || (n.key != null && n.key.equals(key))) { @@ -229,7 +230,7 @@ public final class StaticBucketMap implements Map { int hash = getHash(key); synchronized (locks[hash]) { - Node n = buckets[hash]; + Node<K, V> n = buckets[hash]; while (n != null) { if (n.key == key || (n.key != null && n.key.equals(key))) { @@ -251,7 +252,7 @@ public final class StaticBucketMap implements Map { public boolean containsValue(final Object value) { for (int i = 0; i < buckets.length; i++) { synchronized (locks[i]) { - Node n = buckets[i]; + Node<K, V> n = buckets[i]; while (n != null) { if (n.value == value || (n.value != null && n.value.equals(value))) { @@ -273,14 +274,14 @@ public final class StaticBucketMap implements Map { * @param value the value to use * @return the previous mapping for the key */ - public Object put(final Object key, final Object value) { + public V put(final K key, final V value) { int hash = getHash(key); synchronized (locks[hash]) { - Node n = buckets[hash]; + Node<K, V> n = buckets[hash]; if (n == null) { - n = new Node(); + n = new Node<K, V>(); n.key = key; n.value = value; buckets[hash] = n; @@ -291,11 +292,11 @@ public final class StaticBucketMap implements Map { // Set n to the last node in the linked list. Check each key along the way // If the key is found, then change the value of that node and return // the old value. - for (Node next = n; next != null; next = next.next) { + for (Node<K, V> next = n; next != null; next = next.next) { n = next; if (n.key == key || (n.key != null && n.key.equals(key))) { - Object returnVal = n.value; + V returnVal = n.value; n.value = value; return returnVal; } @@ -303,7 +304,7 @@ public final class StaticBucketMap implements Map { // The key was not found in the current list of nodes, add it to the end // in a new node. - Node newNode = new Node(); + Node<K, V> newNode = new Node<K, V>(); newNode.key = key; newNode.value = value; n.next = newNode; @@ -318,12 +319,12 @@ public final class StaticBucketMap implements Map { * @param key the key to remove * @return the previous value at this key */ - public Object remove(Object key) { + public V remove(Object key) { int hash = getHash(key); synchronized (locks[hash]) { - Node n = buckets[hash]; - Node prev = null; + Node<K, V> n = buckets[hash]; + Node<K, V> prev = null; while (n != null) { if (n.key == key || (n.key != null && n.key.equals(key))) { @@ -345,14 +346,14 @@ public final class StaticBucketMap implements Map { } return null; } - + //----------------------------------------------------------------------- /** * Gets the key set. * * @return the key set */ - public Set keySet() { + public Set<K> keySet() { return new KeySet(); } @@ -361,7 +362,7 @@ public final class StaticBucketMap implements Map { * * @return the values */ - public Collection values() { + public Collection<V> values() { return new Values(); } @@ -370,7 +371,7 @@ public final class StaticBucketMap implements Map { * * @return the entry set */ - public Set entrySet() { + public Set<Map.Entry<K, V>> entrySet() { return new EntrySet(); } @@ -381,11 +382,11 @@ public final class StaticBucketMap implements Map { * * @param map the map of entries to add */ - public void putAll(Map map) { - Iterator i = map.keySet().iterator(); + public void putAll(Map<? extends K, ? extends V> map) { + Iterator<? extends K> i = map.keySet().iterator(); while (i.hasNext()) { - Object key = i.next(); + K key = i.next(); put(key, map.get(key)); } } @@ -416,7 +417,7 @@ public final class StaticBucketMap implements Map { if (obj instanceof Map == false) { return false; } - Map other = (Map) obj; + Map<?, ?> other = (Map<?, ?>) obj; return entrySet().equals(other.entrySet()); } @@ -430,7 +431,7 @@ public final class StaticBucketMap implements Map { for (int i = 0; i < buckets.length; i++) { synchronized (locks[i]) { - Node n = buckets[i]; + Node<K, V> n = buckets[i]; while (n != null) { hashCode += n.hashCode(); @@ -445,16 +446,16 @@ public final class StaticBucketMap implements Map { /** * The Map.Entry for the StaticBucketMap. */ - private static final class Node implements Map.Entry, KeyValue { - protected Object key; - protected Object value; - protected Node next; + private static final class Node<K, V> implements Map.Entry<K, V>, KeyValue<K, V> { + protected K key; + protected V value; + protected Node<K, V> next; - public Object getKey() { + public K getKey() { return key; } - public Object getValue() { + public V getValue() { return value; } @@ -471,20 +472,19 @@ public final class StaticBucketMap implements Map { return false; } - Map.Entry e2 = (Map.Entry) obj; + Map.Entry<?, ?> e2 = (Map.Entry<?, ?>) obj; return ( (key == null ? e2.getKey() == null : key.equals(e2.getKey())) && (value == null ? e2.getValue() == null : value.equals(e2.getValue()))); } - public Object setValue(Object obj) { - Object retVal = value; + public V setValue(V obj) { + V retVal = value; value = obj; return retVal; } } - /** * The lock object, which also includes a count of the nodes in this lock. */ @@ -492,20 +492,17 @@ public final class StaticBucketMap implements Map { public int size; } - //----------------------------------------------------------------------- - private class EntryIterator implements Iterator { - - private ArrayList current = new ArrayList(); + private class BaseIterator { + private ArrayList<Map.Entry<K, V>> current = new ArrayList<Map.Entry<K,V>>(); private int bucket; - private Map.Entry last; - + private Map.Entry<K, V> last; public boolean hasNext() { if (current.size() > 0) return true; while (bucket < buckets.length) { synchronized (locks[bucket]) { - Node n = buckets[bucket]; + Node<K, V> n = buckets[bucket]; while (n != null) { current.add(n); n = n.next; @@ -517,41 +514,44 @@ public final class StaticBucketMap implements Map { return false; } - protected Map.Entry nextEntry() { + protected Map.Entry<K, V> nextEntry() { if (!hasNext()) throw new NoSuchElementException(); - last = (Map.Entry)current.remove(current.size() - 1); + last = current.remove(current.size() - 1); return last; } - public Object next() { - return nextEntry(); - } - public void remove() { if (last == null) throw new IllegalStateException(); StaticBucketMap.this.remove(last.getKey()); last = null; } + } + + private class EntryIterator extends BaseIterator implements Iterator<Map.Entry<K, V>> { + + public Map.Entry<K, V> next() { + return nextEntry(); + } } - private class ValueIterator extends EntryIterator { + private class ValueIterator extends BaseIterator implements Iterator<V> { - public Object next() { + public V next() { return nextEntry().getValue(); } } - private class KeyIterator extends EntryIterator { + private class KeyIterator extends BaseIterator implements Iterator<K> { - public Object next() { + public K next() { return nextEntry().getKey(); } } - private class EntrySet extends AbstractSet { + private class EntrySet extends AbstractSet<Map.Entry<K, V>> { public int size() { return StaticBucketMap.this.size(); @@ -561,15 +561,15 @@ public final class StaticBucketMap implements Map { StaticBucketMap.this.clear(); } - public Iterator iterator() { + public Iterator<Map.Entry<K, V>> iterator() { return new EntryIterator(); } public boolean contains(Object obj) { - Map.Entry entry = (Map.Entry) obj; + Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj; int hash = getHash(entry.getKey()); synchronized (locks[hash]) { - for (Node n = buckets[hash]; n != null; n = n.next) { + for (Node<K, V> n = buckets[hash]; n != null; n = n.next) { if (n.equals(entry)) return true; } } @@ -580,10 +580,10 @@ public final class StaticBucketMap implements Map { if (obj instanceof Map.Entry == false) { return false; } - Map.Entry entry = (Map.Entry) obj; + Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj; int hash = getHash(entry.getKey()); synchronized (locks[hash]) { - for (Node n = buckets[hash]; n != null; n = n.next) { + for (Node<K, V> n = buckets[hash]; n != null; n = n.next) { if (n.equals(entry)) { StaticBucketMap.this.remove(n.getKey()); return true; @@ -595,8 +595,7 @@ public final class StaticBucketMap implements Map { } - - private class KeySet extends AbstractSet { + private class KeySet extends AbstractSet<K> { public int size() { return StaticBucketMap.this.size(); @@ -606,7 +605,7 @@ public final class StaticBucketMap implements Map { StaticBucketMap.this.clear(); } - public Iterator iterator() { + public Iterator<K> iterator() { return new KeyIterator(); } @@ -617,7 +616,7 @@ public final class StaticBucketMap implements Map { public boolean remove(Object obj) { int hash = getHash(obj); synchronized (locks[hash]) { - for (Node n = buckets[hash]; n != null; n = n.next) { + for (Node<K, V> n = buckets[hash]; n != null; n = n.next) { Object k = n.getKey(); if ((k == obj) || ((k != null) && k.equals(obj))) { StaticBucketMap.this.remove(k); @@ -626,13 +625,12 @@ public final class StaticBucketMap implements Map { } } return false; - } } - private class Values extends AbstractCollection { + private class Values extends AbstractCollection<V> { public int size() { return StaticBucketMap.this.size(); @@ -642,13 +640,12 @@ public final class StaticBucketMap implements Map { StaticBucketMap.this.clear(); } - public Iterator iterator() { + public Iterator<V> iterator() { return new ValueIterator(); } } - /** * Prevents any operations from occurring on this map while the * given {@link Runnable} executes. This method can be used, for http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/map/TransformedMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/map/TransformedMap.java b/src/java/org/apache/commons/collections/map/TransformedMap.java index 499facf..4df0f2e 100644 --- a/src/java/org/apache/commons/collections/map/TransformedMap.java +++ b/src/java/org/apache/commons/collections/map/TransformedMap.java @@ -20,7 +20,6 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; -import java.util.Iterator; import java.util.Map; import org.apache.commons.collections.Transformer; @@ -46,17 +45,17 @@ import org.apache.commons.collections.Transformer; * * @author Stephen Colebourne */ -public class TransformedMap - extends AbstractInputCheckedMapDecorator +public class TransformedMap<K, V> + extends AbstractInputCheckedMapDecorator<K, V> implements Serializable { /** Serialization version */ private static final long serialVersionUID = 7023152376788900464L; /** The transformer to use for the key */ - protected final Transformer keyTransformer; + protected final Transformer<? super K, ? extends K> keyTransformer; /** The transformer to use for the value */ - protected final Transformer valueTransformer; + protected final Transformer<? super V, ? extends V> valueTransformer; /** * Factory method to create a transforming map. @@ -70,8 +69,10 @@ public class TransformedMap * @param valueTransformer the transformer to use for value conversion, null means no transformation * @throws IllegalArgumentException if map is null */ - public static Map decorate(Map map, Transformer keyTransformer, Transformer valueTransformer) { - return new TransformedMap(map, keyTransformer, valueTransformer); + public static <K, V> Map<K, V> decorate(Map<K, V> map, + Transformer<? super K, ? extends K> keyTransformer, + Transformer<? super V, ? extends V> valueTransformer) { + return new TransformedMap<K, V>(map, keyTransformer, valueTransformer); } /** @@ -88,10 +89,12 @@ public class TransformedMap * @throws IllegalArgumentException if map is null * @since Commons Collections 3.2 */ - public static Map decorateTransform(Map map, Transformer keyTransformer, Transformer valueTransformer) { - TransformedMap decorated = new TransformedMap(map, keyTransformer, valueTransformer); + public static <K, V> Map<K, V> decorateTransform(Map<K, V> map, + Transformer<? super K, ? extends K> keyTransformer, + Transformer<? super V, ? extends V> valueTransformer) { + TransformedMap<K, V> decorated = new TransformedMap<K, V>(map, keyTransformer, valueTransformer); if (map.size() > 0) { - Map transformed = decorated.transformMap(map); + Map<K, V> transformed = decorated.transformMap(map); decorated.clear(); decorated.decorated().putAll(transformed); // avoids double transformation } @@ -110,7 +113,8 @@ public class TransformedMap * @param valueTransformer the transformer to use for value conversion, null means no conversion * @throws IllegalArgumentException if map is null */ - protected TransformedMap(Map map, Transformer keyTransformer, Transformer valueTransformer) { + protected TransformedMap(Map<K, V> map, Transformer<? super K, ? extends K> keyTransformer, + Transformer<? super V, ? extends V> valueTransformer) { super(map); this.keyTransformer = keyTransformer; this.valueTransformer = valueTransformer; @@ -137,6 +141,7 @@ public class TransformedMap * @throws ClassNotFoundException * @since Commons Collections 3.1 */ + @SuppressWarnings("unchecked") private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); map = (Map) in.readObject(); @@ -151,7 +156,7 @@ public class TransformedMap * @param object the object to transform * @throws the transformed object */ - protected Object transformKey(Object object) { + protected K transformKey(K object) { if (keyTransformer == null) { return object; } @@ -166,7 +171,7 @@ public class TransformedMap * @param object the object to transform * @throws the transformed object */ - protected Object transformValue(Object object) { + protected V transformValue(V object) { if (valueTransformer == null) { return object; } @@ -181,14 +186,15 @@ public class TransformedMap * @param map the map to transform * @throws the transformed object */ - protected Map transformMap(Map map) { + @SuppressWarnings("unchecked") + protected Map<K, V> transformMap(Map<? extends K, ? extends V> map) { if (map.isEmpty()) { - return map; + return (Map<K, V>) map; } - Map result = new LinkedMap(map.size()); - for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) { - Map.Entry entry = (Map.Entry) it.next(); - result.put(transformKey(entry.getKey()), transformValue(entry.getValue())); + Map<K, V> result = new LinkedMap<K, V>(map.size()); + + for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) { + result.put((K) transformKey(entry.getKey()), transformValue(entry.getValue())); } return result; } @@ -200,7 +206,7 @@ public class TransformedMap * @return the transformed value * @since Commons Collections 3.1 */ - protected Object checkSetValue(Object value) { + protected V checkSetValue(V value) { return valueTransformer.transform(value); } @@ -215,13 +221,13 @@ public class TransformedMap } //----------------------------------------------------------------------- - public Object put(Object key, Object value) { + public V put(K key, V value) { key = transformKey(key); value = transformValue(value); return decorated().put(key, value); } - public void putAll(Map mapToCopy) { + public void putAll(Map<? extends K, ? extends V> mapToCopy) { mapToCopy = transformMap(mapToCopy); decorated().putAll(mapToCopy); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/map/TransformedSortedMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/map/TransformedSortedMap.java b/src/java/org/apache/commons/collections/map/TransformedSortedMap.java index abf782b..5957736 100644 --- a/src/java/org/apache/commons/collections/map/TransformedSortedMap.java +++ b/src/java/org/apache/commons/collections/map/TransformedSortedMap.java @@ -43,9 +43,9 @@ import org.apache.commons.collections.Transformer; * * @author Stephen Colebourne */ -public class TransformedSortedMap - extends TransformedMap - implements SortedMap { +public class TransformedSortedMap<K, V> + extends TransformedMap<K, V> + implements SortedMap<K, V> { /** Serialization version */ private static final long serialVersionUID = -8751771676410385778L; @@ -62,8 +62,10 @@ public class TransformedSortedMap * @param valueTransformer the predicate to validate to values, null means no transformation * @throws IllegalArgumentException if the map is null */ - public static SortedMap decorate(SortedMap map, Transformer keyTransformer, Transformer valueTransformer) { - return new TransformedSortedMap(map, keyTransformer, valueTransformer); + public static <K, V> SortedMap<K, V> decorate(SortedMap<K, V> map, + Transformer<? super K, ? extends K> keyTransformer, + Transformer<? super V, ? extends V> valueTransformer) { + return new TransformedSortedMap<K, V>(map, keyTransformer, valueTransformer); } /** @@ -80,10 +82,12 @@ public class TransformedSortedMap * @throws IllegalArgumentException if map is null * @since Commons Collections 3.2 */ - public static SortedMap decorateTransform(SortedMap map, Transformer keyTransformer, Transformer valueTransformer) { - TransformedSortedMap decorated = new TransformedSortedMap(map, keyTransformer, valueTransformer); + public static <K, V> SortedMap<K, V> decorateTransform(SortedMap<K, V> map, + Transformer<? super K, ? extends K> keyTransformer, + Transformer<? super V, ? extends V> valueTransformer) { + TransformedSortedMap<K, V> decorated = new TransformedSortedMap<K, V>(map, keyTransformer, valueTransformer); if (map.size() > 0) { - Map transformed = decorated.transformMap(map); + Map<K, V> transformed = decorated.transformMap(map); decorated.clear(); decorated.decorated().putAll(transformed); // avoids double transformation } @@ -102,7 +106,9 @@ public class TransformedSortedMap * @param valueTransformer the predicate to validate to values, null means no transformation * @throws IllegalArgumentException if the map is null */ - protected TransformedSortedMap(SortedMap map, Transformer keyTransformer, Transformer valueTransformer) { + protected TransformedSortedMap(SortedMap<K, V> map, + Transformer<? super K, ? extends K> keyTransformer, + Transformer<? super V, ? extends V> valueTransformer) { super(map, keyTransformer, valueTransformer); } @@ -112,36 +118,36 @@ public class TransformedSortedMap * * @return the decorated map */ - protected SortedMap getSortedMap() { - return (SortedMap) map; + protected SortedMap<K, V> getSortedMap() { + return (SortedMap<K, V>) map; } //----------------------------------------------------------------------- - public Object firstKey() { + public K firstKey() { return getSortedMap().firstKey(); } - public Object lastKey() { + public K lastKey() { return getSortedMap().lastKey(); } - public Comparator comparator() { + public Comparator<? super K> comparator() { return getSortedMap().comparator(); } - public SortedMap subMap(Object fromKey, Object toKey) { - SortedMap map = getSortedMap().subMap(fromKey, toKey); - return new TransformedSortedMap(map, keyTransformer, valueTransformer); + public SortedMap<K, V> subMap(K fromKey, K toKey) { + SortedMap<K, V> map = getSortedMap().subMap(fromKey, toKey); + return new TransformedSortedMap<K, V>(map, keyTransformer, valueTransformer); } - public SortedMap headMap(Object toKey) { - SortedMap map = getSortedMap().headMap(toKey); - return new TransformedSortedMap(map, keyTransformer, valueTransformer); + public SortedMap<K, V> headMap(K toKey) { + SortedMap<K, V> map = getSortedMap().headMap(toKey); + return new TransformedSortedMap<K, V>(map, keyTransformer, valueTransformer); } - public SortedMap tailMap(Object fromKey) { - SortedMap map = getSortedMap().tailMap(fromKey); - return new TransformedSortedMap(map, keyTransformer, valueTransformer); + public SortedMap<K, V> tailMap(K fromKey) { + SortedMap<K, V> map = getSortedMap().tailMap(fromKey); + return new TransformedSortedMap<K, V>(map, keyTransformer, valueTransformer); } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/map/UnmodifiableEntrySet.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/map/UnmodifiableEntrySet.java b/src/java/org/apache/commons/collections/map/UnmodifiableEntrySet.java index 081e715..afb088e 100644 --- a/src/java/org/apache/commons/collections/map/UnmodifiableEntrySet.java +++ b/src/java/org/apache/commons/collections/map/UnmodifiableEntrySet.java @@ -35,8 +35,11 @@ import org.apache.commons.collections.set.AbstractSetDecorator; * * @author Stephen Colebourne */ -public final class UnmodifiableEntrySet - extends AbstractSetDecorator implements Unmodifiable { +public final class UnmodifiableEntrySet<K, V> + extends AbstractSetDecorator<Map.Entry<K, V>> implements Unmodifiable { + + /** Serialization version */ + private static final long serialVersionUID = 1678353579659253473L; /** * Factory method to create an unmodifiable set of Map Entry objects. @@ -44,11 +47,11 @@ public final class UnmodifiableEntrySet * @param set the set to decorate, must not be null * @throws IllegalArgumentException if set is null */ - public static Set decorate(Set set) { + public static <K, V> Set<Map.Entry<K, V>> decorate(Set<Map.Entry<K, V>> set) { if (set instanceof Unmodifiable) { return set; } - return new UnmodifiableEntrySet(set); + return new UnmodifiableEntrySet<K, V>(set); } //----------------------------------------------------------------------- @@ -58,16 +61,16 @@ public final class UnmodifiableEntrySet * @param set the set to decorate, must not be null * @throws IllegalArgumentException if set is null */ - private UnmodifiableEntrySet(Set set) { + private UnmodifiableEntrySet(Set<Map.Entry<K, V>> set) { super(set); } //----------------------------------------------------------------------- - public boolean add(Object object) { + public boolean add(Map.Entry<K, V> object) { throw new UnsupportedOperationException(); } - public boolean addAll(Collection coll) { + public boolean addAll(Collection<? extends Map.Entry<K, V>> coll) { throw new UnsupportedOperationException(); } @@ -79,28 +82,30 @@ public final class UnmodifiableEntrySet throw new UnsupportedOperationException(); } - public boolean removeAll(Collection coll) { + public boolean removeAll(Collection<?> coll) { throw new UnsupportedOperationException(); } - public boolean retainAll(Collection coll) { + public boolean retainAll(Collection<?> coll) { throw new UnsupportedOperationException(); } //----------------------------------------------------------------------- - public Iterator iterator() { + public Iterator<Map.Entry<K, V>> iterator() { return new UnmodifiableEntrySetIterator(collection.iterator()); } + @SuppressWarnings("unchecked") public Object[] toArray() { Object[] array = collection.toArray(); for (int i = 0; i < array.length; i++) { - array[i] = new UnmodifiableEntry((Map.Entry) array[i]); + array[i] = new UnmodifiableEntry((Map.Entry<K, V>) array[i]); } return array; } - public Object[] toArray(Object array[]) { + @SuppressWarnings("unchecked") + public <T> T[] toArray(T[] array) { Object[] result = array; if (array.length > 0) { // we must create a new array to handle multi-threaded situations @@ -109,15 +114,15 @@ public final class UnmodifiableEntrySet } result = collection.toArray(result); for (int i = 0; i < result.length; i++) { - result[i] = new UnmodifiableEntry((Map.Entry) result[i]); + result[i] = new UnmodifiableEntry((Map.Entry<K, V>) result[i]); } // check to see if result should be returned straight if (result.length > array.length) { - return result; + return (T[]) result; } - // copy back into input array to fulfil the method contract + // copy back into input array to fulfill the method contract System.arraycopy(result, 0, array, 0, result.length); if (array.length > result.length) { array[result.length] = null; @@ -129,17 +134,16 @@ public final class UnmodifiableEntrySet /** * Implementation of an entry set iterator. */ - final static class UnmodifiableEntrySetIterator extends AbstractIteratorDecorator { - - protected UnmodifiableEntrySetIterator(Iterator iterator) { + private class UnmodifiableEntrySetIterator extends AbstractIteratorDecorator<Map.Entry<K, V>> { + + protected UnmodifiableEntrySetIterator(Iterator<Map.Entry<K, V>> iterator) { super(iterator); } - - public Object next() { - Map.Entry entry = (Map.Entry) iterator.next(); - return new UnmodifiableEntry(entry); + + public Map.Entry<K, V> next() { + return new UnmodifiableEntry(iterator.next()); } - + public void remove() { throw new UnsupportedOperationException(); } @@ -149,13 +153,13 @@ public final class UnmodifiableEntrySet /** * Implementation of a map entry that is unmodifiable. */ - final static class UnmodifiableEntry extends AbstractMapEntryDecorator { + private class UnmodifiableEntry extends AbstractMapEntryDecorator<K, V> { - protected UnmodifiableEntry(Map.Entry entry) { + protected UnmodifiableEntry(Map.Entry<K, V> entry) { super(entry); } - public Object setValue(Object obj) { + public V setValue(V obj) { throw new UnsupportedOperationException(); } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/map/UnmodifiableMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/map/UnmodifiableMap.java b/src/java/org/apache/commons/collections/map/UnmodifiableMap.java index cbf085f..0239db1 100644 --- a/src/java/org/apache/commons/collections/map/UnmodifiableMap.java +++ b/src/java/org/apache/commons/collections/map/UnmodifiableMap.java @@ -42,9 +42,9 @@ import org.apache.commons.collections.set.UnmodifiableSet; * * @author Stephen Colebourne */ -public final class UnmodifiableMap - extends AbstractMapDecorator - implements IterableMap, Unmodifiable, Serializable { +public final class UnmodifiableMap<K, V> + extends AbstractMapDecorator<K, V> + implements IterableMap<K, V>, Unmodifiable, Serializable { /** Serialization version */ private static final long serialVersionUID = 2737023427269031941L; @@ -55,11 +55,11 @@ public final class UnmodifiableMap * @param map the map to decorate, must not be null * @throws IllegalArgumentException if map is null */ - public static Map decorate(Map map) { + public static <K, V> Map<K, V> decorate(Map<K, V> map) { if (map instanceof Unmodifiable) { return map; } - return new UnmodifiableMap(map); + return new UnmodifiableMap<K, V>(map); } //----------------------------------------------------------------------- @@ -69,7 +69,7 @@ public final class UnmodifiableMap * @param map the map to decorate, must not be null * @throws IllegalArgumentException if map is null */ - private UnmodifiableMap(Map map) { + private UnmodifiableMap(Map<K, V> map) { super(map); } @@ -94,9 +94,10 @@ public final class UnmodifiableMap * @throws ClassNotFoundException * @since Commons Collections 3.1 */ + @SuppressWarnings("unchecked") private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); - map = (Map) in.readObject(); + map = (Map<K, V>) in.readObject(); } //----------------------------------------------------------------------- @@ -104,40 +105,39 @@ public final class UnmodifiableMap throw new UnsupportedOperationException(); } - public Object put(Object key, Object value) { + public V put(K key, V value) { throw new UnsupportedOperationException(); } - public void putAll(Map mapToCopy) { + public void putAll(Map<? extends K, ? extends V> mapToCopy) { throw new UnsupportedOperationException(); } - public Object remove(Object key) { + public V remove(Object key) { throw new UnsupportedOperationException(); } - public MapIterator mapIterator() { + public MapIterator<K, V> mapIterator() { if (map instanceof IterableMap) { - MapIterator it = ((IterableMap) map).mapIterator(); - return UnmodifiableMapIterator.decorate(it); - } else { - MapIterator it = new EntrySetMapIterator(map); + MapIterator<K, V> it = ((IterableMap<K, V>) map).mapIterator(); return UnmodifiableMapIterator.decorate(it); } + MapIterator<K, V> it = new EntrySetMapIterator<K, V>(map); + return UnmodifiableMapIterator.decorate(it); } - public Set entrySet() { - Set set = super.entrySet(); + public Set<Map.Entry<K, V>> entrySet() { + Set<Map.Entry<K, V>> set = super.entrySet(); return UnmodifiableEntrySet.decorate(set); } - public Set keySet() { - Set set = super.keySet(); + public Set<K> keySet() { + Set<K> set = super.keySet(); return UnmodifiableSet.decorate(set); } - public Collection values() { - Collection coll = super.values(); + public Collection<V> values() { + Collection<V> coll = super.values(); return UnmodifiableCollection.decorate(coll); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/map/UnmodifiableOrderedMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/map/UnmodifiableOrderedMap.java b/src/java/org/apache/commons/collections/map/UnmodifiableOrderedMap.java index d7a949a..2e42934 100644 --- a/src/java/org/apache/commons/collections/map/UnmodifiableOrderedMap.java +++ b/src/java/org/apache/commons/collections/map/UnmodifiableOrderedMap.java @@ -24,12 +24,10 @@ import java.util.Collection; import java.util.Map; import java.util.Set; -import org.apache.commons.collections.MapIterator; import org.apache.commons.collections.OrderedMap; import org.apache.commons.collections.OrderedMapIterator; import org.apache.commons.collections.Unmodifiable; import org.apache.commons.collections.collection.UnmodifiableCollection; -import org.apache.commons.collections.iterators.UnmodifiableMapIterator; import org.apache.commons.collections.iterators.UnmodifiableOrderedMapIterator; import org.apache.commons.collections.set.UnmodifiableSet; @@ -43,9 +41,8 @@ import org.apache.commons.collections.set.UnmodifiableSet; * * @author Stephen Colebourne */ -public final class UnmodifiableOrderedMap - extends AbstractOrderedMapDecorator - implements Unmodifiable, Serializable { +public final class UnmodifiableOrderedMap<K, V> extends AbstractOrderedMapDecorator<K, V> implements + Unmodifiable, Serializable { /** Serialization version */ private static final long serialVersionUID = 8136428161720526266L; @@ -56,11 +53,11 @@ public final class UnmodifiableOrderedMap * @param map the map to decorate, must not be null * @throws IllegalArgumentException if map is null */ - public static OrderedMap decorate(OrderedMap map) { + public static <K, V> OrderedMap<K, V> decorate(OrderedMap<K, V> map) { if (map instanceof Unmodifiable) { return map; } - return new UnmodifiableOrderedMap(map); + return new UnmodifiableOrderedMap<K, V>(map); } //----------------------------------------------------------------------- @@ -70,7 +67,7 @@ public final class UnmodifiableOrderedMap * @param map the map to decorate, must not be null * @throws IllegalArgumentException if map is null */ - private UnmodifiableOrderedMap(OrderedMap map) { + private UnmodifiableOrderedMap(OrderedMap<K, V> map) { super(map); } @@ -95,19 +92,15 @@ public final class UnmodifiableOrderedMap * @throws ClassNotFoundException * @since Commons Collections 3.1 */ + @SuppressWarnings("unchecked") private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); map = (Map) in.readObject(); } //----------------------------------------------------------------------- - public MapIterator mapIterator() { - MapIterator it = decorated().mapIterator(); - return UnmodifiableMapIterator.decorate(it); - } - - public OrderedMapIterator orderedMapIterator() { - OrderedMapIterator it = decorated().orderedMapIterator(); + public OrderedMapIterator<K, V> mapIterator() { + OrderedMapIterator<K, V> it = decorated().mapIterator(); return UnmodifiableOrderedMapIterator.decorate(it); } @@ -115,30 +108,30 @@ public final class UnmodifiableOrderedMap throw new UnsupportedOperationException(); } - public Object put(Object key, Object value) { + public V put(K key, V value) { throw new UnsupportedOperationException(); } - public void putAll(Map mapToCopy) { + public void putAll(Map<? extends K, ? extends V> mapToCopy) { throw new UnsupportedOperationException(); } - public Object remove(Object key) { + public V remove(Object key) { throw new UnsupportedOperationException(); } - public Set entrySet() { - Set set = super.entrySet(); + public Set<Map.Entry<K, V>> entrySet() { + Set<Map.Entry<K, V>> set = super.entrySet(); return UnmodifiableEntrySet.decorate(set); } - public Set keySet() { - Set set = super.keySet(); + public Set<K> keySet() { + Set<K> set = super.keySet(); return UnmodifiableSet.decorate(set); } - public Collection values() { - Collection coll = super.values(); + public Collection<V> values() { + Collection<V> coll = super.values(); return UnmodifiableCollection.decorate(coll); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/map/UnmodifiableSortedMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/map/UnmodifiableSortedMap.java b/src/java/org/apache/commons/collections/map/UnmodifiableSortedMap.java index 3984dad..7c78671 100644 --- a/src/java/org/apache/commons/collections/map/UnmodifiableSortedMap.java +++ b/src/java/org/apache/commons/collections/map/UnmodifiableSortedMap.java @@ -40,8 +40,8 @@ import org.apache.commons.collections.set.UnmodifiableSet; * * @author Stephen Colebourne */ -public final class UnmodifiableSortedMap - extends AbstractSortedMapDecorator +public final class UnmodifiableSortedMap<K, V> + extends AbstractSortedMapDecorator<K, V> implements Unmodifiable, Serializable { /** Serialization version */ @@ -53,11 +53,11 @@ public final class UnmodifiableSortedMap * @param map the map to decorate, must not be null * @throws IllegalArgumentException if map is null */ - public static SortedMap decorate(SortedMap map) { + public static <K, V> SortedMap<K, V> decorate(SortedMap<K, V> map) { if (map instanceof Unmodifiable) { return map; } - return new UnmodifiableSortedMap(map); + return new UnmodifiableSortedMap<K, V>(map); } //----------------------------------------------------------------------- @@ -67,10 +67,10 @@ public final class UnmodifiableSortedMap * @param map the map to decorate, must not be null * @throws IllegalArgumentException if map is null */ - private UnmodifiableSortedMap(SortedMap map) { + private UnmodifiableSortedMap(SortedMap<K, V> map) { super(map); } - + //----------------------------------------------------------------------- /** * Write the map out using a custom routine. @@ -92,9 +92,10 @@ public final class UnmodifiableSortedMap * @throws ClassNotFoundException * @since Commons Collections 3.1 */ + @SuppressWarnings("unchecked") private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); - map = (Map) in.readObject(); + map = (Map<K, V>) in.readObject(); } //----------------------------------------------------------------------- @@ -102,59 +103,53 @@ public final class UnmodifiableSortedMap throw new UnsupportedOperationException(); } - public Object put(Object key, Object value) { + public V put(K key, V value) { throw new UnsupportedOperationException(); } - public void putAll(Map mapToCopy) { + public void putAll(Map<? extends K, ? extends V> mapToCopy) { throw new UnsupportedOperationException(); } - public Object remove(Object key) { + public V remove(Object key) { throw new UnsupportedOperationException(); } - public Set entrySet() { - Set set = super.entrySet(); - return UnmodifiableEntrySet.decorate(set); + public Set<Map.Entry<K, V>> entrySet() { + return UnmodifiableEntrySet.decorate(super.entrySet()); } - public Set keySet() { - Set set = super.keySet(); - return UnmodifiableSet.decorate(set); + public Set<K> keySet() { + return UnmodifiableSet.decorate(super.keySet()); } - public Collection values() { - Collection coll = super.values(); - return UnmodifiableCollection.decorate(coll); + public Collection<V> values() { + return UnmodifiableCollection.decorate(super.values()); } //----------------------------------------------------------------------- - public Object firstKey() { + public K firstKey() { return decorated().firstKey(); } - public Object lastKey() { + public K lastKey() { return decorated().lastKey(); } - public Comparator comparator() { + public Comparator<? super K> comparator() { return decorated().comparator(); } - public SortedMap subMap(Object fromKey, Object toKey) { - SortedMap map = decorated().subMap(fromKey, toKey); - return new UnmodifiableSortedMap(map); + public SortedMap<K, V> subMap(K fromKey, K toKey) { + return new UnmodifiableSortedMap<K, V>(decorated().subMap(fromKey, toKey)); } - public SortedMap headMap(Object toKey) { - SortedMap map = decorated().headMap(toKey); - return new UnmodifiableSortedMap(map); + public SortedMap<K, V> headMap(K toKey) { + return new UnmodifiableSortedMap<K, V>(decorated().headMap(toKey)); } - public SortedMap tailMap(Object fromKey) { - SortedMap map = decorated().tailMap(fromKey); - return new UnmodifiableSortedMap(map); + public SortedMap<K, V> tailMap(K fromKey) { + return new UnmodifiableSortedMap<K, V>(decorated().tailMap(fromKey)); } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/set/AbstractSerializableSetDecorator.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/set/AbstractSerializableSetDecorator.java b/src/java/org/apache/commons/collections/set/AbstractSerializableSetDecorator.java index ba08f5c..5a0c932 100644 --- a/src/java/org/apache/commons/collections/set/AbstractSerializableSetDecorator.java +++ b/src/java/org/apache/commons/collections/set/AbstractSerializableSetDecorator.java @@ -29,8 +29,8 @@ import java.util.Set; * @author Stephen Colebourne * @since Commons Collections 3.1 */ -public abstract class AbstractSerializableSetDecorator - extends AbstractSetDecorator +public abstract class AbstractSerializableSetDecorator<E> + extends AbstractSetDecorator<E> implements Serializable { /** Serialization version */ @@ -39,7 +39,7 @@ public abstract class AbstractSerializableSetDecorator /** * Constructor. */ - protected AbstractSerializableSetDecorator(Set set) { + protected AbstractSerializableSetDecorator(Set<E> set) { super(set); } @@ -62,9 +62,10 @@ public abstract class AbstractSerializableSetDecorator * @throws IOException * @throws ClassNotFoundException */ + @SuppressWarnings("unchecked") private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); - collection = (Collection) in.readObject(); + collection = (Collection<E>) in.readObject(); } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/set/AbstractSetDecorator.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/set/AbstractSetDecorator.java b/src/java/org/apache/commons/collections/set/AbstractSetDecorator.java index 920fceb..dffc0c5 100644 --- a/src/java/org/apache/commons/collections/set/AbstractSetDecorator.java +++ b/src/java/org/apache/commons/collections/set/AbstractSetDecorator.java @@ -31,9 +31,11 @@ import org.apache.commons.collections.collection.AbstractCollectionDecorator; * * @author Stephen Colebourne */ -public abstract class AbstractSetDecorator<E> - extends AbstractCollectionDecorator<E> - implements Set<E> { +public abstract class AbstractSetDecorator<E> extends AbstractCollectionDecorator<E> implements + Set<E> { + + /** Serialization version */ + private static final long serialVersionUID = -4678668309576958546L; /** * Constructor only used in deserialization, do not use otherwise. http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/set/AbstractSortedSetDecorator.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/set/AbstractSortedSetDecorator.java b/src/java/org/apache/commons/collections/set/AbstractSortedSetDecorator.java index 6d9dcb2..e26fe9f 100644 --- a/src/java/org/apache/commons/collections/set/AbstractSortedSetDecorator.java +++ b/src/java/org/apache/commons/collections/set/AbstractSortedSetDecorator.java @@ -35,6 +35,9 @@ public abstract class AbstractSortedSetDecorator<E> extends AbstractSetDecorator<E> implements SortedSet<E> { + /** Serialization version */ + private static final long serialVersionUID = -3462240946294214398L; + /** * Constructor only used in deserialization, do not use otherwise. * @since Commons Collections 3.1 http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/set/CompositeSet.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/set/CompositeSet.java b/src/java/org/apache/commons/collections/set/CompositeSet.java index 90d8f35..1e53d8d 100644 --- a/src/java/org/apache/commons/collections/set/CompositeSet.java +++ b/src/java/org/apache/commons/collections/set/CompositeSet.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,7 +17,7 @@ package org.apache.commons.collections.set; import java.util.Collection; -import java.util.Iterator; +import java.util.List; import java.util.Set; import org.apache.commons.collections.CollectionUtils; @@ -35,29 +35,30 @@ import org.apache.commons.collections.collection.CompositeCollection; * * @author Brian McCallister */ -public class CompositeSet extends CompositeCollection implements Set { +public class CompositeSet<E> extends CompositeCollection<E> implements Set<E> { + /** * Create an empty CompositeSet */ public CompositeSet() { super(); } - + /** * Create a CompositeSet with just <code>set</code> composited * @param set The initial set in the composite */ - public CompositeSet(Set set) { + public CompositeSet(Set<E> set) { super(set); } - + /** * Create a composite set with sets as the initial set of composited Sets */ - public CompositeSet(Set[] sets) { + public CompositeSet(Set<E>[] sets) { super(sets); } - + /** * Add a Set to this composite * @@ -69,14 +70,13 @@ public class CompositeSet extends CompositeCollection implements Set { * @see org.apache.commons.collections.collection.CompositeCollection.CollectionMutator * @see SetMutator */ - public synchronized void addComposited(Collection c) { + public synchronized void addComposited(Collection<E> c) { if (!(c instanceof Set)) { throw new IllegalArgumentException("Collections added must implement java.util.Set"); } - - for (Iterator i = this.getCollections().iterator(); i.hasNext();) { - Set set = (Set) i.next(); - Collection intersects = CollectionUtils.intersection(set, c); + + for (Set<E> set : getCollections()) { + Collection<E> intersects = CollectionUtils.intersection(set, c); if (intersects.size() > 0) { if (this.mutator == null) { throw new UnsupportedOperationException( @@ -86,38 +86,48 @@ public class CompositeSet extends CompositeCollection implements Set { throw new UnsupportedOperationException( "Collision adding composited collection to a CompositeSet with a CollectionMutator instead of a SetMutator"); } - ((SetMutator) this.mutator).resolveCollision(this, set, (Set) c, intersects); + getMutator().resolveCollision(this, set, (Set<E>) c, intersects); if (CollectionUtils.intersection(set, c).size() > 0) { throw new IllegalArgumentException( "Attempt to add illegal entry unresolved by SetMutator.resolveCollision()"); } } } - super.addComposited(new Collection[]{c}); + super.addComposited(c); } - + + /** + * {@inheritDoc} + */ + @SuppressWarnings("unchecked") + @Override + public List<? extends Set<E>> getCollections() { + return (List<Set<E>>) super.getCollections(); + } + /** * Add two sets to this composite * * @throws IllegalArgumentException if c or d does not implement java.util.Set */ - public synchronized void addComposited(Collection c, Collection d) { + @SuppressWarnings("unchecked") + public synchronized void addComposited(Collection<E> c, Collection<E> d) { if (!(c instanceof Set)) throw new IllegalArgumentException("Argument must implement java.util.Set"); if (!(d instanceof Set)) throw new IllegalArgumentException("Argument must implement java.util.Set"); - this.addComposited(new Set[]{(Set) c, (Set) d}); + this.addComposited(new Set[] { (Set<? extends E>) c, (Set<? extends E>) d }); } - + /** * Add an array of sets to this composite * @param comps * @throws IllegalArgumentException if any of the collections in comps do not implement Set */ - public synchronized void addComposited(Collection[] comps) { + public synchronized void addComposited(Collection<E>[] comps) { for (int i = comps.length - 1; i >= 0; --i) { this.addComposited(comps[i]); } } - + /** * This can receive either a <code>CompositeCollection.CollectionMutator</code> * or a <code>CompositeSet.SetMutator</code>. If a @@ -125,12 +135,12 @@ public class CompositeSet extends CompositeCollection implements Set { * composited sets will throw IllegalArgumentException * <p> */ - public void setMutator(CollectionMutator mutator) { + public void setMutator(CollectionMutator<E> mutator) { super.setMutator(mutator); } - + /* Set operations */ - + /** * If a <code>CollectionMutator</code> is defined for this CompositeSet then this * method will be called anyway. @@ -139,46 +149,51 @@ public class CompositeSet extends CompositeCollection implements Set { * @return true if the object is removed, false otherwise */ public boolean remove(Object obj) { - for (Iterator i = this.getCollections().iterator(); i.hasNext();) { - Set set = (Set) i.next(); + for (Set<? extends E> set : getCollections()) { if (set.contains(obj)) return set.remove(obj); } return false; } - - + /** * @see Set#equals */ + @SuppressWarnings("unchecked") public boolean equals(Object obj) { if (obj instanceof Set) { Set set = (Set) obj; - if (set.containsAll(this) && set.size() == this.size()) { - return true; - } + return set.containsAll(this) && set.size() == this.size(); } return false; } - + /** * @see Set#hashCode */ public int hashCode() { int code = 0; - for (Iterator i = this.iterator(); i.hasNext();) { - Object next = i.next(); - code += (next != null ? next.hashCode() : 0); + for (E e : this) { + code += (e == null ? 0 : e.hashCode()); } return code; } - + + /** + * {@inheritDoc} + */ + @Override + protected SetMutator<E> getMutator() { + return (SetMutator<E>) super.getMutator(); + } + /** * Define callbacks for mutation operations. * <p> * Defining remove() on implementations of SetMutator is pointless * as they are never called by CompositeSet. */ - public static interface SetMutator extends CompositeCollection.CollectionMutator { + public static interface SetMutator<E> extends CompositeCollection.CollectionMutator<E> { + /** * <p> * Called when a Set is added to the CompositeSet and there is a @@ -193,6 +208,6 @@ public class CompositeSet extends CompositeCollection implements Set { * @param added the Set being added to the composite * @param intersects the intersection of th existing and added sets */ - public void resolveCollision(CompositeSet comp, Set existing, Set added, Collection intersects); + public void resolveCollision(CompositeSet<E> comp, Set<E> existing, Set<E> added, Collection<E> intersects); } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/set/ListOrderedSet.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/set/ListOrderedSet.java b/src/java/org/apache/commons/collections/set/ListOrderedSet.java index 855c511..beb3990 100644 --- a/src/java/org/apache/commons/collections/set/ListOrderedSet.java +++ b/src/java/org/apache/commons/collections/set/ListOrderedSet.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -36,7 +36,7 @@ import org.apache.commons.collections.list.UnmodifiableList; * <p> * The ListOrderedSet also has various useful direct methods. These include many * from <code>List</code>, such as <code>get(int)</code>, <code>remove(int)</code> - * and <code>indexOf(int)</code>. An unmodifiable <code>List</code> view of + * and <code>indexOf(int)</code>. An unmodifiable <code>List</code> view of * the set can be obtained via <code>asList()</code>. * <p> * This class cannot implement the <code>List</code> interface directly as @@ -46,30 +46,30 @@ import org.apache.commons.collections.list.UnmodifiableList; * * @since Commons Collections 3.0 * @version $Revision$ $Date$ - * + * * @author Stephen Colebourne * @author Henning P. Schmiedehausen */ -public class ListOrderedSet extends AbstractSerializableSetDecorator implements Set { +public class ListOrderedSet<E> extends AbstractSerializableSetDecorator<E> implements Set<E> { /** Serialization version */ private static final long serialVersionUID = -228664372470420141L; /** Internal list to hold the sequence of objects */ - protected final List setOrder; + protected final List<E> setOrder; /** * Factory method to create an ordered set specifying the list and set to use. * <p> * The list and set must both be empty. - * + * * @param set the set to decorate, must be empty and not null * @param list the list to decorate, must be empty and not null * @throws IllegalArgumentException if set or list is null * @throws IllegalArgumentException if either the set or list is not empty * @since Commons Collections 3.1 */ - public static ListOrderedSet decorate(Set set, List list) { + public static <E> ListOrderedSet<E> decorate(Set<E> set, List<E> list) { if (set == null) { throw new IllegalArgumentException("Set must not be null"); } @@ -79,19 +79,19 @@ public class ListOrderedSet extends AbstractSerializableSetDecorator implements if (set.size() > 0 || list.size() > 0) { throw new IllegalArgumentException("Set and List must be empty"); } - return new ListOrderedSet(set, list); + return new ListOrderedSet<E>(set, list); } /** * Factory method to create an ordered set. * <p> * An <code>ArrayList</code> is used to retain order. - * + * * @param set the set to decorate, must not be null * @throws IllegalArgumentException if set is null */ - public static ListOrderedSet decorate(Set set) { - return new ListOrderedSet(set); + public static <E> ListOrderedSet<E> decorate(Set<E> set) { + return new ListOrderedSet<E>(set); } /** @@ -101,53 +101,53 @@ public class ListOrderedSet extends AbstractSerializableSetDecorator implements * <p> * NOTE: If the list contains duplicates, the duplicates are removed, * altering the specified list. - * + * * @param list the list to decorate, must not be null * @throws IllegalArgumentException if list is null */ - public static ListOrderedSet decorate(List list) { + public static <E> ListOrderedSet<E> decorate(List<E> list) { if (list == null) { throw new IllegalArgumentException("List must not be null"); } - Set set = new HashSet(list); + Set<E> set = new HashSet<E>(list); list.retainAll(set); - - return new ListOrderedSet(set, list); + + return new ListOrderedSet<E>(set, list); } //----------------------------------------------------------------------- /** * Constructs a new empty <code>ListOrderedSet</code> using * a <code>HashSet</code> and an <code>ArrayList</code> internally. - * + * * @since Commons Collections 3.1 */ public ListOrderedSet() { - super(new HashSet()); - setOrder = new ArrayList(); + super(new HashSet<E>()); + setOrder = new ArrayList<E>(); } /** * Constructor that wraps (not copies). - * + * * @param set the set to decorate, must not be null * @throws IllegalArgumentException if set is null */ - protected ListOrderedSet(Set set) { + protected ListOrderedSet(Set<E> set) { super(set); - setOrder = new ArrayList(set); + setOrder = new ArrayList<E>(set); } /** * Constructor that wraps (not copies) the Set and specifies the list to use. * <p> * The set and list must both be correctly initialised to the same elements. - * + * * @param set the set to decorate, must not be null * @param list the list to decorate, must not be null * @throws IllegalArgumentException if set or list is null */ - protected ListOrderedSet(Set set, List list) { + protected ListOrderedSet(Set<E> set, List<E> list) { super(set); if (list == null) { throw new IllegalArgumentException("List must not be null"); @@ -158,10 +158,10 @@ public class ListOrderedSet extends AbstractSerializableSetDecorator implements //----------------------------------------------------------------------- /** * Gets an unmodifiable view of the order of the Set. - * + * * @return an unmodifiable list view */ - public List asList() { + public List<E> asList() { return UnmodifiableList.decorate(setOrder); } @@ -171,27 +171,22 @@ public class ListOrderedSet extends AbstractSerializableSetDecorator implements setOrder.clear(); } - public Iterator iterator() { - return new OrderedSetIterator(setOrder.iterator(), collection); + public Iterator<E> iterator() { + return new OrderedSetIterator<E>(setOrder.iterator(), collection); } - public boolean add(Object object) { - if (collection.contains(object)) { - // re-adding doesn't change order - return collection.add(object); - } else { - // first add, so add to both set and list - boolean result = collection.add(object); + public boolean add(E object) { + if (collection.add(object)) { setOrder.add(object); - return result; + return true; } + return false; } - public boolean addAll(Collection coll) { + public boolean addAll(Collection<? extends E> coll) { boolean result = false; - for (Iterator it = coll.iterator(); it.hasNext();) { - Object object = it.next(); - result = result | add(object); + for (E e : coll) { + result |= add(e); } return result; } @@ -202,25 +197,24 @@ public class ListOrderedSet extends AbstractSerializableSetDecorator implements return result; } - public boolean removeAll(Collection coll) { + public boolean removeAll(Collection<?> coll) { boolean result = false; - for (Iterator it = coll.iterator(); it.hasNext();) { - Object object = it.next(); - result = result | remove(object); + for (Iterator<?> it = coll.iterator(); it.hasNext();) { + result |= remove(it.next()); } return result; } - public boolean retainAll(Collection coll) { + public boolean retainAll(Collection<?> coll) { boolean result = collection.retainAll(coll); if (result == false) { return false; - } else if (collection.size() == 0) { + } + if (collection.size() == 0) { setOrder.clear(); } else { - for (Iterator it = setOrder.iterator(); it.hasNext();) { - Object object = it.next(); - if (collection.contains(object) == false) { + for (Iterator<E> it = setOrder.iterator(); it.hasNext();) { + if (!collection.contains(it.next())) { it.remove(); } } @@ -232,12 +226,12 @@ public class ListOrderedSet extends AbstractSerializableSetDecorator implements return setOrder.toArray(); } - public Object[] toArray(Object a[]) { + public <T> T[] toArray(T a[]) { return setOrder.toArray(a); } //----------------------------------------------------------------------- - public Object get(int index) { + public E get(int index) { return setOrder.get(index); } @@ -245,23 +239,22 @@ public class ListOrderedSet extends AbstractSerializableSetDecorator implements return setOrder.indexOf(object); } - public void add(int index, Object object) { - if (contains(object) == false) { + public void add(int index, E object) { + if (!contains(object)) { collection.add(object); setOrder.add(index, object); } } - public boolean addAll(int index, Collection coll) { + public boolean addAll(int index, Collection<? extends E> coll) { boolean changed = false; - for (Iterator it = coll.iterator(); it.hasNext();) { - Object object = it.next(); - if (contains(object) == false) { - collection.add(object); - setOrder.add(index, object); - index++; - changed = true; + for (E e : coll) { + if (contains(e)) { + continue; } + collection.add(e); + setOrder.add(index++, e); + changed = true; } return changed; } @@ -273,9 +266,9 @@ public class ListOrderedSet extends AbstractSerializableSetDecorator implements } /** - * Uses the underlying List's toString so that order is achieved. - * This means that the decorated Set's toString is not used, so - * any custom toStrings will be ignored. + * Uses the underlying List's toString so that order is achieved. + * This means that the decorated Set's toString is not used, so + * any custom toStrings will be ignored. */ // Fortunately List.toString and Set.toString look the same public String toString() { @@ -286,19 +279,19 @@ public class ListOrderedSet extends AbstractSerializableSetDecorator implements /** * Internal iterator handle remove. */ - static class OrderedSetIterator extends AbstractIteratorDecorator { - + static class OrderedSetIterator<E> extends AbstractIteratorDecorator<E> { + /** Object we iterate on */ - protected final Collection set; + protected final Collection<E> set; /** Last object retrieved */ - protected Object last; + protected E last; - private OrderedSetIterator(Iterator iterator, Collection set) { + private OrderedSetIterator(Iterator<E> iterator, Collection<E> set) { super(iterator); this.set = set; } - public Object next() { + public E next() { last = iterator.next(); return last; } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/set/MapBackedSet.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/set/MapBackedSet.java b/src/java/org/apache/commons/collections/set/MapBackedSet.java index 21863ba..5144de5 100644 --- a/src/java/org/apache/commons/collections/set/MapBackedSet.java +++ b/src/java/org/apache/commons/collections/set/MapBackedSet.java @@ -37,15 +37,16 @@ import java.util.Set; * * @author Stephen Colebourne */ -public final class MapBackedSet implements Set, Serializable { +public final class MapBackedSet<E, V> implements Set<E>, Serializable { /** Serialization version */ private static final long serialVersionUID = 6723912213766056587L; /** The map being used as the backing store */ - protected final Map map; + protected final Map<E, ? super V> map; + /** The dummyValue to use */ - protected final Object dummyValue; + protected final V dummyValue; /** * Factory method to create a set from a map. @@ -53,7 +54,7 @@ public final class MapBackedSet implements Set, Serializable { * @param map the map to decorate, must not be null * @throws IllegalArgumentException if set is null */ - public static Set decorate(Map map) { + public static <E, V> Set<E> decorate(Map<E, ? super V> map) { return decorate(map, null); } @@ -64,11 +65,11 @@ public final class MapBackedSet implements Set, Serializable { * @param dummyValue the dummy value to use * @throws IllegalArgumentException if map is null */ - public static Set decorate(Map map, Object dummyValue) { + public static <E, V> Set<E> decorate(Map<E, ? super V> map, V dummyValue) { if (map == null) { throw new IllegalArgumentException("The map must not be null"); } - return new MapBackedSet(map, dummyValue); + return new MapBackedSet<E, V>(map, dummyValue); } //----------------------------------------------------------------------- @@ -79,7 +80,7 @@ public final class MapBackedSet implements Set, Serializable { * @param dummyValue the dummy value to use * @throws IllegalArgumentException if map is null */ - private MapBackedSet(Map map, Object dummyValue) { + private MapBackedSet(Map<E, ? super V> map, V dummyValue) { super(); this.map = map; this.dummyValue = dummyValue; @@ -94,7 +95,7 @@ public final class MapBackedSet implements Set, Serializable { return map.isEmpty(); } - public Iterator iterator() { + public Iterator<E> iterator() { return map.keySet().iterator(); } @@ -102,21 +103,20 @@ public final class MapBackedSet implements Set, Serializable { return map.containsKey(obj); } - public boolean containsAll(Collection coll) { + public boolean containsAll(Collection<?> coll) { return map.keySet().containsAll(coll); } - public boolean add(Object obj) { + public boolean add(E obj) { int size = map.size(); map.put(obj, dummyValue); return (map.size() != size); } - public boolean addAll(Collection coll) { + public boolean addAll(Collection<? extends E> coll) { int size = map.size(); - for (Iterator it = coll.iterator(); it.hasNext();) { - Object obj = it.next(); - map.put(obj, dummyValue); + for (E e : coll) { + map.put(e, dummyValue); } return (map.size() != size); } @@ -127,11 +127,11 @@ public final class MapBackedSet implements Set, Serializable { return (map.size() != size); } - public boolean removeAll(Collection coll) { + public boolean removeAll(Collection<?> coll) { return map.keySet().removeAll(coll); } - public boolean retainAll(Collection coll) { + public boolean retainAll(Collection<?> coll) { return map.keySet().retainAll(coll); } @@ -143,7 +143,7 @@ public final class MapBackedSet implements Set, Serializable { return map.keySet().toArray(); } - public Object[] toArray(Object[] array) { + public <T> T[] toArray(T[] array) { return map.keySet().toArray(array); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/set/SynchronizedSet.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/set/SynchronizedSet.java b/src/java/org/apache/commons/collections/set/SynchronizedSet.java index 53b31aa..1bc69f1 100644 --- a/src/java/org/apache/commons/collections/set/SynchronizedSet.java +++ b/src/java/org/apache/commons/collections/set/SynchronizedSet.java @@ -75,8 +75,8 @@ public class SynchronizedSet<E> extends SynchronizedCollection<E> implements Set * * @return the decorated set */ - protected Set getSet() { - return (Set) collection; + protected Set<E> getSet() { + return (Set<E>) collection; } }