http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/bidimap/UnmodifiableBidiMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/bidimap/UnmodifiableBidiMap.java b/src/java/org/apache/commons/collections/bidimap/UnmodifiableBidiMap.java index 5d043e6..1684cd5 100644 --- a/src/java/org/apache/commons/collections/bidimap/UnmodifiableBidiMap.java +++ b/src/java/org/apache/commons/collections/bidimap/UnmodifiableBidiMap.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. @@ -33,39 +33,39 @@ import org.apache.commons.collections.set.UnmodifiableSet; * * @since Commons Collections 3.0 * @version $Revision$ $Date$ - * + * * @author Stephen Colebourne */ -public final class UnmodifiableBidiMap - extends AbstractBidiMapDecorator implements Unmodifiable { - +public final class UnmodifiableBidiMap<K, V> + extends AbstractBidiMapDecorator<K, V> implements Unmodifiable { + /** The inverse unmodifiable map */ - private UnmodifiableBidiMap inverse; + private UnmodifiableBidiMap<V, K> inverse; /** * Factory method to create an unmodifiable map. * <p> * If the map passed in is already unmodifiable, it is returned. - * + * * @param map the map to decorate, must not be null * @return an unmodifiable BidiMap * @throws IllegalArgumentException if map is null */ - public static BidiMap decorate(BidiMap map) { + public static <K, V> BidiMap<K, V> decorate(BidiMap<K, V> map) { if (map instanceof Unmodifiable) { return map; } - return new UnmodifiableBidiMap(map); + return new UnmodifiableBidiMap<K, V>(map); } //----------------------------------------------------------------------- /** * Constructor that wraps (not copies). - * + * * @param map the map to decorate, must not be null * @throws IllegalArgumentException if map is null */ - private UnmodifiableBidiMap(BidiMap map) { + private UnmodifiableBidiMap(BidiMap<K, V> map) { super(map); } @@ -74,49 +74,49 @@ public final class UnmodifiableBidiMap 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); } //----------------------------------------------------------------------- - public Object removeValue(Object value) { + public K removeValue(Object value) { throw new UnsupportedOperationException(); } - public MapIterator mapIterator() { - MapIterator it = getBidiMap().mapIterator(); + public MapIterator<K, V> mapIterator() { + MapIterator<K, V> it = decorated().mapIterator(); return UnmodifiableMapIterator.decorate(it); } - public BidiMap inverseBidiMap() { + public synchronized BidiMap<V, K> inverseBidiMap() { if (inverse == null) { - inverse = new UnmodifiableBidiMap(getBidiMap().inverseBidiMap()); + inverse = new UnmodifiableBidiMap<V, K>(decorated().inverseBidiMap()); inverse.inverse = this; } return inverse; } - + }
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/bidimap/UnmodifiableOrderedBidiMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/bidimap/UnmodifiableOrderedBidiMap.java b/src/java/org/apache/commons/collections/bidimap/UnmodifiableOrderedBidiMap.java index b836a31..19c0f64 100644 --- a/src/java/org/apache/commons/collections/bidimap/UnmodifiableOrderedBidiMap.java +++ b/src/java/org/apache/commons/collections/bidimap/UnmodifiableOrderedBidiMap.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. @@ -20,8 +20,6 @@ import java.util.Collection; import java.util.Map; import java.util.Set; -import org.apache.commons.collections.BidiMap; -import org.apache.commons.collections.MapIterator; import org.apache.commons.collections.OrderedBidiMap; import org.apache.commons.collections.OrderedMapIterator; import org.apache.commons.collections.Unmodifiable; @@ -35,39 +33,39 @@ import org.apache.commons.collections.set.UnmodifiableSet; * * @since Commons Collections 3.0 * @version $Revision$ $Date$ - * + * * @author Stephen Colebourne */ -public final class UnmodifiableOrderedBidiMap - extends AbstractOrderedBidiMapDecorator implements Unmodifiable { - +public final class UnmodifiableOrderedBidiMap<K, V> + extends AbstractOrderedBidiMapDecorator<K, V> implements Unmodifiable { + /** The inverse unmodifiable map */ - private UnmodifiableOrderedBidiMap inverse; + private UnmodifiableOrderedBidiMap<V, K> inverse; /** * Factory method to create an unmodifiable map. * <p> * If the map passed in is already unmodifiable, it is returned. - * + * * @param map the map to decorate, must not be null * @return an unmodifiable OrderedBidiMap * @throws IllegalArgumentException if map is null */ - public static OrderedBidiMap decorate(OrderedBidiMap map) { + public static <K, V> OrderedBidiMap<K, V> decorate(OrderedBidiMap<K, V> map) { if (map instanceof Unmodifiable) { return map; } - return new UnmodifiableOrderedBidiMap(map); + return new UnmodifiableOrderedBidiMap<K, V>(map); } //----------------------------------------------------------------------- /** * Constructor that wraps (not copies). - * + * * @param map the map to decorate, must not be null * @throws IllegalArgumentException if map is null */ - private UnmodifiableOrderedBidiMap(OrderedBidiMap map) { + private UnmodifiableOrderedBidiMap(OrderedBidiMap<K, V> map) { super(map); } @@ -76,55 +74,51 @@ public final class UnmodifiableOrderedBidiMap 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); } //----------------------------------------------------------------------- - public Object removeValue(Object value) { + public K removeValue(Object value) { throw new UnsupportedOperationException(); } - public MapIterator mapIterator() { - return orderedMapIterator(); - } - - public BidiMap inverseBidiMap() { + public OrderedBidiMap<V, K> inverseBidiMap() { return inverseOrderedBidiMap(); } - + //----------------------------------------------------------------------- - public OrderedMapIterator orderedMapIterator() { - OrderedMapIterator it = getOrderedBidiMap().orderedMapIterator(); + public OrderedMapIterator<K, V> mapIterator() { + OrderedMapIterator<K, V> it = decorated().mapIterator(); return UnmodifiableOrderedMapIterator.decorate(it); } - public OrderedBidiMap inverseOrderedBidiMap() { + public OrderedBidiMap<V, K> inverseOrderedBidiMap() { if (inverse == null) { - inverse = new UnmodifiableOrderedBidiMap(getOrderedBidiMap().inverseOrderedBidiMap()); + inverse = new UnmodifiableOrderedBidiMap<V, K>(decorated().inverseBidiMap()); inverse.inverse = this; } return inverse; http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/bidimap/UnmodifiableSortedBidiMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/bidimap/UnmodifiableSortedBidiMap.java b/src/java/org/apache/commons/collections/bidimap/UnmodifiableSortedBidiMap.java index 4e47f58..aa73454 100644 --- a/src/java/org/apache/commons/collections/bidimap/UnmodifiableSortedBidiMap.java +++ b/src/java/org/apache/commons/collections/bidimap/UnmodifiableSortedBidiMap.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. @@ -21,9 +21,6 @@ import java.util.Map; import java.util.Set; import java.util.SortedMap; -import org.apache.commons.collections.BidiMap; -import org.apache.commons.collections.MapIterator; -import org.apache.commons.collections.OrderedBidiMap; import org.apache.commons.collections.OrderedMapIterator; import org.apache.commons.collections.SortedBidiMap; import org.apache.commons.collections.Unmodifiable; @@ -38,39 +35,39 @@ import org.apache.commons.collections.set.UnmodifiableSet; * * @since Commons Collections 3.0 * @version $Revision$ $Date$ - * + * * @author Stephen Colebourne */ -public final class UnmodifiableSortedBidiMap - extends AbstractSortedBidiMapDecorator implements Unmodifiable { - +public final class UnmodifiableSortedBidiMap<K, V> + extends AbstractSortedBidiMapDecorator<K, V> implements Unmodifiable { + /** The inverse unmodifiable map */ - private UnmodifiableSortedBidiMap inverse; + private UnmodifiableSortedBidiMap<V, K> inverse; /** * Factory method to create an unmodifiable map. * <p> * If the map passed in is already unmodifiable, it is returned. - * + * * @param map the map to decorate, must not be null * @return an unmodifiable SortedBidiMap * @throws IllegalArgumentException if map is null */ - public static SortedBidiMap decorate(SortedBidiMap map) { + public static <K, V> SortedBidiMap<K, V> decorate(SortedBidiMap<K, V> map) { if (map instanceof Unmodifiable) { return map; } - return new UnmodifiableSortedBidiMap(map); + return new UnmodifiableSortedBidiMap<K, V>(map); } //----------------------------------------------------------------------- /** * Constructor that wraps (not copies). - * + * * @param map the map to decorate, must not be null * @throws IllegalArgumentException if map is null */ - private UnmodifiableSortedBidiMap(SortedBidiMap map) { + private UnmodifiableSortedBidiMap(SortedBidiMap<K, V> map) { super(map); } @@ -79,77 +76,65 @@ public final class UnmodifiableSortedBidiMap 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); } //----------------------------------------------------------------------- - public Object removeValue(Object value) { + public K removeValue(Object value) { throw new UnsupportedOperationException(); } - public MapIterator mapIterator() { - return orderedMapIterator(); - } - - public BidiMap inverseBidiMap() { - return inverseSortedBidiMap(); - } - //----------------------------------------------------------------------- - public OrderedMapIterator orderedMapIterator() { - OrderedMapIterator it = getSortedBidiMap().orderedMapIterator(); + public OrderedMapIterator<K, V> mapIterator() { + OrderedMapIterator<K, V> it = decorated().mapIterator(); return UnmodifiableOrderedMapIterator.decorate(it); } - public OrderedBidiMap inverseOrderedBidiMap() { - return inverseSortedBidiMap(); - } - //----------------------------------------------------------------------- - public SortedBidiMap inverseSortedBidiMap() { + public SortedBidiMap<V, K> inverseBidiMap() { if (inverse == null) { - inverse = new UnmodifiableSortedBidiMap(getSortedBidiMap().inverseSortedBidiMap()); + inverse = new UnmodifiableSortedBidiMap<V, K>(decorated().inverseBidiMap()); inverse.inverse = this; } return inverse; } - public SortedMap subMap(Object fromKey, Object toKey) { - SortedMap sm = getSortedBidiMap().subMap(fromKey, toKey); + public SortedMap<K, V> subMap(K fromKey, K toKey) { + SortedMap<K, V> sm = decorated().subMap(fromKey, toKey); return UnmodifiableSortedMap.decorate(sm); } - public SortedMap headMap(Object toKey) { - SortedMap sm = getSortedBidiMap().headMap(toKey); + public SortedMap<K, V> headMap(K toKey) { + SortedMap<K, V> sm = decorated().headMap(toKey); return UnmodifiableSortedMap.decorate(sm); } - public SortedMap tailMap(Object fromKey) { - SortedMap sm = getSortedBidiMap().tailMap(fromKey); + public SortedMap<K, V> tailMap(K fromKey) { + SortedMap<K, V> sm = decorated().tailMap(fromKey); return UnmodifiableSortedMap.decorate(sm); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/buffer/AbstractBufferDecorator.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/buffer/AbstractBufferDecorator.java b/src/java/org/apache/commons/collections/buffer/AbstractBufferDecorator.java index a049ef4..90b4ad2 100644 --- a/src/java/org/apache/commons/collections/buffer/AbstractBufferDecorator.java +++ b/src/java/org/apache/commons/collections/buffer/AbstractBufferDecorator.java @@ -30,9 +30,11 @@ import org.apache.commons.collections.collection.AbstractCollectionDecorator; * * @author Stephen Colebourne */ -public abstract class AbstractBufferDecorator<E> - extends AbstractCollectionDecorator<E> - implements Buffer<E> { +public abstract class AbstractBufferDecorator<E> extends AbstractCollectionDecorator<E> implements + Buffer<E> { + + /** Serialization version */ + private static final long serialVersionUID = -2629815475789577029L; /** * 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/buffer/BoundedBuffer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/buffer/BoundedBuffer.java b/src/java/org/apache/commons/collections/buffer/BoundedBuffer.java index 06d6789..fc92c3e 100644 --- a/src/java/org/apache/commons/collections/buffer/BoundedBuffer.java +++ b/src/java/org/apache/commons/collections/buffer/BoundedBuffer.java @@ -45,7 +45,7 @@ import org.apache.commons.collections.iterators.AbstractIteratorDecorator; * @version $Revision$ $Date$ * @since Commons Collections 3.2 */ -public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollection { +public class BoundedBuffer<E> extends SynchronizedBuffer<E> implements BoundedCollection<E> { /** The serialization version. */ private static final long serialVersionUID = 1536432911093974264L; @@ -67,8 +67,8 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti * @throws IllegalArgumentException if the buffer is null * @throws IllegalArgumentException if the maximum size is zero or less */ - public static BoundedBuffer decorate(Buffer buffer, int maximumSize) { - return new BoundedBuffer(buffer, maximumSize, 0L); + public static <E> BoundedBuffer<E> decorate(Buffer<E> buffer, int maximumSize) { + return new BoundedBuffer<E>(buffer, maximumSize, 0L); } /** @@ -82,8 +82,8 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti * @throws IllegalArgumentException if the buffer is null * @throws IllegalArgumentException if the maximum size is zero or less */ - public static BoundedBuffer decorate(Buffer buffer, int maximumSize, long timeout) { - return new BoundedBuffer(buffer, maximumSize, timeout); + public static <E> BoundedBuffer<E> decorate(Buffer<E> buffer, int maximumSize, long timeout) { + return new BoundedBuffer<E>(buffer, maximumSize, timeout); } //----------------------------------------------------------------------- @@ -97,7 +97,7 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti * @throws IllegalArgumentException if the buffer is null * @throws IllegalArgumentException if the maximum size is zero or less */ - protected BoundedBuffer(Buffer buffer, int maximumSize, long timeout) { + protected BoundedBuffer(Buffer<E> buffer, int maximumSize, long timeout) { super(buffer); if (maximumSize < 1) { throw new IllegalArgumentException(); @@ -107,29 +107,29 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti } //----------------------------------------------------------------------- - public Object remove() { + public E remove() { synchronized (lock) { - Object returnValue = decorated().remove(); + E returnValue = decorated().remove(); lock.notifyAll(); return returnValue; } } - public boolean add(Object o) { + public boolean add(E o) { synchronized (lock) { timeoutWait(1); return decorated().add(o); } } - public boolean addAll(final Collection c) { + public boolean addAll(final Collection<? extends E> c) { synchronized (lock) { timeoutWait(c.size()); return decorated().addAll(c); } } - public Iterator iterator() { + public Iterator<E> iterator() { return new NotifyingIterator(collection.iterator()); } @@ -178,9 +178,9 @@ public class BoundedBuffer extends SynchronizedBuffer implements BoundedCollecti /** * BoundedBuffer iterator. */ - private class NotifyingIterator extends AbstractIteratorDecorator { + private class NotifyingIterator extends AbstractIteratorDecorator<E> { - public NotifyingIterator(Iterator it) { + public NotifyingIterator(Iterator<E> it) { super(it); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/buffer/BoundedFifoBuffer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/buffer/BoundedFifoBuffer.java b/src/java/org/apache/commons/collections/buffer/BoundedFifoBuffer.java index 3b5f0a3..1fdab68 100644 --- a/src/java/org/apache/commons/collections/buffer/BoundedFifoBuffer.java +++ b/src/java/org/apache/commons/collections/buffer/BoundedFifoBuffer.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. @@ -35,7 +35,7 @@ import org.apache.commons.collections.BufferUnderflowException; * The BoundedFifoBuffer is a very efficient implementation of * <code>Buffer</code> that is of a fixed size. * <p> - * The removal order of a <code>BoundedFifoBuffer</code> is based on the + * The removal order of a <code>BoundedFifoBuffer</code> is based on the * insertion order; elements are removed in the same order in which they * were added. The iteration order is the same as the removal order. * <p> @@ -55,37 +55,37 @@ import org.apache.commons.collections.BufferUnderflowException; * * @since Commons Collections 3.0 (previously in main package v2.1) * @version $Revision$ $Date$ - * + * * @author Avalon * @author Berin Loritsch * @author Paul Jack * @author Stephen Colebourne * @author Herve Quiroz */ -public class BoundedFifoBuffer extends AbstractCollection - implements Buffer, BoundedCollection, Serializable { +public class BoundedFifoBuffer<E> extends AbstractCollection<E> + implements Buffer<E>, BoundedCollection<E>, Serializable { /** Serialization version */ private static final long serialVersionUID = 5603722811189451017L; /** Underlying storage array */ - private transient Object[] elements; - + private transient E[] elements; + /** Array index of first (oldest) buffer element */ private transient int start = 0; - - /** + + /** * Index mod maxElements of the array position following the last buffer * element. Buffer elements start at elements[start] and "wrap around" - * elements[maxElements-1], ending at elements[decrement(end)]. - * For example, elements = {c,a,b}, start=1, end=1 corresponds to + * elements[maxElements-1], ending at elements[decrement(end)]. + * For example, elements = {c,a,b}, start=1, end=1 corresponds to * the buffer [a,b,c]. */ private transient int end = 0; - + /** Flag to indicate if the buffer is currently full. */ private transient boolean full = false; - + /** Capacity of the buffer */ private final int maxElements; @@ -104,11 +104,12 @@ public class BoundedFifoBuffer extends AbstractCollection * @param size the maximum number of elements for this fifo * @throws IllegalArgumentException if the size is less than 1 */ + @SuppressWarnings("unchecked") public BoundedFifoBuffer(int size) { if (size <= 0) { throw new IllegalArgumentException("The size must be greater than 0"); } - elements = new Object[size]; + elements = (E[]) new Object[size]; maxElements = elements.length; } @@ -120,7 +121,7 @@ public class BoundedFifoBuffer extends AbstractCollection * @param coll the collection whose elements to add, may not be null * @throws NullPointerException if the collection is null */ - public BoundedFifoBuffer(Collection coll) { + public BoundedFifoBuffer(Collection<? extends E> coll) { this(coll.size()); addAll(coll); } @@ -128,31 +129,32 @@ public class BoundedFifoBuffer extends AbstractCollection //----------------------------------------------------------------------- /** * Write the buffer out using a custom routine. - * + * * @param out the output stream * @throws IOException */ private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(size()); - for (Iterator it = iterator(); it.hasNext();) { + for (Iterator<E> it = iterator(); it.hasNext();) { out.writeObject(it.next()); } } /** * Read the buffer in using a custom routine. - * + * * @param in the input stream * @throws IOException * @throws ClassNotFoundException */ + @SuppressWarnings("unchecked") private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); - elements = new Object[maxElements]; + elements = (E[]) new Object[maxElements]; int size = in.readInt(); for (int i = 0; i < size; i++) { - elements[i] = in.readObject(); + elements[i] = (E) in.readObject(); } start = 0; full = (size == maxElements); @@ -200,7 +202,7 @@ public class BoundedFifoBuffer extends AbstractCollection public boolean isFull() { return size() == maxElements; } - + /** * Gets the maximum size of the collection (the bound). * @@ -209,7 +211,7 @@ public class BoundedFifoBuffer extends AbstractCollection public int maxSize() { return maxElements; } - + /** * Clears this buffer. */ @@ -228,7 +230,7 @@ public class BoundedFifoBuffer extends AbstractCollection * @throws NullPointerException if the given element is null * @throws BufferOverflowException if this buffer is full */ - public boolean add(Object element) { + public boolean add(E element) { if (null == element) { throw new NullPointerException("Attempted to add null object to buffer"); } @@ -256,11 +258,10 @@ public class BoundedFifoBuffer extends AbstractCollection * @return the least recently inserted element * @throws BufferUnderflowException if the buffer is empty */ - public Object get() { + public E get() { if (isEmpty()) { throw new BufferUnderflowException("The buffer is already empty"); } - return elements[start]; } @@ -270,12 +271,12 @@ public class BoundedFifoBuffer extends AbstractCollection * @return the least recently inserted element * @throws BufferUnderflowException if the buffer is empty */ - public Object remove() { + public E remove() { if (isEmpty()) { throw new BufferUnderflowException("The buffer is already empty"); } - Object element = elements[start]; + E element = elements[start]; if (null != element) { elements[start++] = null; @@ -283,21 +284,19 @@ public class BoundedFifoBuffer extends AbstractCollection if (start >= maxElements) { start = 0; } - full = false; } - return element; } /** * Increments the internal index. - * + * * @param index the index to increment * @return the updated index */ private int increment(int index) { - index++; + index++; if (index >= maxElements) { index = 0; } @@ -306,7 +305,7 @@ public class BoundedFifoBuffer extends AbstractCollection /** * Decrements the internal index. - * + * * @param index the index to decrement * @return the updated index */ @@ -323,8 +322,8 @@ public class BoundedFifoBuffer extends AbstractCollection * * @return an iterator over this buffer's elements */ - public Iterator iterator() { - return new Iterator() { + public Iterator<E> iterator() { + return new Iterator<E>() { private int index = start; private int lastReturnedIndex = -1; @@ -332,10 +331,9 @@ public class BoundedFifoBuffer extends AbstractCollection public boolean hasNext() { return isFirst || (index != end); - } - public Object next() { + public E next() { if (!hasNext()) { throw new NoSuchElementException(); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/buffer/CircularFifoBuffer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/buffer/CircularFifoBuffer.java b/src/java/org/apache/commons/collections/buffer/CircularFifoBuffer.java index bd5dea8..1f354fd 100644 --- a/src/java/org/apache/commons/collections/buffer/CircularFifoBuffer.java +++ b/src/java/org/apache/commons/collections/buffer/CircularFifoBuffer.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. @@ -18,11 +18,11 @@ package org.apache.commons.collections.buffer; import java.util.Collection; -/** +/** * CircularFifoBuffer is a first in first out buffer with a fixed size that * replaces its oldest element if full. * <p> - * The removal order of a <code>CircularFifoBuffer</code> is based on the + * The removal order of a <code>CircularFifoBuffer</code> is based on the * insertion order; elements are removed in the same order in which they * were added. The iteration order is the same as the removal order. * <p> @@ -39,14 +39,14 @@ import java.util.Collection; * This buffer prevents null objects from being added. * <p> * This class is Serializable from Commons Collections 3.1. - * + * * @since Commons Collections 3.0 * @version $Revision$ $Date$ - * + * * @author Stefano Fornari * @author Stephen Colebourne */ -public class CircularFifoBuffer extends BoundedFifoBuffer { +public class CircularFifoBuffer<E> extends BoundedFifoBuffer<E> { /** Serialization version */ private static final long serialVersionUID = -8423413834657610406L; @@ -60,7 +60,7 @@ public class CircularFifoBuffer extends BoundedFifoBuffer { /** * Constructor that creates a buffer with the specified size. - * + * * @param size the size of the buffer (cannot be changed) * @throws IllegalArgumentException if the size is less than 1 */ @@ -71,11 +71,11 @@ public class CircularFifoBuffer extends BoundedFifoBuffer { /** * Constructor that creates a buffer from the specified collection. * The collection size also sets the buffer size - * + * * @param coll the collection to copy into the buffer, may not be null * @throws NullPointerException if the collection is null */ - public CircularFifoBuffer(Collection coll) { + public CircularFifoBuffer(Collection<E> coll) { super(coll); } @@ -86,11 +86,11 @@ public class CircularFifoBuffer extends BoundedFifoBuffer { * @param element the element to add * @return true, always */ - public boolean add(Object element) { + public boolean add(E element) { if (isFull()) { remove(); } return super.add(element); } - + } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/buffer/PriorityBuffer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/buffer/PriorityBuffer.java b/src/java/org/apache/commons/collections/buffer/PriorityBuffer.java index b7db824..f6b6e90 100644 --- a/src/java/org/apache/commons/collections/buffer/PriorityBuffer.java +++ b/src/java/org/apache/commons/collections/buffer/PriorityBuffer.java @@ -24,6 +24,7 @@ import java.util.NoSuchElementException; import org.apache.commons.collections.Buffer; import org.apache.commons.collections.BufferUnderflowException; +import org.apache.commons.collections.comparators.ComparableComparator; /** * Binary heap implementation of <code>Buffer</code> that provides for @@ -62,8 +63,7 @@ import org.apache.commons.collections.BufferUnderflowException; * @author Stephen Colebourne * @author Steve Phelps */ -public class PriorityBuffer extends AbstractCollection - implements Buffer, Serializable { +public class PriorityBuffer<E> extends AbstractCollection<E> implements Buffer<E>, Serializable { /** Serialization lock. */ private static final long serialVersionUID = 6891186490470027896L; @@ -76,21 +76,24 @@ public class PriorityBuffer extends AbstractCollection /** * The elements in this buffer. */ - protected Object[] elements; + protected E[] elements; + /** * The number of elements currently in this buffer. */ protected int size; + /** * If true, the first element as determined by the sort order will * be returned. If false, the last element as determined by the * sort order will be returned. */ protected boolean ascendingOrder; + /** * The comparator used to order the elements */ - protected Comparator comparator; + protected Comparator<? super E> comparator; //----------------------------------------------------------------------- /** @@ -108,7 +111,7 @@ public class PriorityBuffer extends AbstractCollection * @param comparator the comparator used to order the elements, * null means use natural order */ - public PriorityBuffer(Comparator comparator) { + public PriorityBuffer(Comparator<? super E> comparator) { this(DEFAULT_CAPACITY, true, comparator); } @@ -131,7 +134,7 @@ public class PriorityBuffer extends AbstractCollection * @param comparator the comparator used to order the elements, * null means use natural order */ - public PriorityBuffer(boolean ascendingOrder, Comparator comparator) { + public PriorityBuffer(boolean ascendingOrder, Comparator<? super E> comparator) { this(DEFAULT_CAPACITY, ascendingOrder, comparator); } @@ -155,7 +158,7 @@ public class PriorityBuffer extends AbstractCollection * null means use natural order * @throws IllegalArgumentException if <code>capacity</code> is <= <code>0</code> */ - public PriorityBuffer(int capacity, Comparator comparator) { + public PriorityBuffer(int capacity, Comparator<? super E> comparator) { this(capacity, true, comparator); } @@ -183,7 +186,8 @@ public class PriorityBuffer extends AbstractCollection * null means use natural order * @throws IllegalArgumentException if <code>capacity</code> is <code><= 0</code> */ - public PriorityBuffer(int capacity, boolean ascendingOrder, Comparator comparator) { + @SuppressWarnings("unchecked") + public PriorityBuffer(int capacity, boolean ascendingOrder, Comparator<? super E> comparator) { super(); if (capacity <= 0) { throw new IllegalArgumentException("invalid capacity"); @@ -191,8 +195,8 @@ public class PriorityBuffer extends AbstractCollection this.ascendingOrder = ascendingOrder; //+1 as 0 is noop - this.elements = new Object[capacity + 1]; - this.comparator = comparator; + this.elements = (E[]) new Object[capacity + 1]; + this.comparator = (Comparator<? super E>) (comparator == null ? ComparableComparator.INSTANCE : comparator); } //----------------------------------------------------------------------- @@ -210,7 +214,7 @@ public class PriorityBuffer extends AbstractCollection * * @return the comparator in use, null is natural order */ - public Comparator comparator() { + public Comparator<? super E> comparator() { return comparator; } @@ -227,8 +231,9 @@ public class PriorityBuffer extends AbstractCollection /** * Clears all elements from the buffer. */ + @SuppressWarnings("unchecked") public void clear() { - elements = new Object[elements.length]; // for gc + elements = (E[]) new Object[elements.length]; // for gc size = 0; } @@ -240,11 +245,11 @@ public class PriorityBuffer extends AbstractCollection * @param element the element to be added * @return true always */ - public boolean add(Object element) { + public boolean add(E element) { if (isAtCapacity()) { grow(); } - // percolate element to it's place in tree + // percolate element to its place in tree if (ascendingOrder) { percolateUpMinHeap(element); } else { @@ -259,12 +264,11 @@ public class PriorityBuffer extends AbstractCollection * @return the next element * @throws BufferUnderflowException if the buffer is empty */ - public Object get() { + public E get() { if (isEmpty()) { throw new BufferUnderflowException(); - } else { - return elements[1]; } + return elements[1]; } /** @@ -273,8 +277,8 @@ public class PriorityBuffer extends AbstractCollection * @return the next element * @throws BufferUnderflowException if the buffer is empty */ - public Object remove() { - final Object result = get(); + public E remove() { + final E result = get(); elements[1] = elements[size--]; // set the unused element to 'null' so that the garbage collector @@ -313,7 +317,7 @@ public class PriorityBuffer extends AbstractCollection * @param index the index for the element */ protected void percolateDownMinHeap(final int index) { - final Object element = elements[index]; + final E element = elements[index]; int hole = index; while ((hole * 2) <= size) { @@ -345,7 +349,7 @@ public class PriorityBuffer extends AbstractCollection * @param index the index of the element */ protected void percolateDownMaxHeap(final int index) { - final Object element = elements[index]; + final E element = elements[index]; int hole = index; while ((hole * 2) <= size) { @@ -378,7 +382,7 @@ public class PriorityBuffer extends AbstractCollection */ protected void percolateUpMinHeap(final int index) { int hole = index; - Object element = elements[hole]; + E element = elements[hole]; while (hole > 1 && compare(element, elements[hole / 2]) < 0) { // save element that is being pushed down // as the element "bubble" is percolated up @@ -396,7 +400,7 @@ public class PriorityBuffer extends AbstractCollection * * @param element the element */ - protected void percolateUpMinHeap(final Object element) { + protected void percolateUpMinHeap(final E element) { elements[++size] = element; percolateUpMinHeap(size); } @@ -410,7 +414,7 @@ public class PriorityBuffer extends AbstractCollection */ protected void percolateUpMaxHeap(final int index) { int hole = index; - Object element = elements[hole]; + E element = elements[hole]; while (hole > 1 && compare(element, elements[hole / 2]) > 0) { // save element that is being pushed down @@ -430,7 +434,7 @@ public class PriorityBuffer extends AbstractCollection * * @param element the element */ - protected void percolateUpMaxHeap(final Object element) { + protected void percolateUpMaxHeap(final E element) { elements[++size] = element; percolateUpMaxHeap(size); } @@ -443,19 +447,16 @@ public class PriorityBuffer extends AbstractCollection * @param b the second object * @return -ve if a less than b, 0 if they are equal, +ve if a greater than b */ - protected int compare(Object a, Object b) { - if (comparator != null) { - return comparator.compare(a, b); - } else { - return ((Comparable) a).compareTo(b); - } + protected int compare(E a, E b) { + return comparator.compare(a, b); } /** * Increases the size of the heap to support additional elements */ + @SuppressWarnings("unchecked") protected void grow() { - final Object[] array = new Object[elements.length * 2]; + final E[] array = (E[]) new Object[elements.length * 2]; System.arraycopy(elements, 0, array, 0, elements.length); elements = array; } @@ -466,8 +467,8 @@ public class PriorityBuffer extends AbstractCollection * * @return an iterator over this heap's elements */ - public Iterator iterator() { - return new Iterator() { + public Iterator<E> iterator() { + return new Iterator<E>() { private int index = 1; private int lastReturnedIndex = -1; @@ -476,7 +477,7 @@ public class PriorityBuffer extends AbstractCollection return index <= size; } - public Object next() { + public E next() { if (!hasNext()) { throw new NoSuchElementException(); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/buffer/TransformedBuffer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/buffer/TransformedBuffer.java b/src/java/org/apache/commons/collections/buffer/TransformedBuffer.java index 3ccb0d8..c8ba28e 100644 --- a/src/java/org/apache/commons/collections/buffer/TransformedBuffer.java +++ b/src/java/org/apache/commons/collections/buffer/TransformedBuffer.java @@ -35,7 +35,7 @@ import org.apache.commons.collections.collection.TransformedCollection; * * @author Stephen Colebourne */ -public class TransformedBuffer extends TransformedCollection implements Buffer { +public class TransformedBuffer<E> extends TransformedCollection<E> implements Buffer<E> { /** Serialization version */ private static final long serialVersionUID = -7901091318986132033L; @@ -51,8 +51,8 @@ public class TransformedBuffer extends TransformedCollection implements Buffer { * @return a new transformed Buffer * @throws IllegalArgumentException if buffer or transformer is null */ - public static Buffer decorate(Buffer buffer, Transformer transformer) { - return new TransformedBuffer(buffer, transformer); + public static <E> Buffer<E> decorate(Buffer<E> buffer, Transformer<? super E, ? extends E> transformer) { + return new TransformedBuffer<E>(buffer, transformer); } //----------------------------------------------------------------------- @@ -66,7 +66,7 @@ public class TransformedBuffer extends TransformedCollection implements Buffer { * @param transformer the transformer to use for conversion, must not be null * @throws IllegalArgumentException if buffer or transformer is null */ - protected TransformedBuffer(Buffer buffer, Transformer transformer) { + protected TransformedBuffer(Buffer<E> buffer, Transformer<? super E, ? extends E> transformer) { super(buffer, transformer); } @@ -75,16 +75,16 @@ public class TransformedBuffer extends TransformedCollection implements Buffer { * * @return the decorated buffer */ - protected Buffer getBuffer() { - return (Buffer) collection; + protected Buffer<E> getBuffer() { + return (Buffer<E>) collection; } //----------------------------------------------------------------------- - public Object get() { + public E get() { return getBuffer().get(); } - public Object remove() { + public E remove() { return getBuffer().remove(); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/buffer/UnboundedFifoBuffer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/buffer/UnboundedFifoBuffer.java b/src/java/org/apache/commons/collections/buffer/UnboundedFifoBuffer.java index 668b4c8..921ccfa 100644 --- a/src/java/org/apache/commons/collections/buffer/UnboundedFifoBuffer.java +++ b/src/java/org/apache/commons/collections/buffer/UnboundedFifoBuffer.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. @@ -50,7 +50,7 @@ import org.apache.commons.collections.BufferUnderflowException; * This buffer prevents null objects from being added. * <p> * This class is Serializable from Commons Collections 3.1. - * + * * @since Commons Collections 3.0 (previously in main package v2.1) * @version $Revision$ $Date$ * @@ -63,7 +63,7 @@ import org.apache.commons.collections.BufferUnderflowException; * @author Thomas Knych * @author Jordan Krey */ -public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, Serializable { +public class UnboundedFifoBuffer<E> extends AbstractCollection<E> implements Buffer<E>, Serializable { // invariant: buffer.length > size() // ie.buffer always has at least one empty entry @@ -71,9 +71,11 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, S private static final long serialVersionUID = -3482960336579541419L; /** The array of objects in the buffer. */ - protected transient Object[] buffer; + protected transient E[] buffer; + /** The current head index. */ protected transient int head; + /** The current tail index. */ protected transient int tail; @@ -92,15 +94,16 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, S /** * Constructs an UnboundedFifoBuffer with the specified number of elements. * The integer must be a positive integer. - * + * * @param initialSize the initial size of the buffer * @throws IllegalArgumentException if the size is less than 1 */ + @SuppressWarnings("unchecked") public UnboundedFifoBuffer(int initialSize) { if (initialSize <= 0) { throw new IllegalArgumentException("The size must be greater than 0"); } - buffer = new Object[initialSize + 1]; + buffer = (E[]) new Object[initialSize + 1]; head = 0; tail = 0; } @@ -108,31 +111,32 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, S //----------------------------------------------------------------------- /** * Write the buffer out using a custom routine. - * + * * @param out the output stream * @throws IOException */ private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(size()); - for (Iterator it = iterator(); it.hasNext();) { + for (Iterator<E> it = iterator(); it.hasNext();) { out.writeObject(it.next()); } } /** * Read the buffer in using a custom routine. - * + * * @param in the input stream * @throws IOException * @throws ClassNotFoundException */ + @SuppressWarnings("unchecked") private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); int size = in.readInt(); - buffer = new Object[size + 1]; + buffer = (E[]) new Object[size + 1]; for (int i = 0; i < size; i++) { - buffer[i] = in.readObject(); + buffer[i] = (E) in.readObject(); } head = 0; tail = size; @@ -172,14 +176,15 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, S * @return true, always * @throws NullPointerException if the given element is null */ - public boolean add(final Object obj) { + @SuppressWarnings("unchecked") + public boolean add(final E obj) { if (obj == null) { throw new NullPointerException("Attempted to add null object to buffer"); } if (size() + 1 >= buffer.length) { // copy contents to a new buffer array - Object[] tmp = new Object[((buffer.length - 1) * 2) + 1]; + E[] tmp = (E[]) new Object[((buffer.length - 1) * 2) + 1]; int j = 0; // move head to element zero in the new array for (int i = head; i != tail;) { @@ -205,7 +210,7 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, S * @return the next object in the buffer * @throws BufferUnderflowException if this buffer is empty */ - public Object get() { + public E get() { if (isEmpty()) { throw new BufferUnderflowException("The buffer is already empty"); } @@ -219,12 +224,12 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, S * @return the removed object * @throws BufferUnderflowException if this buffer is empty */ - public Object remove() { + public E remove() { if (isEmpty()) { throw new BufferUnderflowException("The buffer is already empty"); } - Object element = buffer[head]; + E element = buffer[head]; if (element != null) { buffer[head] = null; head = increment(head); @@ -234,7 +239,7 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, S /** * Increments the internal index. - * + * * @param index the index to increment * @return the updated index */ @@ -248,7 +253,7 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, S /** * Decrements the internal index. - * + * * @param index the index to decrement * @return the updated index */ @@ -265,8 +270,8 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, S * * @return an iterator over this buffer's elements */ - public Iterator iterator() { - return new Iterator() { + public Iterator<E> iterator() { + return new Iterator<E>() { private int index = head; private int lastReturnedIndex = -1; @@ -276,7 +281,7 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer, S } - public Object next() { + public E next() { if (!hasNext()) { throw new NoSuchElementException(); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/buffer/UnmodifiableBuffer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/buffer/UnmodifiableBuffer.java b/src/java/org/apache/commons/collections/buffer/UnmodifiableBuffer.java index ccd7c5f..a4799ef 100644 --- a/src/java/org/apache/commons/collections/buffer/UnmodifiableBuffer.java +++ b/src/java/org/apache/commons/collections/buffer/UnmodifiableBuffer.java @@ -37,8 +37,8 @@ import org.apache.commons.collections.iterators.UnmodifiableIterator; * * @author Stephen Colebourne */ -public final class UnmodifiableBuffer - extends AbstractBufferDecorator +public final class UnmodifiableBuffer<E> + extends AbstractBufferDecorator<E> implements Unmodifiable, Serializable { /** Serialization version */ @@ -53,11 +53,11 @@ public final class UnmodifiableBuffer * @return an unmodifiable Buffer * @throws IllegalArgumentException if buffer is null */ - public static Buffer decorate(Buffer buffer) { + public static <E> Buffer<E> decorate(Buffer<E> buffer) { if (buffer instanceof Unmodifiable) { return buffer; } - return new UnmodifiableBuffer(buffer); + return new UnmodifiableBuffer<E>(buffer); } //----------------------------------------------------------------------- @@ -67,7 +67,7 @@ public final class UnmodifiableBuffer * @param buffer the buffer to decorate, must not be null * @throws IllegalArgumentException if buffer is null */ - private UnmodifiableBuffer(Buffer buffer) { + private UnmodifiableBuffer(Buffer<E> buffer) { super(buffer); } @@ -90,13 +90,14 @@ public final class UnmodifiableBuffer * @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(); } //----------------------------------------------------------------------- - public Iterator iterator() { + public Iterator<E> iterator() { return UnmodifiableIterator.decorate(decorated().iterator()); } @@ -104,7 +105,7 @@ public final class UnmodifiableBuffer throw new UnsupportedOperationException(); } - public boolean addAll(Collection coll) { + public boolean addAll(Collection<? extends E> coll) { throw new UnsupportedOperationException(); } @@ -116,16 +117,16 @@ public final class UnmodifiableBuffer 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 Object remove() { + public E remove() { throw new UnsupportedOperationException(); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/collection/AbstractUntypedCollectionDecorator.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/collection/AbstractUntypedCollectionDecorator.java b/src/java/org/apache/commons/collections/collection/AbstractUntypedCollectionDecorator.java new file mode 100644 index 0000000..c67394c --- /dev/null +++ b/src/java/org/apache/commons/collections/collection/AbstractUntypedCollectionDecorator.java @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.collections.collection; + +import java.io.Serializable; +import java.util.Collection; + +/** + * Decorates another <code>Collection</code> to provide additional behaviour + * without guaranteeing that the provided <code>Collection</code> type is the + * same as that of the decorated <code>Collection</code>. + * <p> + * Each untyped method call made on this <code>Collection</code> is forwarded to the + * decorated <code>Collection</code>. This class is used as a framework on which + * to build to extensions such as synchronized and unmodifiable behaviour. The + * main advantage of decoration is that one decorator can wrap any + * implementation of <code>Collection</code>, whereas sub-classing requires a + * new class to be written for each implementation. + * <p> + * This implementation does not perform any special processing with + * {@link #iterator()}. Instead it simply returns the value from the wrapped + * collection. This may be undesirable, for example if you are trying to write + * an unmodifiable implementation it might provide a loophole. + * + * @param <D> the type of the elements in the decorated collection + * @param <E> the element type of the Collection implementation + * @since Commons Collections 5 + * @version $Revision$ $Date$ + * + * @author Stephen Colebourne + * @author Paul Jack + * @author Matt Benson + */ +public abstract class AbstractUntypedCollectionDecorator<E, D> implements Collection<E>, Serializable { + + /** Serialization version */ + private static final long serialVersionUID = -8016691444524268856L; + + /** The collection being decorated */ + protected Collection<D> collection; + + /** + * Create a new AbstractUntypedCollectionDecorator. + */ + public AbstractUntypedCollectionDecorator() { + super(); + } + + /** + * Gets the collection being decorated. All access to the decorated + * collection goes via this method. + * + * @return the decorated collection + */ + protected Collection<D> decorated() { + return collection; + } + + public void clear() { + decorated().clear(); + } + + public boolean contains(Object object) { + return decorated().contains(object); + } + + public boolean isEmpty() { + return decorated().isEmpty(); + } + + public boolean remove(Object object) { + return decorated().remove(object); + } + + public int size() { + return decorated().size(); + } + + public Object[] toArray() { + return decorated().toArray(); + } + + public <T> T[] toArray(T[] object) { + return decorated().toArray(object); + } + + public boolean containsAll(Collection<?> coll) { + return decorated().containsAll(coll); + } + + public boolean removeAll(Collection<?> coll) { + return decorated().removeAll(coll); + } + + public boolean retainAll(Collection<?> coll) { + return decorated().retainAll(coll); + } + + public boolean equals(Object object) { + return object == this || decorated().equals(object); + } + + public int hashCode() { + return decorated().hashCode(); + } + + public String toString() { + return decorated().toString(); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/collection/CompositeCollection.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/collection/CompositeCollection.java b/src/java/org/apache/commons/collections/collection/CompositeCollection.java index 9e838fb..54b747b 100644 --- a/src/java/org/apache/commons/collections/collection/CompositeCollection.java +++ b/src/java/org/apache/commons/collections/collection/CompositeCollection.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. @@ -31,7 +31,7 @@ import org.apache.commons.collections.list.UnmodifiableList; * Decorates a collection of other collections to provide a single unified view. * <p> * Changes made to this collection will actually be made on the decorated collection. - * Add and remove operations require the use of a pluggable strategy. If no + * Add and remove operations require the use of a pluggable strategy. If no * strategy is provided then add and remove are unsupported. * * @param <E> the type of the elements in the collection @@ -46,6 +46,7 @@ public class CompositeCollection<E> implements Collection<E> { /** CollectionMutator to handle changes to the collection */ protected CollectionMutator<E> mutator; + /** Collections in the composite */ protected List<Collection<E>> all = new ArrayList<Collection<E>>(); @@ -79,7 +80,7 @@ public class CompositeCollection<E> implements Collection<E> { /** * Create a Composite Collection with an array of collections. - * + * * @param compositeCollections the collections to composite */ public CompositeCollection(Collection<E>[] compositeCollections) { @@ -89,7 +90,7 @@ public class CompositeCollection<E> implements Collection<E> { // /** // * Create a Composite Collection extracting the collections from an iterable. -// * +// * // * @param compositeCollections the collections to composite // */ // public CompositeCollection(Iterable<Collection<E>> compositeCollections) { @@ -121,7 +122,7 @@ public class CompositeCollection<E> implements Collection<E> { * @return true if all of the contained collections are empty */ public boolean isEmpty() { - for (Collection<E> item : all) { + for (Collection<? extends E> item : all) { if (item.isEmpty() == false) { return false; } @@ -138,7 +139,7 @@ public class CompositeCollection<E> implements Collection<E> { * @return true if obj is contained in any of the contained collections */ public boolean contains(Object obj) { - for (Collection<E> item : all) { + for (Collection<? extends E> item : all) { if (item.contains(obj)) { return true; } @@ -158,10 +159,10 @@ public class CompositeCollection<E> implements Collection<E> { */ public Iterator<E> iterator() { if (all.isEmpty()) { - return EmptyIterator.INSTANCE; + return EmptyIterator.<E>getInstance(); } - IteratorChain chain = new IteratorChain(); - for (Collection<E> item : all) { + IteratorChain<E> chain = new IteratorChain<E>(); + for (Collection<? extends E> item : all) { chain.addIterator(item.iterator()); } return chain; @@ -197,11 +198,11 @@ public class CompositeCollection<E> implements Collection<E> { } else { result = (Object[]) Array.newInstance(array.getClass().getComponentType(), size); } - + int offset = 0; - for (Collection<E> item : all) { - for (Iterator<E> it = item.iterator(); it.hasNext();) { - result[offset++] = it.next(); + for (Collection<? extends E> item : all) { + for (E e : item) { + result[offset++] = e; } } if (result.length > size) { @@ -301,7 +302,7 @@ public class CompositeCollection<E> implements Collection<E> { return false; } boolean changed = false; - for (Collection<E> item : all) { + for (Collection<? extends E> item : all) { changed |= item.removeAll(coll); } return changed; @@ -319,7 +320,7 @@ public class CompositeCollection<E> implements Collection<E> { */ public boolean retainAll(final Collection<?> coll) { boolean changed = false; - for (Collection<E> item : all) { + for (Collection<? extends E> item : all) { changed |= item.retainAll(coll); } return changed; @@ -333,7 +334,7 @@ public class CompositeCollection<E> implements Collection<E> { * @throws UnsupportedOperationException if clear is unsupported */ public void clear() { - for (Collection<E> coll : all) { + for (Collection<? extends E> coll : all) { coll.clear(); } } @@ -413,18 +414,26 @@ public class CompositeCollection<E> implements Collection<E> { * * @return Unmodifiable list of all collections in this composite. */ - public List<Collection<E>> getCollections() { + public List<? extends Collection<E>> getCollections() { return UnmodifiableList.decorate(all); } + /** + * Get the collection mutator to be used for this CompositeCollection. + * @return CollectionMutator<E> + */ + protected CollectionMutator<E> getMutator() { + return mutator; + } + //----------------------------------------------------------------------- /** * Pluggable strategy to handle changes to the composite. - * + * * @param <E> the element being held in the collection */ public interface CollectionMutator<E> { - + /** * Called when an object is to be added to the composite. * @@ -438,7 +447,7 @@ public class CompositeCollection<E> implements Collection<E> { * @throws IllegalArgumentException if the object cannot be added */ public boolean add(CompositeCollection<E> composite, List<Collection<E>> collections, E obj); - + /** * Called when a collection is to be added to the composite. * @@ -452,7 +461,7 @@ public class CompositeCollection<E> implements Collection<E> { * @throws IllegalArgumentException if the object cannot be added */ public boolean addAll(CompositeCollection<E> composite, List<Collection<E>> collections, Collection<? extends E> coll); - + /** * Called when an object is to be removed to the composite. * @@ -466,7 +475,7 @@ public class CompositeCollection<E> implements Collection<E> { * @throws IllegalArgumentException if the object cannot be removed */ public boolean remove(CompositeCollection<E> composite, List<Collection<E>> collections, Object obj); - + } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/collection/TransformedCollection.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/collection/TransformedCollection.java b/src/java/org/apache/commons/collections/collection/TransformedCollection.java index 8a652fc..7aaa7a3 100644 --- a/src/java/org/apache/commons/collections/collection/TransformedCollection.java +++ b/src/java/org/apache/commons/collections/collection/TransformedCollection.java @@ -44,7 +44,7 @@ public class TransformedCollection<E> extends AbstractCollectionDecorator<E> { private static final long serialVersionUID = 8692300188161871514L; /** The transformer to use */ - protected final Transformer<E, E> transformer; + protected final Transformer<? super E, ? extends E> transformer; /** * Factory method to create a transforming collection. @@ -57,8 +57,8 @@ public class TransformedCollection<E> extends AbstractCollectionDecorator<E> { * @return a new transformed collection * @throws IllegalArgumentException if collection or transformer is null */ - public static Collection decorate(Collection coll, Transformer transformer) { - return new TransformedCollection(coll, transformer); + public static <E> Collection<E> decorate(Collection<E> coll, Transformer<? super E, ? extends E> transformer) { + return new TransformedCollection<E>(coll, transformer); } //----------------------------------------------------------------------- @@ -72,7 +72,7 @@ public class TransformedCollection<E> extends AbstractCollectionDecorator<E> { * @param transformer the transformer to use for conversion, must not be null * @throws IllegalArgumentException if collection or transformer is null */ - protected TransformedCollection(Collection coll, Transformer transformer) { + protected TransformedCollection(Collection<E> coll, Transformer<? super E, ? extends E> transformer) { super(coll); if (transformer == null) { throw new IllegalArgumentException("Transformer must not be null"); @@ -88,8 +88,8 @@ public class TransformedCollection<E> extends AbstractCollectionDecorator<E> { * @param object the object to transform * @return a transformed object */ - protected E transform(Object object) { - return transformer.transform((E) object); + protected E transform(E object) { + return transformer.transform(object); } /** @@ -100,20 +100,20 @@ public class TransformedCollection<E> extends AbstractCollectionDecorator<E> { * @param coll the collection to transform * @return a transformed object */ - protected Collection transform(Collection coll) { - List list = new ArrayList(coll.size()); - for (Object item : coll) { + protected Collection<E> transform(Collection<? extends E> coll) { + List<E> list = new ArrayList<E>(coll.size()); + for (E item : coll) { list.add(transform(item)); } return list; } //----------------------------------------------------------------------- - public boolean add(Object object) { + public boolean add(E object) { return decorated().add(transform(object)); } - public boolean addAll(Collection coll) { + public boolean addAll(Collection<? extends E> coll) { return decorated().addAll(transform(coll)); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/collection/UnmodifiableBoundedCollection.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/collection/UnmodifiableBoundedCollection.java b/src/java/org/apache/commons/collections/collection/UnmodifiableBoundedCollection.java index e1ac9b4..f94eef4 100644 --- a/src/java/org/apache/commons/collections/collection/UnmodifiableBoundedCollection.java +++ b/src/java/org/apache/commons/collections/collection/UnmodifiableBoundedCollection.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. @@ -23,11 +23,11 @@ import org.apache.commons.collections.BoundedCollection; import org.apache.commons.collections.iterators.UnmodifiableIterator; /** - * <code>UnmodifiableBoundedCollection</code> decorates another + * <code>UnmodifiableBoundedCollection</code> decorates another * <code>BoundedCollection</code> to ensure it can't be altered. * <p> * If a BoundedCollection is first wrapped in some other collection decorator, - * such as synchronized or predicated, the BoundedCollection methods are no + * such as synchronized or predicated, the BoundedCollection methods are no * longer accessible. * The factory on this class will attempt to retrieve the bounded nature by * examining the package scope variables. @@ -36,81 +36,80 @@ import org.apache.commons.collections.iterators.UnmodifiableIterator; * * @since Commons Collections 3.0 * @version $Revision$ $Date$ - * + * * @author Stephen Colebourne */ -public final class UnmodifiableBoundedCollection - extends AbstractCollectionDecorator - implements BoundedCollection { +public final class UnmodifiableBoundedCollection<E> extends AbstractCollectionDecorator<E> + implements BoundedCollection<E> { /** Serialization version */ private static final long serialVersionUID = -7112672385450340330L; /** * Factory method to create an unmodifiable bounded collection. - * + * * @param coll the <code>BoundedCollection</code> to decorate, must not be null * @return a new unmodifiable bounded collection * @throws IllegalArgumentException if bag is null */ - public static BoundedCollection decorate(BoundedCollection coll) { - return new UnmodifiableBoundedCollection(coll); + public static <E> BoundedCollection<E> decorate(BoundedCollection<E> coll) { + return new UnmodifiableBoundedCollection<E>(coll); } - + /** * Factory method to create an unmodifiable bounded collection. * <p> - * This method is capable of drilling down through up to 1000 other decorators + * This method is capable of drilling down through up to 1000 other decorators * to find a suitable BoundedCollection. - * + * * @param coll the <code>BoundedCollection</code> to decorate, must not be null * @return a new unmodifiable bounded collection * @throws IllegalArgumentException if bag is null */ - public static BoundedCollection decorateUsing(Collection coll) { + @SuppressWarnings("unchecked") + public static <E> BoundedCollection<E> decorateUsing(Collection<? super E> coll) { if (coll == null) { throw new IllegalArgumentException("The collection must not be null"); } - + // handle decorators for (int i = 0; i < 1000; i++) { // counter to prevent infinite looping if (coll instanceof BoundedCollection) { break; // normal loop exit - } else if (coll instanceof AbstractCollectionDecorator) { - coll = ((AbstractCollectionDecorator) coll).collection; + } + if (coll instanceof AbstractCollectionDecorator) { + coll = ((AbstractCollectionDecorator<E>) coll).collection; } else if (coll instanceof SynchronizedCollection) { - coll = ((SynchronizedCollection) coll).collection; - } else { - break; // normal loop exit + coll = ((SynchronizedCollection<E>) coll).collection; } } - + if (coll instanceof BoundedCollection == false) { throw new IllegalArgumentException("The collection is not a bounded collection"); } - return new UnmodifiableBoundedCollection((BoundedCollection) coll); - } - + return new UnmodifiableBoundedCollection((BoundedCollection<E>) coll); + } + /** * Constructor that wraps (not copies). - * + * * @param coll the collection to decorate, must not be null * @throws IllegalArgumentException if coll is null */ - private UnmodifiableBoundedCollection(BoundedCollection coll) { + private UnmodifiableBoundedCollection(BoundedCollection<E> coll) { super(coll); } //----------------------------------------------------------------------- - public Iterator iterator() { + public Iterator<E> iterator() { return UnmodifiableIterator.decorate(decorated().iterator()); } - public boolean add(Object object) { + public boolean add(E object) { throw new UnsupportedOperationException(); } - public boolean addAll(Collection coll) { + public boolean addAll(Collection<? extends E> coll) { throw new UnsupportedOperationException(); } @@ -122,21 +121,28 @@ public final class UnmodifiableBoundedCollection 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 boolean isFull() { - return ((BoundedCollection) collection).isFull(); + return decorated().isFull(); } public int maxSize() { - return ((BoundedCollection) collection).maxSize(); + return decorated().maxSize(); } + /** + * {@inheritDoc} + */ + @Override + protected BoundedCollection<E> decorated() { + return (BoundedCollection<E>) super.decorated(); + } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/comparators/BooleanComparator.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/comparators/BooleanComparator.java b/src/java/org/apache/commons/collections/comparators/BooleanComparator.java index 2be6029..0efd38f 100644 --- a/src/java/org/apache/commons/collections/comparators/BooleanComparator.java +++ b/src/java/org/apache/commons/collections/comparators/BooleanComparator.java @@ -32,7 +32,7 @@ import java.util.Comparator; * * @author Rodney Waldhoff */ -public final class BooleanComparator implements Comparator, Serializable { +public final class BooleanComparator implements Comparator<Boolean>, Serializable { /** Serialization version. */ private static final long serialVersionUID = 1830042991606340609L; @@ -127,22 +127,6 @@ public final class BooleanComparator implements Comparator, Serializable { //----------------------------------------------------------------------- /** - * Compares two arbitrary Objects. - * When both arguments are <code>Boolean</code>, this method is equivalent to - * {@link #compare(Boolean,Boolean) compare((Boolean)<i>obj1</i>,(Boolean)<i>obj2</i>)}. - * When either argument is not a <code>Boolean</code>, this methods throws - * a {@link ClassCastException}. - * - * @param obj1 the first object to compare - * @param obj2 the second object to compare - * @return negative if obj1 is less, positive if greater, zero if equal - * @throws ClassCastException when either argument is not <code>Boolean</code> - */ - public int compare(Object obj1, Object obj2) { - return compare((Boolean)obj1, (Boolean)obj2); - } - - /** * Compares two non-<code>null</code> <code>Boolean</code> objects * according to the value of {@link #sortsTrueFirst()}. * http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/comparators/ComparableComparator.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/comparators/ComparableComparator.java b/src/java/org/apache/commons/collections/comparators/ComparableComparator.java index d643e3e..6cac25c 100644 --- a/src/java/org/apache/commons/collections/comparators/ComparableComparator.java +++ b/src/java/org/apache/commons/collections/comparators/ComparableComparator.java @@ -42,13 +42,14 @@ import java.util.Comparator; * * @see java.util.Collections#reverseOrder() */ -public class ComparableComparator implements Comparator, Serializable { +public class ComparableComparator<E extends Comparable<? super E>> implements Comparator<E>, Serializable { /** Serialization version. */ private static final long serialVersionUID=-291439688585137865L; /** The singleton instance. */ - private static final ComparableComparator instance = new ComparableComparator(); + @SuppressWarnings("unchecked") + public static final ComparableComparator<?> INSTANCE = new ComparableComparator(); //----------------------------------------------------------------------- /** @@ -60,8 +61,9 @@ public class ComparableComparator implements Comparator, Serializable { * * @return the singleton ComparableComparator */ - public static ComparableComparator getInstance() { - return instance; + @SuppressWarnings("unchecked") + public static <E extends Comparable<? super E>> ComparableComparator<E> getInstance() { + return (ComparableComparator<E>) INSTANCE; } //----------------------------------------------------------------------- @@ -88,8 +90,8 @@ public class ComparableComparator implements Comparator, Serializable { * @throws ClassCastException when <i>obj1</i> is not a <code>Comparable</code>, * or when <code>((Comparable)obj1).compareTo(obj2)</code> does */ - public int compare(Object obj1, Object obj2) { - return ((Comparable)obj1).compareTo(obj2); + public int compare(E obj1, E obj2) { + return obj1.compareTo(obj2); } //-----------------------------------------------------------------------
