Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/CollectionUtils.java URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/CollectionUtils.java?rev=738956&r1=738955&r2=738956&view=diff ============================================================================== --- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/CollectionUtils.java (original) +++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/CollectionUtils.java Thu Jan 29 18:48:37 2009 @@ -63,9 +63,9 @@ private static class CardinalityHelper<O> { final Map<O, Integer> cardinalityA, cardinalityB; - public <I1 extends O, I2 extends O> CardinalityHelper(Iterable<I1> a, Iterable<I2> b) { - cardinalityA = CollectionUtils.<O, I1>getCardinalityMap(a); - cardinalityB = CollectionUtils.<O, I2>getCardinalityMap(b); + public CardinalityHelper(Iterable<? extends O> a, Iterable<? extends O> b) { + cardinalityA = CollectionUtils.<O>getCardinalityMap(a); + cardinalityB = CollectionUtils.<O>getCardinalityMap(b); } public final int max(Object obj) { @@ -119,7 +119,6 @@ return newList; } - } /** @@ -292,9 +291,9 @@ * super type of <I>. * @return the populated cardinality map */ - public static <O, I extends O> Map<O, Integer> getCardinalityMap(final Iterable<I> coll) { + public static <O> Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll) { Map<O, Integer> count = new HashMap<O, Integer>(); - for (I obj : coll) { + for (O obj : coll) { Integer c = count.get(obj); if (c == null) { count.put(obj, 1); @@ -490,7 +489,8 @@ * @param transformer * the transformer to perform, may be null */ - public static <C> void transform(Collection<C> collection, Transformer<? super C, ? extends C> transformer) { + public static <C> void transform(Collection<C> collection, + Transformer<? super C, ? extends C> transformer) { if (collection != null && transformer != null) { if (collection instanceof List) { List<C> list = (List<C>) collection; @@ -567,7 +567,8 @@ * @throws NullPointerException * if the input collection is null */ - public static <O, I extends O> Collection<O> select(Collection<I> inputCollection, Predicate<? super I> predicate) { + public static <O> Collection<O> select(Collection<? extends O> inputCollection, + Predicate<? super O> predicate) { return select(inputCollection, predicate, new ArrayList<O>(inputCollection.size())); } @@ -586,9 +587,10 @@ * the collection to output into, may not be null * @return outputCollection */ - public static <O, I extends O, R extends Collection<O>> R select(Collection<I> inputCollection, Predicate<? super I> predicate, R outputCollection) { + public static <O, R extends Collection<? super O>> R select(Collection<? extends O> inputCollection, + Predicate<? super O> predicate, R outputCollection) { if (inputCollection != null && predicate != null) { - for (I item : inputCollection) { + for (O item : inputCollection) { if (predicate.evaluate(item)) { outputCollection.add(item); } @@ -612,7 +614,8 @@ * @throws NullPointerException * if the input collection is null */ - public static <O, I extends O> Collection<O> selectRejected(Collection<I> inputCollection, Predicate<? super I> predicate) { + public static <O> Collection<O> selectRejected(Collection<? extends O> inputCollection, + Predicate<? super O> predicate) { return selectRejected(inputCollection, predicate, new ArrayList<O>(inputCollection.size())); } @@ -631,9 +634,10 @@ * the collection to output into, may not be null * @return outputCollection */ - public static <O, I extends O, R extends Collection<O>> R selectRejected(Collection<I> inputCollection, Predicate<? super I> predicate, R outputCollection) { + public static <O, R extends Collection<? super O>> R selectRejected( + Collection<? extends O> inputCollection, Predicate<? super O> predicate, R outputCollection) { if (inputCollection != null && predicate != null) { - for (I item : inputCollection) { + for (O item : inputCollection) { if (!predicate.evaluate(item)) { outputCollection.add(item); } @@ -658,7 +662,8 @@ * @throws NullPointerException * if the input collection is null */ - public static <I,O> Collection<O> collect(Iterable<I> inputCollection, Transformer<? super I, ? extends O> transformer) { + public static <I, O> Collection<O> collect(Iterable<I> inputCollection, + Transformer<? super I, ? extends O> transformer) { ArrayList<O> answer = new ArrayList<O>(); collect(inputCollection, transformer, answer); return answer; @@ -679,7 +684,8 @@ * @param <O> the type of object in the output collection * @return the transformed result (new list) */ - public static <I,O> Collection<O> collect(Iterator<I> inputIterator, Transformer<? super I, ? extends O> transformer) { + public static <I, O> Collection<O> collect(Iterator<I> inputIterator, + Transformer<? super I, ? extends O> transformer) { ArrayList<O> answer = new ArrayList<O>(); collect(inputIterator, transformer, answer); return answer; @@ -701,7 +707,8 @@ * @return the outputCollection with the transformed input added * @throws NullPointerException if the output collection is null */ - public static <I,O,T extends O, R extends Collection<O>> R collect(Iterable<I> inputCollection, final Transformer<? super I,T> transformer, final R outputCollection) { + public static <I, O, R extends Collection<? super O>> R collect(Iterable<? extends I> inputCollection, + final Transformer<? super I, ? extends O> transformer, final R outputCollection) { if (inputCollection != null) { return collect(inputCollection.iterator(), transformer, outputCollection); } @@ -725,11 +732,12 @@ * @throws NullPointerException if the output collection is null */ //TODO - deprecate and replace with IteratorIterable - public static <I,O,T extends O, R extends Collection<O>> R collect(Iterator<I> inputIterator, final Transformer<? super I,T> transformer, final R outputCollection) { + public static <I, O, R extends Collection<? super O>> R collect(Iterator<? extends I> inputIterator, + final Transformer<? super I, ? extends O> transformer, final R outputCollection) { if (inputIterator != null && transformer != null) { while (inputIterator.hasNext()) { I item = inputIterator.next(); - T value = transformer.transform(item); + O value = transformer.transform(item); outputCollection.add(value); } } @@ -813,7 +821,7 @@ * @throws NullPointerException * if the collection or array is null */ - public static <C, T extends C> void addAll(Collection<C> collection, T[] elements) { + public static <C> void addAll(Collection<C> collection, C[] elements) { for (int i = 0, size = elements.length; i < size; i++) { collection.add(elements[i]); } @@ -913,13 +921,13 @@ throw new IndexOutOfBoundsException("Index cannot be negative: " + i); } if (object instanceof Map) { - Map map = (Map) object; - Iterator iterator = map.entrySet().iterator(); + Map<?, ?> map = (Map<?, ?>) object; + Iterator<?> iterator = map.entrySet().iterator(); return get(iterator, i); } else if (object instanceof Object[]) { return ((Object[]) object)[i]; } else if (object instanceof Iterator) { - Iterator it = (Iterator) object; + Iterator<?> it = (Iterator<?>) object; while (it.hasNext()) { i--; if (i == -1) { @@ -929,10 +937,10 @@ } throw new IndexOutOfBoundsException("Entry does not exist: " + i); } else if (object instanceof Collection) { - Iterator iterator = ((Collection) object).iterator(); + Iterator<?> iterator = ((Collection<?>) object).iterator(); return get(iterator, i); } else if (object instanceof Enumeration) { - Enumeration it = (Enumeration) object; + Enumeration<?> it = (Enumeration<?>) object; while (it.hasMoreElements()) { i--; if (i == -1) { @@ -987,19 +995,19 @@ public static int size(Object object) { int total = 0; if (object instanceof Map) { - total = ((Map) object).size(); + total = ((Map<?, ?>) object).size(); } else if (object instanceof Collection) { - total = ((Collection) object).size(); + total = ((Collection<?>) object).size(); } else if (object instanceof Object[]) { total = ((Object[]) object).length; } else if (object instanceof Iterator) { - Iterator it = (Iterator) object; + Iterator<?> it = (Iterator<?>) object; while (it.hasNext()) { total++; it.next(); } } else if (object instanceof Enumeration) { - Enumeration it = (Enumeration) object; + Enumeration<?> it = (Enumeration<?>) object; while (it.hasMoreElements()) { total++; it.nextElement(); @@ -1038,15 +1046,15 @@ */ public static boolean sizeIsEmpty(Object object) { if (object instanceof Collection) { - return ((Collection) object).isEmpty(); + return ((Collection<?>) object).isEmpty(); } else if (object instanceof Map) { - return ((Map) object).isEmpty(); + return ((Map<?, ?>) object).isEmpty(); } else if (object instanceof Object[]) { return ((Object[]) object).length == 0; } else if (object instanceof Iterator) { - return ((Iterator) object).hasNext() == false; + return ((Iterator<?>) object).hasNext() == false; } else if (object instanceof Enumeration) { - return ((Enumeration) object).hasMoreElements() == false; + return ((Enumeration<?>) object).hasMoreElements() == false; } else if (object == null) { throw new IllegalArgumentException("Unsupported object type: null"); } else { @@ -1068,7 +1076,7 @@ * @return true if empty or null * @since Commons Collections 3.2 */ - public static boolean isEmpty(Collection coll) { + public static boolean isEmpty(Collection<?> coll) { return (coll == null || coll.isEmpty()); } @@ -1081,7 +1089,7 @@ * @return true if non-null and non-empty * @since Commons Collections 3.2 */ - public static boolean isNotEmpty(Collection coll) { + public static boolean isNotEmpty(Collection<?> coll) { return !isEmpty(coll); } @@ -1120,17 +1128,17 @@ * @return true if the BoundedCollection is full * @throws NullPointerException if the collection is null */ - public static boolean isFull(Collection coll) { + @SuppressWarnings("unchecked") + public static boolean isFull(Collection<?> coll) { if (coll == null) { throw new NullPointerException("The collection must not be null"); } if (coll instanceof BoundedCollection) { - return ((BoundedCollection) coll).isFull(); + return ((BoundedCollection<?>) coll).isFull(); } try { - BoundedCollection bcoll = UnmodifiableBoundedCollection.decorateUsing(coll); + BoundedCollection<?> bcoll = UnmodifiableBoundedCollection.decorateUsing((Collection<Object>) coll); return bcoll.isFull(); - } catch (IllegalArgumentException ex) { return false; } @@ -1151,17 +1159,17 @@ * @return the maximum size of the BoundedCollection, -1 if no maximum size * @throws NullPointerException if the collection is null */ - public static int maxSize(Collection coll) { + @SuppressWarnings("unchecked") + public static int maxSize(Collection<?> coll) { if (coll == null) { throw new NullPointerException("The collection must not be null"); } if (coll instanceof BoundedCollection) { - return ((BoundedCollection) coll).maxSize(); + return ((BoundedCollection<?>) coll).maxSize(); } try { - BoundedCollection bcoll = UnmodifiableBoundedCollection.decorateUsing(coll); + BoundedCollection<?> bcoll = UnmodifiableBoundedCollection.decorateUsing((Collection<Object>) coll); return bcoll.maxSize(); - } catch (IllegalArgumentException ex) { return -1; } @@ -1203,7 +1211,7 @@ * @throws NullPointerException if either parameter is null * @since Commons Collections 3.3 (method existed in 3.2 but was completely broken) */ - public static Collection removeAll(Collection collection, Collection remove) { + public static <E> Collection<E> removeAll(Collection<E> collection, Collection<?> remove) { return ListUtils.removeAll(collection, remove); } @@ -1277,7 +1285,7 @@ * @return a transformed collection backed by the given collection * @throws IllegalArgumentException if the Collection or Transformer is null */ - public static Collection transformedCollection(Collection collection, Transformer transformer) { + public static <E> Collection<E> transformedCollection(Collection<E> collection, Transformer<? super E, ? extends E> transformer) { return TransformedCollection.decorate(collection, transformer); }
Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/ComparatorUtils.java URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/ComparatorUtils.java?rev=738956&r1=738955&r2=738956&view=diff ============================================================================== --- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/ComparatorUtils.java (original) +++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/ComparatorUtils.java Thu Jan 29 18:48:37 2009 @@ -54,14 +54,16 @@ * * @see ComparableComparator#getInstance */ - public static final Comparator NATURAL_COMPARATOR = ComparableComparator.getInstance(); + @SuppressWarnings("unchecked") + public static final Comparator NATURAL_COMPARATOR = ComparableComparator.<Comparable>getInstance(); /** * Gets a comparator that uses the natural order of the objects. * * @return a comparator which uses natural order */ - public static Comparator naturalComparator() { + @SuppressWarnings("unchecked") + public static <E extends Comparable<? super E>> Comparator<E> naturalComparator() { return NATURAL_COMPARATOR; } @@ -76,7 +78,8 @@ * @throws NullPointerException if either comparator is null * @see ComparatorChain */ - public static Comparator chainedComparator(Comparator comparator1, Comparator comparator2) { + @SuppressWarnings("unchecked") + public static <E extends Comparable<? super E>> Comparator<E> chainedComparator(Comparator<E> comparator1, Comparator<E> comparator2) { return chainedComparator(new Comparator[] {comparator1, comparator2}); } @@ -89,8 +92,8 @@ * @throws NullPointerException if comparators array is null or contains a null * @see ComparatorChain */ - public static Comparator chainedComparator(Comparator[] comparators) { - ComparatorChain chain = new ComparatorChain(); + public static <E extends Comparable<? super E>> Comparator<E> chainedComparator(Comparator<E>[] comparators) { + ComparatorChain<E> chain = new ComparatorChain<E>(); for (int i = 0; i < comparators.length; i++) { if (comparators[i] == null) { throw new NullPointerException("Comparator cannot be null"); @@ -111,9 +114,10 @@ * @throws ClassCastException if the comparators collection contains the wrong object type * @see ComparatorChain */ - public static Comparator chainedComparator(Collection comparators) { + @SuppressWarnings("unchecked") + public static <E extends Comparable<? super E>> Comparator<E> chainedComparator(Collection<Comparator<E>> comparators) { return chainedComparator( - (Comparator[]) comparators.toArray(new Comparator[comparators.size()]) + (Comparator<E>[]) comparators.toArray(new Comparator[comparators.size()]) ); } @@ -124,11 +128,8 @@ * @return a comparator that reverses the order of the input comparator * @see ReverseComparator */ - public static Comparator reversedComparator(Comparator comparator) { - if (comparator == null) { - comparator = NATURAL_COMPARATOR; - } - return new ReverseComparator(comparator); + public static <E> Comparator<E> reversedComparator(Comparator<E> comparator) { + return new ReverseComparator<E>(comparator); } /** @@ -143,7 +144,7 @@ * <code>false</code> {...@link Boolean}s. * @return a comparator that sorts booleans */ - public static Comparator booleanComparator(boolean trueFirst) { + public static Comparator<Boolean> booleanComparator(boolean trueFirst) { return BooleanComparator.getBooleanComparator(trueFirst); } @@ -158,11 +159,12 @@ * @return a version of that comparator that allows nulls * @see NullComparator */ - public static Comparator nullLowComparator(Comparator comparator) { + @SuppressWarnings("unchecked") + public static <E> Comparator<E> nullLowComparator(Comparator<E> comparator) { if (comparator == null) { comparator = NATURAL_COMPARATOR; } - return new NullComparator(comparator, false); + return new NullComparator<E>(comparator, false); } /** @@ -176,11 +178,12 @@ * @return a version of that comparator that allows nulls * @see NullComparator */ - public static Comparator nullHighComparator(Comparator comparator) { + @SuppressWarnings("unchecked") + public static <E> Comparator<E> nullHighComparator(Comparator<E> comparator) { if (comparator == null) { comparator = NATURAL_COMPARATOR; } - return new NullComparator(comparator, true); + return new NullComparator<E>(comparator, true); } /** @@ -195,11 +198,12 @@ * @return a comparator that transforms its input objects before comparing them * @see TransformingComparator */ - public static Comparator transformedComparator(Comparator comparator, Transformer transformer) { + @SuppressWarnings("unchecked") + public static <E> Comparator<E> transformedComparator(Comparator<E> comparator, Transformer<? super E, ? extends E> transformer) { if (comparator == null) { comparator = NATURAL_COMPARATOR; } - return new TransformingComparator(transformer, comparator); + return new TransformingComparator<E>(transformer, comparator); } /** @@ -212,7 +216,8 @@ * @param comparator the sort order to use * @return the smaller of the two objects */ - public static Object min(Object o1, Object o2, Comparator comparator) { + @SuppressWarnings("unchecked") + public static <E> E min(E o1, E o2, Comparator<E> comparator) { if (comparator == null) { comparator = NATURAL_COMPARATOR; } @@ -230,7 +235,8 @@ * @param comparator the sort order to use * @return the larger of the two objects */ - public static Object max(Object o1, Object o2, Comparator comparator) { + @SuppressWarnings("unchecked") + public static <E> E max(E o1, E o2, Comparator<E> comparator) { if (comparator == null) { comparator = NATURAL_COMPARATOR; } Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/EnumerationUtils.java URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/EnumerationUtils.java?rev=738956&r1=738955&r2=738956&view=diff ============================================================================== --- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/EnumerationUtils.java (original) +++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/EnumerationUtils.java Thu Jan 29 18:48:37 2009 @@ -16,8 +16,10 @@ */ package org.apache.commons.collections; +import java.util.ArrayList; import java.util.Enumeration; import java.util.List; +import java.util.StringTokenizer; import org.apache.commons.collections.iterators.EnumerationIterator; @@ -47,8 +49,21 @@ * @param enumeration the enumeration to traverse, which should not be <code>null</code>. * @throws NullPointerException if the enumeration parameter is <code>null</code>. */ - public static List toList(Enumeration enumeration) { - return IteratorUtils.toList(new EnumerationIterator(enumeration)); + public static <E> List<E> toList(Enumeration<E> enumeration) { + return IteratorUtils.toList(new EnumerationIterator<E>(enumeration)); } + /** + * Override toList(Enumeration) for StringTokenizer as it implements Enumeration<String> + * for the sake of backward compatibility. + * @param stringTokenizer + * @return List<String> + */ + public static List<String> toList(StringTokenizer stringTokenizer) { + List<String> result = new ArrayList<String>(stringTokenizer.countTokens()); + while (stringTokenizer.hasMoreTokens()) { + result.add(stringTokenizer.nextToken()); + } + return result; + } } Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/FactoryUtils.java URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/FactoryUtils.java?rev=738956&r1=738955&r2=738956&view=diff ============================================================================== --- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/FactoryUtils.java (original) +++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/FactoryUtils.java Thu Jan 29 18:48:37 2009 @@ -55,8 +55,8 @@ * * @return the factory */ - public static Factory exceptionFactory() { - return ExceptionFactory.INSTANCE; + public static <T> Factory<T> exceptionFactory() { + return ExceptionFactory.<T>getInstance(); } /** @@ -68,7 +68,7 @@ * @return the factory */ public static <T> Factory<T> nullFactory() { - return ConstantFactory.NULL_INSTANCE; + return ConstantFactory.<T>getInstance(null); } /** @@ -82,7 +82,7 @@ * @param constantToReturn the constant object to return each time in the factory * @return the <code>constant</code> factory. */ - public static Factory constantFactory(Object constantToReturn) { + public static <T> Factory<T> constantFactory(T constantToReturn) { return ConstantFactory.getInstance(constantToReturn); } @@ -103,8 +103,8 @@ * @throws IllegalArgumentException if the prototype is null * @throws IllegalArgumentException if the prototype cannot be cloned */ - public static Factory prototypeFactory(Object prototype) { - return PrototypeFactory.getInstance(prototype); + public static <T> Factory<T> prototypeFactory(T prototype) { + return PrototypeFactory.<T>getInstance(prototype); } /** @@ -135,7 +135,7 @@ * @throws IllegalArgumentException if the paramTypes and args don't match * @throws IllegalArgumentException if the constructor doesn't exist */ - public static <T> Factory<T> instantiateFactory(Class<T> classToInstantiate, Class[] paramTypes, Object[] args) { + public static <T> Factory<T> instantiateFactory(Class<T> classToInstantiate, Class<?>[] paramTypes, Object[] args) { return InstantiateFactory.getInstance(classToInstantiate, paramTypes, args); } Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/FunctorException.java URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/FunctorException.java?rev=738956&r1=738955&r2=738956&view=diff ============================================================================== --- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/FunctorException.java (original) +++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/FunctorException.java Thu Jan 29 18:48:37 2009 @@ -30,11 +30,14 @@ */ public class FunctorException extends RuntimeException { + /** Serialization version */ + private static final long serialVersionUID = 9139387246344345475L; + /** * Does JDK support nested exceptions */ private static final boolean JDK_SUPPORTS_NESTED; - + static { boolean flag = false; try { @@ -45,7 +48,7 @@ } JDK_SUPPORTS_NESTED = flag; } - + /** * Root cause of the exception */ Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/IteratorUtils.java URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/IteratorUtils.java?rev=738956&r1=738955&r2=738956&view=diff ============================================================================== --- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/IteratorUtils.java (original) +++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/IteratorUtils.java Thu Jan 29 18:48:37 2009 @@ -81,26 +81,30 @@ * WARNING: This constant is binary incompatible with Commons Collections 2.1 and 2.1.1. * Use <code>EmptyIterator.INSTANCE</code> for compatability with Commons Collections 2.1.1. */ - public static final ResettableIterator EMPTY_ITERATOR = EmptyIterator.RESETTABLE_INSTANCE; + public static final ResettableIterator<Object> EMPTY_ITERATOR = EmptyIterator.RESETTABLE_INSTANCE; + /** * A list iterator over no elements. * <p> * WARNING: This constant is binary incompatible with Commons Collections 2.1 and 2.1.1. * Use <code>EmptyListIterator.INSTANCE</code> for compatability with Commons Collections 2.1.1. */ - public static final ResettableListIterator EMPTY_LIST_ITERATOR = EmptyListIterator.RESETTABLE_INSTANCE; + public static final ResettableListIterator<Object> EMPTY_LIST_ITERATOR = EmptyListIterator.RESETTABLE_INSTANCE; + /** * An ordered iterator over no elements. */ - public static final OrderedIterator EMPTY_ORDERED_ITERATOR = EmptyOrderedIterator.INSTANCE; + public static final OrderedIterator<Object> EMPTY_ORDERED_ITERATOR = EmptyOrderedIterator.INSTANCE; + /** * A map iterator over no elements. */ - public static final MapIterator EMPTY_MAP_ITERATOR = EmptyMapIterator.INSTANCE; + public static final MapIterator<Object, Object> EMPTY_MAP_ITERATOR = EmptyMapIterator.INSTANCE; + /** * An ordered map iterator over no elements. */ - public static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR = EmptyOrderedMapIterator.INSTANCE; + public static final OrderedMapIterator<Object, Object> EMPTY_ORDERED_MAP_ITERATOR = EmptyOrderedMapIterator.INSTANCE; /** * IteratorUtils is not normally instantiated. @@ -121,8 +125,8 @@ * * @return an iterator over nothing */ - public static ResettableIterator emptyIterator() { - return EMPTY_ITERATOR; + public static <E> ResettableIterator<E> emptyIterator() { + return EmptyIterator.<E>getResettableInstance(); } /** @@ -136,8 +140,8 @@ * * @return a list iterator over nothing */ - public static ResettableListIterator emptyListIterator() { - return EMPTY_LIST_ITERATOR; + public static <E> ResettableListIterator<E> emptyListIterator() { + return EmptyListIterator.<E>getResettableInstance(); } /** @@ -148,8 +152,8 @@ * * @return an ordered iterator over nothing */ - public static OrderedIterator emptyOrderedIterator() { - return EMPTY_ORDERED_ITERATOR; + public static <E> OrderedIterator<E> emptyOrderedIterator() { + return EmptyOrderedIterator.<E>getInstance(); } /** @@ -160,8 +164,8 @@ * * @return a map iterator over nothing */ - public static MapIterator emptyMapIterator() { - return EMPTY_MAP_ITERATOR; + public static <K, V> MapIterator<K, V> emptyMapIterator() { + return EmptyMapIterator.<K, V>getInstance(); } /** @@ -172,8 +176,8 @@ * * @return a map iterator over nothing */ - public static OrderedMapIterator emptyOrderedMapIterator() { - return EMPTY_ORDERED_MAP_ITERATOR; + public static <K, V> OrderedMapIterator<K, V> emptyOrderedMapIterator() { + return EmptyOrderedMapIterator.<K, V>getInstance(); } // Singleton @@ -190,8 +194,8 @@ * @param object the single object over which to iterate * @return a singleton iterator over the object */ - public static ResettableIterator singletonIterator(Object object) { - return new SingletonIterator(object); + public static <E> ResettableIterator<E> singletonIterator(E object) { + return new SingletonIterator<E>(object); } /** @@ -203,8 +207,8 @@ * @param object the single object over which to iterate * @return a singleton list iterator over the object */ - public static ListIterator singletonListIterator(Object object) { - return new SingletonListIterator(object); + public static <E> ListIterator<E> singletonListIterator(E object) { + return new SingletonListIterator<E>(object); } // Arrays @@ -219,8 +223,8 @@ * @return an iterator over the array * @throws NullPointerException if array is null */ - public static ResettableIterator arrayIterator(Object[] array) { - return new ObjectArrayIterator(array); + public static <E> ResettableIterator<E> arrayIterator(E[] array) { + return new ObjectArrayIterator<E>(array); } /** @@ -234,8 +238,8 @@ * @throws IllegalArgumentException if the array is not an array * @throws NullPointerException if array is null */ - public static ResettableIterator arrayIterator(Object array) { - return new ArrayIterator(array); + public static <E> ResettableIterator<E> arrayIterator(Object array) { + return new ArrayIterator<E>(array); } /** @@ -251,8 +255,8 @@ * than the length of the array * @throws NullPointerException if array is null */ - public static ResettableIterator arrayIterator(Object[] array, int start) { - return new ObjectArrayIterator(array, start); + public static <E> ResettableIterator<E> arrayIterator(E[] array, int start) { + return new ObjectArrayIterator<E>(array, start); } /** @@ -269,8 +273,8 @@ * than the length of the array * @throws NullPointerException if array is null */ - public static ResettableIterator arrayIterator(Object array, int start) { - return new ArrayIterator(array, start); + public static <E> ResettableIterator<E> arrayIterator(Object array, int start) { + return new ArrayIterator<E>(array, start); } /** @@ -287,8 +291,8 @@ * @throws IllegalArgumentException if end is before start * @throws NullPointerException if array is null */ - public static ResettableIterator arrayIterator(Object[] array, int start, int end) { - return new ObjectArrayIterator(array, start, end); + public static <E> ResettableIterator<E> arrayIterator(E[] array, int start, int end) { + return new ObjectArrayIterator<E>(array, start, end); } /** @@ -306,8 +310,8 @@ * @throws IllegalArgumentException if end is before start * @throws NullPointerException if array is null */ - public static ResettableIterator arrayIterator(Object array, int start, int end) { - return new ArrayIterator(array, start, end); + public static <E> ResettableIterator<E> arrayIterator(Object array, int start, int end) { + return new ArrayIterator<E>(array, start, end); } //----------------------------------------------------------------------- @@ -318,8 +322,8 @@ * @return a list iterator over the array * @throws NullPointerException if array is null */ - public static ResettableListIterator arrayListIterator(Object[] array) { - return new ObjectArrayListIterator(array); + public static <E> ResettableListIterator<E> arrayListIterator(E[] array) { + return new ObjectArrayListIterator<E>(array); } /** @@ -333,8 +337,8 @@ * @throws IllegalArgumentException if the array is not an array * @throws NullPointerException if array is null */ - public static ResettableListIterator arrayListIterator(Object array) { - return new ArrayListIterator(array); + public static <E> ResettableListIterator<E> arrayListIterator(Object array) { + return new ArrayListIterator<E>(array); } /** @@ -346,8 +350,8 @@ * @throws IndexOutOfBoundsException if start is less than zero * @throws NullPointerException if array is null */ - public static ResettableListIterator arrayListIterator(Object[] array, int start) { - return new ObjectArrayListIterator(array, start); + public static <E> ResettableListIterator<E> arrayListIterator(E[] array, int start) { + return new ObjectArrayListIterator<E>(array, start); } /** @@ -363,8 +367,8 @@ * @throws IndexOutOfBoundsException if start is less than zero * @throws NullPointerException if array is null */ - public static ResettableListIterator arrayListIterator(Object array, int start) { - return new ArrayListIterator(array, start); + public static <E> ResettableListIterator<E> arrayListIterator(Object array, int start) { + return new ArrayListIterator<E>(array, start); } /** @@ -378,8 +382,8 @@ * @throws IllegalArgumentException if end is before start * @throws NullPointerException if array is null */ - public static ResettableListIterator arrayListIterator(Object[] array, int start, int end) { - return new ObjectArrayListIterator(array, start, end); + public static <E> ResettableListIterator<E> arrayListIterator(E[] array, int start, int end) { + return new ObjectArrayListIterator<E>(array, start, end); } /** @@ -397,8 +401,8 @@ * @throws IllegalArgumentException if end is before start * @throws NullPointerException if array is null */ - public static ResettableListIterator arrayListIterator(Object array, int start, int end) { - return new ArrayListIterator(array, start, end); + public static <E> ResettableListIterator<E> arrayListIterator(Object array, int start, int end) { + return new ArrayListIterator<E>(array, start, end); } // Unmodifiable @@ -411,7 +415,7 @@ * @param iterator the iterator to make immutable * @return an immutable version of the iterator */ - public static Iterator unmodifiableIterator(Iterator iterator) { + public static <E> Iterator<E> unmodifiableIterator(Iterator<E> iterator) { return UnmodifiableIterator.decorate(iterator); } @@ -424,7 +428,7 @@ * @param listIterator the iterator to make immutable * @return an immutable version of the iterator */ - public static ListIterator unmodifiableListIterator(ListIterator listIterator) { + public static <E> ListIterator<E> unmodifiableListIterator(ListIterator<E> listIterator) { return UnmodifiableListIterator.decorate(listIterator); } @@ -436,7 +440,7 @@ * @param mapIterator the iterator to make immutable * @return an immutable version of the iterator */ - public static MapIterator unmodifiableMapIterator(MapIterator mapIterator) { + public static <K, V> MapIterator<K, V> unmodifiableMapIterator(MapIterator<K, V> mapIterator) { return UnmodifiableMapIterator.decorate(mapIterator); } @@ -451,8 +455,8 @@ * @return a combination iterator over the iterators * @throws NullPointerException if either iterator is null */ - public static Iterator chainedIterator(Iterator iterator1, Iterator iterator2) { - return new IteratorChain(iterator1, iterator2); + public static <E> Iterator<E> chainedIterator(Iterator<? extends E> iterator1, Iterator<? extends E> iterator2) { + return new IteratorChain<E>(iterator1, iterator2); } /** @@ -463,8 +467,8 @@ * @return a combination iterator over the iterators * @throws NullPointerException if iterators array is null or contains a null */ - public static Iterator chainedIterator(Iterator[] iterators) { - return new IteratorChain(iterators); + public static <E> Iterator<E> chainedIterator(Iterator<? extends E>[] iterators) { + return new IteratorChain<E>(iterators); } /** @@ -476,8 +480,8 @@ * @throws NullPointerException if iterators collection is null or contains a null * @throws ClassCastException if the iterators collection contains the wrong object type */ - public static Iterator chainedIterator(Collection iterators) { - return new IteratorChain(iterators); + public static <E> Iterator<E> chainedIterator(Collection<Iterator<? extends E>> iterators) { + return new IteratorChain<E>(iterators); } // Collated @@ -498,8 +502,8 @@ * @return a combination iterator over the iterators * @throws NullPointerException if either iterator is null */ - public static Iterator collatedIterator(Comparator comparator, Iterator iterator1, Iterator iterator2) { - return new CollatingIterator(comparator, iterator1, iterator2); + public static <E> Iterator<E> collatedIterator(Comparator<? super E> comparator, Iterator<? extends E> iterator1, Iterator<? extends E> iterator2) { + return new CollatingIterator<E>(comparator, iterator1, iterator2); } /** @@ -517,8 +521,8 @@ * @return a combination iterator over the iterators * @throws NullPointerException if iterators array is null or contains a null */ - public static Iterator collatedIterator(Comparator comparator, Iterator[] iterators) { - return new CollatingIterator(comparator, iterators); + public static <E> Iterator<E> collatedIterator(Comparator<? super E> comparator, Iterator<? extends E>[] iterators) { + return new CollatingIterator<E>(comparator, iterators); } /** @@ -537,8 +541,9 @@ * @throws NullPointerException if iterators collection is null or contains a null * @throws ClassCastException if the iterators collection contains the wrong object type */ - public static Iterator collatedIterator(Comparator comparator, Collection iterators) { - return new CollatingIterator(comparator, iterators); + public static <E> Iterator<E> collatedIterator(Comparator<? super E> comparator, + Collection<Iterator<? extends E>> iterators) { + return new CollatingIterator<E>(comparator, iterators); } // Object Graph @@ -596,8 +601,8 @@ * @return a new object graph iterator * @since Commons Collections 3.1 */ - public static Iterator objectGraphIterator(Object root, Transformer transformer) { - return new ObjectGraphIterator(root, transformer); + public static <E> Iterator<E> objectGraphIterator(E root, Transformer<? super E, ? extends E> transformer) { + return new ObjectGraphIterator<E>(root, transformer); } // Transformed @@ -613,14 +618,14 @@ * @return a new transforming iterator * @throws NullPointerException if either parameter is null */ - public static Iterator transformedIterator(Iterator iterator, Transformer transform) { + public static <I, O> Iterator<O> transformedIterator(Iterator<? extends I> iterator, Transformer<? super I, ? extends O> transform) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } if (transform == null) { throw new NullPointerException("Transformer must not be null"); } - return new TransformIterator(iterator, transform); + return new TransformIterator<I, O>(iterator, transform); } // Filtered @@ -636,14 +641,14 @@ * @return a new filtered iterator * @throws NullPointerException if either parameter is null */ - public static Iterator filteredIterator(Iterator iterator, Predicate predicate) { + public static <E> Iterator<E> filteredIterator(Iterator<? extends E> iterator, Predicate<? super E> predicate) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } - return new FilterIterator(iterator, predicate); + return new FilterIterator<E>(iterator, predicate); } /** @@ -657,14 +662,14 @@ * @return a new filtered iterator * @throws NullPointerException if either parameter is null */ - public static ListIterator filteredListIterator(ListIterator listIterator, Predicate predicate) { + public static <E> ListIterator<E> filteredListIterator(ListIterator<? extends E> listIterator, Predicate<? super E> predicate) { if (listIterator == null) { throw new NullPointerException("ListIterator must not be null"); } if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } - return new FilterListIterator(listIterator, predicate); + return new FilterListIterator<E>(listIterator, predicate); } // Looping @@ -680,11 +685,11 @@ * @return a new looping iterator * @throws NullPointerException if the collection is null */ - public static ResettableIterator loopingIterator(Collection coll) { + public static <E> ResettableIterator<E> loopingIterator(Collection<? extends E> coll) { if (coll == null) { throw new NullPointerException("Collection must not be null"); } - return new LoopingIterator(coll); + return new LoopingIterator<E>(coll); } /** @@ -698,13 +703,13 @@ * @throws NullPointerException if the list is null * @since Commons Collections 3.2 */ - public static ResettableListIterator loopingListIterator(List list) { + public static <E> ResettableListIterator<E> loopingListIterator(List<E> list) { if (list == null) { throw new NullPointerException("List must not be null"); } - return new LoopingListIterator(list); + return new LoopingListIterator<E>(list); } - + // Views //----------------------------------------------------------------------- /** @@ -713,11 +718,11 @@ * @param enumeration the enumeration to use * @return a new iterator */ - public static Iterator asIterator(Enumeration enumeration) { + public static <E> Iterator<E> asIterator(Enumeration<? extends E> enumeration) { if (enumeration == null) { throw new NullPointerException("Enumeration must not be null"); } - return new EnumerationIterator(enumeration); + return new EnumerationIterator<E>(enumeration); } /** @@ -728,14 +733,14 @@ * @param removeCollection the collection to remove elements from * @return a new iterator */ - public static Iterator asIterator(Enumeration enumeration, Collection removeCollection) { + public static <E> Iterator<E> asIterator(Enumeration<? extends E> enumeration, Collection<? super E> removeCollection) { if (enumeration == null) { throw new NullPointerException("Enumeration must not be null"); } if (removeCollection == null) { throw new NullPointerException("Collection must not be null"); } - return new EnumerationIterator(enumeration, removeCollection); + return new EnumerationIterator<E>(enumeration, removeCollection); } /** @@ -745,11 +750,11 @@ * @return a new enumeration * @throws NullPointerException if iterator is null */ - public static Enumeration asEnumeration(Iterator iterator) { + public static <E> Enumeration<E> asEnumeration(Iterator<? extends E> iterator) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } - return new IteratorEnumeration(iterator); + return new IteratorEnumeration<E>(iterator); } /** @@ -762,11 +767,11 @@ * @return a new iterator * @throws NullPointerException if iterator parameter is null */ - public static ListIterator toListIterator(Iterator iterator) { + public static <E> ListIterator<E> toListIterator(Iterator<? extends E> iterator) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } - return new ListIteratorWrapper(iterator); + return new ListIteratorWrapper<E>(iterator); } /** @@ -779,14 +784,14 @@ * @return an array of the iterator contents * @throws NullPointerException if iterator parameter is null */ - public static Object[] toArray(Iterator iterator) { + public static Object[] toArray(Iterator<?> iterator) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } - List list = toList(iterator, 100); + List<?> list = toList(iterator, 100); return list.toArray(); } - + /** * Gets an array based on an iterator. * <p> @@ -800,15 +805,16 @@ * @throws NullPointerException if arrayClass is null * @throws ClassCastException if the arrayClass is invalid */ - public static Object[] toArray(Iterator iterator, Class arrayClass) { + @SuppressWarnings("unchecked") + public static <E> E[] toArray(Iterator<? extends E> iterator, Class<E> arrayClass) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } if (arrayClass == null) { throw new NullPointerException("Array class must not be null"); } - List list = toList(iterator, 100); - return list.toArray((Object[]) Array.newInstance(arrayClass, list.size())); + List<E> list = toList(iterator, 100); + return list.toArray((E[]) Array.newInstance(arrayClass, list.size())); } /** @@ -821,10 +827,10 @@ * @return a list of the iterator contents * @throws NullPointerException if iterator parameter is null */ - public static List toList(Iterator iterator) { + public static <E> List<E> toList(Iterator<? extends E> iterator) { return toList(iterator, 10); } - + /** * Gets a list based on an iterator. * <p> @@ -837,14 +843,14 @@ * @throws NullPointerException if iterator parameter is null * @throws IllegalArgumentException if the size is less than 1 */ - public static List toList(Iterator iterator, int estimatedSize) { + public static <E> List<E> toList(Iterator<? extends E> iterator, int estimatedSize) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } if (estimatedSize < 1) { throw new IllegalArgumentException("Estimated size must be greater than 0"); } - List list = new ArrayList(estimatedSize); + List<E> list = new ArrayList<E>(estimatedSize); while (iterator.hasNext()) { list.add(iterator.next()); } @@ -854,7 +860,7 @@ /** * Gets a suitable Iterator for the given object. * <p> - * This method can handles objects as follows + * This method can handle objects as follows * <ul> * <li>null - empty iterator * <li>Iterator - returned directly @@ -870,45 +876,44 @@ * @param obj the object to convert to an iterator * @return a suitable iterator, never null */ - public static Iterator getIterator(Object obj) { + @SuppressWarnings("unchecked") + public static Iterator<?> getIterator(Object obj) { if (obj == null) { return emptyIterator(); - - } else if (obj instanceof Iterator) { + } + if (obj instanceof Iterator) { return (Iterator) obj; - - } else if (obj instanceof Collection) { + } + if (obj instanceof Collection) { return ((Collection) obj).iterator(); - - } else if (obj instanceof Object[]) { + } + if (obj instanceof Object[]) { return new ObjectArrayIterator((Object[]) obj); - - } else if (obj instanceof Enumeration) { + } + if (obj instanceof Enumeration) { return new EnumerationIterator((Enumeration) obj); - - } else if (obj instanceof Map) { + } + if (obj instanceof Map) { return ((Map) obj).values().iterator(); - - } else if (obj instanceof Dictionary) { + } + if (obj instanceof Dictionary) { return new EnumerationIterator(((Dictionary) obj).elements()); - - } else if (obj != null && obj.getClass().isArray()) { + } + if (obj != null && obj.getClass().isArray()) { return new ArrayIterator(obj); - - } else { - try { - Method method = obj.getClass().getMethod("iterator", (Class[]) null); - if (Iterator.class.isAssignableFrom(method.getReturnType())) { - Iterator it = (Iterator) method.invoke(obj, (Object[]) null); - if (it != null) { - return it; - } + } + try { + Method method = obj.getClass().getMethod("iterator", (Class[]) null); + if (Iterator.class.isAssignableFrom(method.getReturnType())) { + Iterator it = (Iterator) method.invoke(obj, (Object[]) null); + if (it != null) { + return it; } - } catch (Exception ex) { - // ignore } - return singletonIterator(obj); + } catch (Exception ex) { + // ignore } + return singletonIterator(obj); } } Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/ListUtils.java URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/ListUtils.java?rev=738956&r1=738955&r2=738956&view=diff ============================================================================== --- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/ListUtils.java (original) +++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/ListUtils.java Thu Jan 29 18:48:37 2009 @@ -49,8 +49,8 @@ * This uses the {...@link Collections Collections} implementation * and is provided for completeness. */ - public static final List EMPTY_LIST = Collections.EMPTY_LIST; - + public static final List<Object> EMPTY_LIST = Collections.<Object>emptyList(); + /** * <code>ListUtils</code> should not normally be instantiated. */ @@ -67,18 +67,14 @@ * @return the intersection of those two lists * @throws NullPointerException if either list is null */ - public static List intersection(final List list1, final List list2) { - final List result = new ArrayList(); - final Iterator iterator = list2.iterator(); - - while (iterator.hasNext()) { - final Object o = iterator.next(); + public static <E> List<E> intersection(final List<? extends E> list1, final List<? extends E> list2) { + final List<E> result = new ArrayList<E>(); - if (list1.contains(o)) { - result.add(o); + for (E e : list2) { + if (list1.contains(e)) { + result.add(e); } } - return result; } @@ -97,14 +93,11 @@ * @return a new list containing the results * @throws NullPointerException if either list is null */ - public static List subtract(final List list1, final List list2) { - final ArrayList result = new ArrayList(list1); - final Iterator iterator = list2.iterator(); - - while (iterator.hasNext()) { - result.remove(iterator.next()); + public static <E> List<E> subtract(final List<E> list1, final List<? extends E> list2) { + final ArrayList<E> result = new ArrayList<E>(list1); + for (E e : list2) { + result.remove(e); } - return result; } @@ -117,7 +110,7 @@ * @return a new list containing the sum of those lists * @throws NullPointerException if either list is null */ - public static List sum(final List list1, final List list2) { + public static <E> List<E> sum(final List<? extends E> list1, final List<? extends E> list2) { return subtract(union(list1, list2), intersection(list1, list2)); } @@ -131,8 +124,8 @@ * @return a new list containing the union of those lists * @throws NullPointerException if either list is null */ - public static List union(final List list1, final List list2) { - final ArrayList result = new ArrayList(list1); + public static <E> List<E> union(final List<? extends E> list1, final List<? extends E> list2) { + final ArrayList<E> result = new ArrayList<E>(list1); result.addAll(list2); return result; } @@ -166,7 +159,7 @@ * @param list2 the second list, may be null * @return whether the lists are equal by value comparison */ - public static boolean isEqualList(final Collection list1, final Collection list2) { + public static boolean isEqualList(final Collection<?> list1, final Collection<?> list2) { if (list1 == list2) { return true; } @@ -174,8 +167,8 @@ return false; } - Iterator it1 = list1.iterator(); - Iterator it2 = list2.iterator(); + Iterator<?> it1 = list1.iterator(); + Iterator<?> it2 = list2.iterator(); Object obj1 = null; Object obj2 = null; @@ -306,7 +299,7 @@ * @return an unmodifiable list backed by the given list * @throws IllegalArgumentException if the list is null */ - public static <E> List unmodifiableList(List<E> list) { + public static <E> List<E> unmodifiableList(List<E> list) { return UnmodifiableList.decorate(list); } @@ -339,7 +332,7 @@ * @return a transformed list backed by the given list * @throws IllegalArgumentException if the List or Transformer is null */ - public static List transformedList(List list, Transformer transformer) { + public static <E> List<E> transformedList(List<E> list, Transformer<? super E, ? extends E> transformer) { return TransformedList.decorate(list, transformer); } @@ -372,7 +365,7 @@ * @return a lazy list backed by the given list * @throws IllegalArgumentException if the List or Factory is null */ - public static List lazyList(List list, Factory factory) { + public static <E> List<E> lazyList(List<E> list, Factory<? extends E> factory) { return LazyList.decorate(list, factory); } @@ -386,7 +379,7 @@ * @return a fixed-size list backed by that list * @throws IllegalArgumentException if the List is null */ - public static List fixedSizeList(List list) { + public static <E> List<E> fixedSizeList(List<E> list) { return FixedSizeList.decorate(list); }