http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/PredicateUtils.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/PredicateUtils.java b/src/java/org/apache/commons/collections/PredicateUtils.java index e59b1b6..c2b557b 100644 --- a/src/java/org/apache/commons/collections/PredicateUtils.java +++ b/src/java/org/apache/commons/collections/PredicateUtils.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. @@ -16,9 +16,6 @@ */ package org.apache.commons.collections; -import static org.apache.commons.collections.functors.AllPredicate.allPredicate; -import static org.apache.commons.collections.functors.TruePredicate.truePredicate; - import java.util.Collection; import org.apache.commons.collections.functors.AllPredicate; @@ -71,7 +68,7 @@ import org.apache.commons.collections.functors.UniquePredicate; * * @since Commons Collections 3.0 * @version $Revision$ $Date$ - * + * * @author Stephen Colebourne * @author Ola Berg */ @@ -87,23 +84,23 @@ public class PredicateUtils { // Simple predicates //----------------------------------------------------------------------------- - /** + /** * Gets a Predicate that always throws an exception. * This could be useful during testing as a placeholder. * * @see org.apache.commons.collections.functors.ExceptionPredicate - * + * * @return the predicate */ - public static Predicate exceptionPredicate() { - return ExceptionPredicate.INSTANCE; + public static <T> Predicate<T> exceptionPredicate() { + return ExceptionPredicate.<T>getInstance(); } /** * Gets a Predicate that always returns true. - * + * * @see org.apache.commons.collections.functors.TruePredicate - * + * * @return the predicate * @deprecated use {@link TruePredicate#truePredicate()} instead. */ @@ -114,20 +111,21 @@ public class PredicateUtils { /** * Gets a Predicate that always returns false. - * + * * @see org.apache.commons.collections.functors.FalsePredicate - * + * * @return the predicate + * @deprecated use {@link FalsePredicate#()} instead. */ - public static Predicate falsePredicate() { - return FalsePredicate.INSTANCE; + public static <T> Predicate<T> falsePredicate() { + return FalsePredicate.<T>getInstance(); } /** * Gets a Predicate that checks if the input object passed in is null. - * + * * @see org.apache.commons.collections.functors.NullPredicate - * + * * @return the predicate * @deprecated use {@link NullPredicate#nullPredicate()} instead */ @@ -138,21 +136,21 @@ public class PredicateUtils { /** * Gets a Predicate that checks if the input object passed in is not null. - * + * * @see org.apache.commons.collections.functors.NotNullPredicate - * + * * @return the predicate */ - public static Predicate notNullPredicate() { - return NotNullPredicate.INSTANCE; + public static <T> Predicate<T> notNullPredicate() { + return NotNullPredicate.<T>getInstance(); } /** * Creates a Predicate that checks if the input object is equal to the * specified object using equals(). - * + * * @see org.apache.commons.collections.functors.EqualPredicate - * + * * @param value the value to compare against * @return the predicate * @deprecated use {@link EqualPredicate#equalPredicate(Object)} instead. @@ -165,82 +163,82 @@ public class PredicateUtils { /** * Creates a Predicate that checks if the input object is equal to the * specified object by identity. - * + * * @see org.apache.commons.collections.functors.IdentityPredicate - * + * * @param value the value to compare against * @return the predicate */ - public static Predicate identityPredicate(Object value) { - return IdentityPredicate.getInstance(value); + public static <T> Predicate<T> identityPredicate(T value) { + return IdentityPredicate.<T>getInstance(value); } - + /** * Creates a Predicate that checks if the object passed in is of * a particular type, using instanceof. A <code>null</code> input * object will return <code>false</code>. - * + * * @see org.apache.commons.collections.functors.InstanceofPredicate - * + * * @param type the type to check for, may not be null * @return the predicate * @throws IllegalArgumentException if the class is null */ - public static Predicate instanceofPredicate(Class type) { + public static Predicate<Object> instanceofPredicate(Class<?> type) { return InstanceofPredicate.getInstance(type); } /** * Creates a Predicate that returns true the first time an object is - * encountered, and false if the same object is received + * encountered, and false if the same object is received * again. The comparison is by equals(). A <code>null</code> input object * is accepted and will return true the first time, and false subsequently * as well. - * + * * @see org.apache.commons.collections.functors.UniquePredicate - * + * * @return the predicate */ - public static Predicate uniquePredicate() { + public static <T> Predicate<T> uniquePredicate() { // must return new instance each time - return UniquePredicate.getInstance(); + return UniquePredicate.<T>getInstance(); } /** * Creates a Predicate that invokes a method on the input object. * The method must return either a boolean or a non-null Boolean, - * and have no parameters. If the input object is null, a + * and have no parameters. If the input object is null, a * PredicateException is thrown. * <p> * For example, <code>PredicateUtils.invokerPredicate("isEmpty");</code> - * will call the <code>isEmpty</code> method on the input object to + * will call the <code>isEmpty</code> method on the input object to * determine the predicate result. - * + * * @see org.apache.commons.collections.functors.InvokerTransformer * @see org.apache.commons.collections.functors.TransformerPredicate - * + * * @param methodName the method name to call on the input object, may not be null * @return the predicate * @throws IllegalArgumentException if the methodName is null. */ - public static Predicate invokerPredicate(String methodName){ + public static <T> Predicate<T> invokerPredicate(String methodName){ // reuse transformer as it has caching - this is lazy really, should have inner class here - return asPredicate(InvokerTransformer.getInstance(methodName)); + return asPredicate(InvokerTransformer.<Object, Boolean>getInstance(methodName)); } /** * Creates a Predicate that invokes a method on the input object. * The method must return either a boolean or a non-null Boolean, - * and have no parameters. If the input object is null, a + * and have no parameters. If the input object is null, a * PredicateException is thrown. * <p> * For example, <code>PredicateUtils.invokerPredicate("isEmpty");</code> - * will call the <code>isEmpty</code> method on the input object to + * will call the <code>isEmpty</code> method on the input object to * determine the predicate result. - * + * * @see org.apache.commons.collections.functors.InvokerTransformer * @see org.apache.commons.collections.functors.TransformerPredicate - * + * * @param methodName the method name to call on the input object, may not be null * @param paramTypes the parameter types * @param args the arguments @@ -248,9 +246,9 @@ public class PredicateUtils { * @throws IllegalArgumentException if the method name is null * @throws IllegalArgumentException if the paramTypes and args don't match */ - public static Predicate invokerPredicate(String methodName, Class[] paramTypes, Object[] args){ + public static <T> Predicate<T> invokerPredicate(String methodName, Class<?>[] paramTypes, Object[] args){ // reuse transformer as it has caching - this is lazy really, should have inner class here - return asPredicate(InvokerTransformer.getInstance(methodName, paramTypes, args)); + return asPredicate(InvokerTransformer.<Object, Boolean>getInstance(methodName, paramTypes, args)); } // Boolean combinations @@ -259,25 +257,25 @@ public class PredicateUtils { /** * Create a new Predicate that returns true only if both of the specified * predicates are true. - * + * * @see org.apache.commons.collections.functors.AndPredicate - * + * * @param predicate1 the first predicate, may not be null * @param predicate2 the second predicate, may not be null * @return the <code>and</code> predicate * @throws IllegalArgumentException if either predicate is null */ - public static Predicate andPredicate(Predicate predicate1, Predicate predicate2) { - return AndPredicate.getInstance(predicate1, predicate2); + public static <T> Predicate<T> andPredicate(Predicate<? super T> predicate1, Predicate<? super T> predicate2) { + return AndPredicate.<T>getInstance(predicate1, predicate2); } /** * Create a new Predicate that returns true only if all of the specified * predicates are true. * If the array of predicates is empty, then this predicate returns true. - * + * * @see org.apache.commons.collections.functors.AllPredicate - * + * * @param predicates an array of predicates to check, may not be null * @return the <code>all</code> predicate * @throws IllegalArgumentException if the predicates array is null @@ -293,48 +291,46 @@ public class PredicateUtils { * Create a new Predicate that returns true only if all of the specified * predicates are true. The predicates are checked in iterator order. * If the collection of predicates is empty, then this predicate returns true. - * + * * @see org.apache.commons.collections.functors.AllPredicate - * + * * @param predicates a collection of predicates to check, may not be null * @return the <code>all</code> predicate * @throws IllegalArgumentException if the predicates collection is null * @throws IllegalArgumentException if any predicate in the collection is null - * @deprecated use {@link AllPredicate#allPredicate(Collection))} instead. */ - @Deprecated - public static <T> Predicate<T> allPredicate(Collection<Predicate<? super T>> predicates) { + public static <T> Predicate<T> allPredicate(Collection<? extends Predicate<T>> predicates) { return AllPredicate.allPredicate(predicates); } /** * Create a new Predicate that returns true if either of the specified * predicates are true. - * + * * @see org.apache.commons.collections.functors.OrPredicate - * + * * @param predicate1 the first predicate, may not be null * @param predicate2 the second predicate, may not be null * @return the <code>or</code> predicate * @throws IllegalArgumentException if either predicate is null */ - public static Predicate orPredicate(Predicate predicate1, Predicate predicate2) { - return OrPredicate.getInstance(predicate1, predicate2); + public static <T> Predicate<T> orPredicate(Predicate<? super T> predicate1, Predicate<? super T> predicate2) { + return OrPredicate.<T>getInstance(predicate1, predicate2); } /** * Create a new Predicate that returns true if any of the specified * predicates are true. * If the array of predicates is empty, then this predicate returns false. - * + * * @see org.apache.commons.collections.functors.AnyPredicate - * + * * @param predicates an array of predicates to check, may not be null * @return the <code>any</code> predicate * @throws IllegalArgumentException if the predicates array is null * @throws IllegalArgumentException if any predicate in the array is null */ - public static Predicate anyPredicate(Predicate[] predicates) { + public static <T> Predicate<T> anyPredicate(Predicate<? super T>[] predicates) { return AnyPredicate.getInstance(predicates); } @@ -342,30 +338,31 @@ public class PredicateUtils { * Create a new Predicate that returns true if any of the specified * predicates are true. The predicates are checked in iterator order. * If the collection of predicates is empty, then this predicate returns false. - * + * * @see org.apache.commons.collections.functors.AnyPredicate - * + * * @param predicates a collection of predicates to check, may not be null * @return the <code>any</code> predicate * @throws IllegalArgumentException if the predicates collection is null * @throws IllegalArgumentException if any predicate in the collection is null */ - public static Predicate anyPredicate(Collection predicates) { + public static <T> Predicate<T> anyPredicate(Collection<? extends Predicate<T>> predicates) { return AnyPredicate.getInstance(predicates); } /** * Create a new Predicate that returns true if one, but not both, of the - * specified predicates are true. - * + * specified predicates are true. XOR + * * @see org.apache.commons.collections.functors.OnePredicate - * + * * @param predicate1 the first predicate, may not be null * @param predicate2 the second predicate, may not be null * @return the <code>either</code> predicate * @throws IllegalArgumentException if either predicate is null */ - public static Predicate eitherPredicate(Predicate predicate1, Predicate predicate2) { + @SuppressWarnings("unchecked") + public static <T> Predicate<T> eitherPredicate(Predicate<? super T> predicate1, Predicate<? super T> predicate2) { return onePredicate(new Predicate[] { predicate1, predicate2 }); } @@ -373,15 +370,15 @@ public class PredicateUtils { * Create a new Predicate that returns true if only one of the specified * predicates are true. * If the array of predicates is empty, then this predicate returns false. - * + * * @see org.apache.commons.collections.functors.OnePredicate - * + * * @param predicates an array of predicates to check, may not be null * @return the <code>one</code> predicate * @throws IllegalArgumentException if the predicates array is null * @throws IllegalArgumentException if any predicate in the array is null */ - public static Predicate onePredicate(Predicate[] predicates) { + public static <T> Predicate<T> onePredicate(Predicate<? super T>[] predicates) { return OnePredicate.getInstance(predicates); } @@ -389,30 +386,31 @@ public class PredicateUtils { * Create a new Predicate that returns true if only one of the specified * predicates are true. The predicates are checked in iterator order. * If the collection of predicates is empty, then this predicate returns false. - * + * * @see org.apache.commons.collections.functors.OnePredicate - * + * * @param predicates a collection of predicates to check, may not be null * @return the <code>one</code> predicate * @throws IllegalArgumentException if the predicates collection is null * @throws IllegalArgumentException if any predicate in the collection is null */ - public static Predicate onePredicate(Collection predicates) { + public static <T> Predicate<T> onePredicate(Collection<Predicate<T>> predicates) { return OnePredicate.getInstance(predicates); } /** - * Create a new Predicate that returns true if neither of the specified + * Create a new Predicate that returns true if neither of the specified * predicates are true. - * + * * @see org.apache.commons.collections.functors.NonePredicate - * + * * @param predicate1 the first predicate, may not be null * @param predicate2 the second predicate, may not be null * @return the <code>neither</code> predicate * @throws IllegalArgumentException if either predicate is null */ - public static Predicate neitherPredicate(Predicate predicate1, Predicate predicate2) { + @SuppressWarnings("unchecked") + public static <T> Predicate<T> neitherPredicate(Predicate<? super T> predicate1, Predicate<? super T> predicate2) { return nonePredicate(new Predicate[] { predicate1, predicate2 }); } @@ -420,15 +418,15 @@ public class PredicateUtils { * Create a new Predicate that returns true if none of the specified * predicates are true. * If the array of predicates is empty, then this predicate returns true. - * + * * @see org.apache.commons.collections.functors.NonePredicate - * + * * @param predicates an array of predicates to check, may not be null * @return the <code>none</code> predicate * @throws IllegalArgumentException if the predicates array is null * @throws IllegalArgumentException if any predicate in the array is null */ - public static Predicate nonePredicate(Predicate[] predicates) { + public static <T> Predicate<T> nonePredicate(Predicate<? super T>[] predicates) { return NonePredicate.getInstance(predicates); } @@ -436,29 +434,29 @@ public class PredicateUtils { * Create a new Predicate that returns true if none of the specified * predicates are true. The predicates are checked in iterator order. * If the collection of predicates is empty, then this predicate returns true. - * + * * @see org.apache.commons.collections.functors.NonePredicate - * + * * @param predicates a collection of predicates to check, may not be null * @return the <code>none</code> predicate * @throws IllegalArgumentException if the predicates collection is null * @throws IllegalArgumentException if any predicate in the collection is null */ - public static Predicate nonePredicate(Collection predicates) { + public static <T> Predicate<T> nonePredicate(Collection<? extends Predicate<T>> predicates) { return NonePredicate.getInstance(predicates); } /** * Create a new Predicate that returns true if the specified predicate * returns false and vice versa. - * + * * @see org.apache.commons.collections.functors.NotPredicate - * + * * @param predicate the predicate to not * @return the <code>not</code> predicate * @throws IllegalArgumentException if the predicate is null */ - public static Predicate notPredicate(Predicate predicate) { + public static <T> Predicate<T> notPredicate(Predicate<? super T> predicate) { return NotPredicate.getInstance(predicate); } @@ -469,14 +467,14 @@ public class PredicateUtils { * Create a new Predicate that wraps a Transformer. The Transformer must * return either Boolean.TRUE or Boolean.FALSE otherwise a PredicateException * will be thrown. - * + * * @see org.apache.commons.collections.functors.TransformerPredicate - * + * * @param transformer the transformer to wrap, may not be null * @return the transformer wrapping predicate * @throws IllegalArgumentException if the transformer is null */ - public static Predicate asPredicate(Transformer transformer) { + public static <T> Predicate<T> asPredicate(Transformer<? super T, Boolean> transformer) { return TransformerPredicate.getInstance(transformer); } @@ -484,17 +482,17 @@ public class PredicateUtils { //----------------------------------------------------------------------------- /** - * Gets a Predicate that throws an exception if the input object is null, - * otherwise it calls the specified Predicate. This allows null handling + * Gets a Predicate that throws an exception if the input object is null, + * otherwise it calls the specified Predicate. This allows null handling * behaviour to be added to Predicates that don't support nulls. - * + * * @see org.apache.commons.collections.functors.NullIsExceptionPredicate - * + * * @param predicate the predicate to wrap, may not be null * @return the predicate * @throws IllegalArgumentException if the predicate is null. */ - public static Predicate nullIsExceptionPredicate(Predicate predicate){ + public static <T> Predicate<T> nullIsExceptionPredicate(Predicate<? super T> predicate){ return NullIsExceptionPredicate.getInstance(predicate); } @@ -502,14 +500,14 @@ public class PredicateUtils { * Gets a Predicate that returns false if the input object is null, otherwise * it calls the specified Predicate. This allows null handling behaviour to * be added to Predicates that don't support nulls. - * + * * @see org.apache.commons.collections.functors.NullIsFalsePredicate - * + * * @param predicate the predicate to wrap, may not be null * @return the predicate * @throws IllegalArgumentException if the predicate is null. */ - public static Predicate nullIsFalsePredicate(Predicate predicate){ + public static <T> Predicate<T> nullIsFalsePredicate(Predicate<? super T> predicate){ return NullIsFalsePredicate.getInstance(predicate); } @@ -517,14 +515,14 @@ public class PredicateUtils { * Gets a Predicate that returns true if the input object is null, otherwise * it calls the specified Predicate. This allows null handling behaviour to * be added to Predicates that don't support nulls. - * + * * @see org.apache.commons.collections.functors.NullIsTruePredicate - * + * * @param predicate the predicate to wrap, may not be null * @return the predicate * @throws IllegalArgumentException if the predicate is null. */ - public static Predicate nullIsTruePredicate(Predicate predicate){ + public static <T> Predicate<T> nullIsTruePredicate(Predicate<? super T> predicate){ return NullIsTruePredicate.getInstance(predicate); } @@ -533,17 +531,18 @@ public class PredicateUtils { /** * Creates a predicate that transforms the input object before passing it * to the predicate. - * + * * @see org.apache.commons.collections.functors.TransformedPredicate - * + * * @param transformer the transformer to call first * @param predicate the predicate to call with the result of the transform * @return the predicate * @throws IllegalArgumentException if the transformer or the predicate is null * @since Commons Collections 3.1 */ - public static Predicate transformedPredicate(Transformer transformer, Predicate predicate) { - return TransformedPredicate.getInstance(transformer, predicate); + public static <T> Predicate<T> transformedPredicate( + Transformer<? super T, ? extends T> transformer, Predicate<? super T> predicate) { + return TransformedPredicate.<T>getInstance(transformer, predicate); } }
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/SetUtils.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/SetUtils.java b/src/java/org/apache/commons/collections/SetUtils.java index c45ffff..683be0e 100644 --- a/src/java/org/apache/commons/collections/SetUtils.java +++ b/src/java/org/apache/commons/collections/SetUtils.java @@ -18,7 +18,6 @@ package org.apache.commons.collections; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; @@ -52,12 +51,32 @@ public class SetUtils { * This uses the {@link Collections} implementation * and is provided for completeness. */ - public static final Set EMPTY_SET = Collections.EMPTY_SET; + public static final Set<?> EMPTY_SET = Collections.EMPTY_SET; + + /** + * Get a typed empty unmodifiable Set. + * @param <E> + * @return Set<E> + */ + public static <E> Set<E> emptySet() { + return Collections.<E>emptySet(); + } + /** * An empty unmodifiable sorted set. * This is not provided in the JDK. */ - public static final SortedSet EMPTY_SORTED_SET = UnmodifiableSortedSet.decorate(new TreeSet()); + public static final SortedSet<?> EMPTY_SORTED_SET = UnmodifiableSortedSet.decorate(new TreeSet<Object>()); + + /** + * Get a typed empty unmodifiable sorted set. + * @param <E> + * @return SortedSet<E> + */ + @SuppressWarnings("unchecked") + public static <E> SortedSet<E> emptySortedSet() { + return (SortedSet<E>) EMPTY_SORTED_SET; + } /** * <code>SetUtils</code> should not normally be instantiated. @@ -94,7 +113,7 @@ public class SetUtils { * @param set2 the second set, may be null * @return whether the sets are equal by value comparison */ - public static boolean isEqualSet(final Collection set1, final Collection set2) { + public static boolean isEqualSet(final Collection<?> set1, final Collection<?> set2) { if (set1 == set2) { return true; } @@ -167,7 +186,7 @@ public class SetUtils { * @return an unmodifiable set backed by the given set * @throws IllegalArgumentException if the set is null */ - public static Set unmodifiableSet(Set set) { + public static <E> Set<E> unmodifiableSet(Set<E> set) { return UnmodifiableSet.decorate(set); } @@ -200,7 +219,7 @@ public class SetUtils { * @return a transformed set backed by the given set * @throws IllegalArgumentException if the Set or Transformer is null */ - public static Set transformedSet(Set set, Transformer transformer) { + public static <E> Set<E> transformedSet(Set<E> set, Transformer<? super E, ? extends E> transformer) { return TransformedSet.decorate(set, transformer); } @@ -215,7 +234,7 @@ public class SetUtils { * @return an ordered set backed by the given set * @throws IllegalArgumentException if the Set is null */ - public static Set orderedSet(Set set) { + public static <E> Set<E> orderedSet(Set<E> set) { return ListOrderedSet.decorate(set); } @@ -288,7 +307,7 @@ public class SetUtils { * @return a transformed set backed by the given set * @throws IllegalArgumentException if the Set or Transformer is null */ - public static SortedSet transformedSortedSet(SortedSet set, Transformer transformer) { + public static <E> SortedSet<E> transformedSortedSet(SortedSet<E> set, Transformer<? super E, ? extends E> transformer) { return TransformedSortedSet.decorate(set, transformer); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/SortedBidiMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/SortedBidiMap.java b/src/java/org/apache/commons/collections/SortedBidiMap.java index efc70b9..b56d7cf 100644 --- a/src/java/org/apache/commons/collections/SortedBidiMap.java +++ b/src/java/org/apache/commons/collections/SortedBidiMap.java @@ -16,6 +16,7 @@ */ package org.apache.commons.collections; +import java.util.Comparator; import java.util.SortedMap; /** @@ -49,23 +50,11 @@ public interface SortedBidiMap<K, V> extends OrderedBidiMap<K, V>, SortedMap<K, * * @return an inverted bidirectional map */ - public BidiMap<V, K> inverseBidiMap(); + public SortedBidiMap<V, K> inverseBidiMap(); /** - * Gets a view of this map where the keys and values are reversed. - * <p> - * Changes to one map will be visible in the other and vice versa. - * This enables both directions of the map to be accessed as a <code>SortedMap</code>. - * <p> - * Implementations should seek to avoid creating a new object every time this - * method is called. See <code>AbstractMap.values()</code> etc. Calling this - * method on the inverse map should return the original. - * <p> - * The inverse map returned by <code>inverseBidiMap()</code> should be the - * same object as returned by this method. - * - * @return an inverted bidirectional map + * Get the comparator used for the values in the value-to-key map aspect. + * @return Comparator<? super V> */ - public SortedBidiMap<V, K> inverseSortedBidiMap(); - + public Comparator<? super V> valueComparator(); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/TransformerUtils.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/TransformerUtils.java b/src/java/org/apache/commons/collections/TransformerUtils.java index fb96fa3..e602fbf 100644 --- a/src/java/org/apache/commons/collections/TransformerUtils.java +++ b/src/java/org/apache/commons/collections/TransformerUtils.java @@ -17,7 +17,6 @@ package org.apache.commons.collections; import java.util.Collection; -import java.util.Iterator; import java.util.Map; import org.apache.commons.collections.functors.ChainedTransformer; @@ -80,8 +79,8 @@ public class TransformerUtils { * * @return the transformer */ - public static Transformer exceptionTransformer() { - return ExceptionTransformer.INSTANCE; + public static <I, O> Transformer<I, O> exceptionTransformer() { + return ExceptionTransformer.<I, O>getInstance(); } /** @@ -91,8 +90,8 @@ public class TransformerUtils { * * @return the transformer */ - public static Transformer nullTransformer() { - return ConstantTransformer.NULL_INSTANCE; + public static <I, O> Transformer<I, O> nullTransformer() { + return ConstantTransformer.<I, O>getNullInstance(); } /** @@ -104,8 +103,8 @@ public class TransformerUtils { * * @return the transformer */ - public static Transformer nopTransformer() { - return NOPTransformer.INSTANCE; + public static <T> Transformer<T, T> nopTransformer() { + return NOPTransformer.<T>getInstance(); } /** @@ -122,8 +121,8 @@ public class TransformerUtils { * * @return the transformer */ - public static Transformer cloneTransformer() { - return CloneTransformer.INSTANCE; + public static <T> Transformer<T, T> cloneTransformer() { + return CloneTransformer.<T>getInstance(); } /** @@ -135,7 +134,7 @@ public class TransformerUtils { * @param constantToReturn the constant object to return each time in the transformer * @return the transformer. */ - public static Transformer constantTransformer(Object constantToReturn) { + public static <I, O> Transformer<I, O> constantTransformer(O constantToReturn) { return ConstantTransformer.getInstance(constantToReturn); } @@ -149,7 +148,7 @@ public class TransformerUtils { * @return the transformer * @throws IllegalArgumentException if the closure is null */ - public static Transformer asTransformer(Closure closure) { + public static <T> Transformer<T, T> asTransformer(Closure<? super T> closure) { return ClosureTransformer.getInstance(closure); } @@ -163,7 +162,7 @@ public class TransformerUtils { * @return the transformer * @throws IllegalArgumentException if the predicate is null */ - public static Transformer asTransformer(Predicate predicate) { + public static <T> Transformer<T, Boolean> asTransformer(Predicate<? super T> predicate) { return PredicateTransformer.getInstance(predicate); } @@ -177,7 +176,7 @@ public class TransformerUtils { * @return the transformer * @throws IllegalArgumentException if the factory is null */ - public static Transformer asTransformer(Factory factory) { + public static <I, O> Transformer<I, O> asTransformer(Factory<? extends O> factory) { return FactoryTransformer.getInstance(factory); } @@ -192,8 +191,10 @@ public class TransformerUtils { * @return the transformer * @throws IllegalArgumentException if either transformer is null */ - public static Transformer chainedTransformer(Transformer transformer1, Transformer transformer2) { - return ChainedTransformer.getInstance(transformer1, transformer2); + public static <T> Transformer<T, T> chainedTransformer( + Transformer<? super T, ? extends T> transformer1, + Transformer<? super T, ? extends T> transformer2) { + return ChainedTransformer.<T>getInstance(transformer1, transformer2); } /** @@ -207,7 +208,7 @@ public class TransformerUtils { * @throws IllegalArgumentException if the transformers array is null * @throws IllegalArgumentException if any transformer in the array is null */ - public static Transformer chainedTransformer(Transformer[] transformers) { + public static <T> Transformer<T, T> chainedTransformer(Transformer<? super T, ? extends T>[] transformers) { return ChainedTransformer.getInstance(transformers); } @@ -223,7 +224,8 @@ public class TransformerUtils { * @throws IllegalArgumentException if the transformers collection is null * @throws IllegalArgumentException if any transformer in the collection is null */ - public static Transformer chainedTransformer(Collection transformers) { + public static <T> Transformer<T, T> chainedTransformer( + Collection<? extends Transformer<T, T>> transformers) { return ChainedTransformer.getInstance(transformers); } @@ -240,8 +242,12 @@ public class TransformerUtils { * @throws IllegalArgumentException if the predicate is null * @throws IllegalArgumentException if either transformer is null */ - public static Transformer switchTransformer(Predicate predicate, Transformer trueTransformer, Transformer falseTransformer) { - return SwitchTransformer.getInstance(new Predicate[] { predicate }, new Transformer[] { trueTransformer }, falseTransformer); + @SuppressWarnings("unchecked") + public static <I, O> Transformer<I, O> switchTransformer(Predicate<? super I> predicate, + Transformer<? super I, ? extends O> trueTransformer, + Transformer<? super I, ? extends O> falseTransformer) { + return SwitchTransformer.getInstance(new Predicate[] { predicate }, + new Transformer[] { trueTransformer }, falseTransformer); } /** @@ -260,8 +266,9 @@ public class TransformerUtils { * @throws IllegalArgumentException if any element in the arrays is null * @throws IllegalArgumentException if the arrays are different sizes */ - public static Transformer switchTransformer(Predicate[] predicates, Transformer[] transformers) { - return SwitchTransformer.getInstance(predicates, transformers, null); + public static <I, O> Transformer<I, O> switchTransformer(Predicate<? super I>[] predicates, + Transformer<? super I, ? extends O>[] transformers) { + return SwitchTransformer.<I, O>getInstance(predicates, transformers, null); } /** @@ -282,8 +289,10 @@ public class TransformerUtils { * @throws IllegalArgumentException if any element in the arrays is null * @throws IllegalArgumentException if the arrays are different sizes */ - public static Transformer switchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) { - return SwitchTransformer.getInstance(predicates, transformers, defaultTransformer); + public static <I, O> Transformer<I, O> switchTransformer(Predicate<? super I>[] predicates, + Transformer<? super I, ? extends O>[] transformers, + Transformer<? super I, ? extends O> defaultTransformer) { + return SwitchTransformer.<I, O>getInstance(predicates, transformers, defaultTransformer); } /** @@ -307,8 +316,9 @@ public class TransformerUtils { * @throws IllegalArgumentException if any transformer in the map is null * @throws ClassCastException if the map elements are of the wrong type */ - public static Transformer switchTransformer(Map predicatesAndTransformers) { - return SwitchTransformer.getInstance(predicatesAndTransformers); + public static <I, O> Transformer<I, O> switchTransformer( + Map<Predicate<I>, Transformer<I, O>> predicatesAndTransformers) { + return SwitchTransformer.<I, O>getInstance(predicatesAndTransformers); } /** @@ -328,24 +338,23 @@ public class TransformerUtils { * @throws IllegalArgumentException if the map is empty * @throws IllegalArgumentException if any transformer in the map is null */ - public static Transformer switchMapTransformer(Map objectsAndTransformers) { - Transformer[] trs = null; - Predicate[] preds = null; + @SuppressWarnings("unchecked") + public static <I, O> Transformer<I, O> switchMapTransformer(Map<I, Transformer<I, O>> objectsAndTransformers) { + Transformer<? super I, ? extends O>[] trs = null; + Predicate<I>[] preds = null; if (objectsAndTransformers == null) { throw new IllegalArgumentException("The object and transformer map must not be null"); } - Transformer def = (Transformer) objectsAndTransformers.remove(null); + Transformer<? super I, ? extends O> def = objectsAndTransformers.remove(null); int size = objectsAndTransformers.size(); trs = new Transformer[size]; preds = new Predicate[size]; int i = 0; - for (Iterator it = objectsAndTransformers.entrySet().iterator(); it.hasNext();) { - Map.Entry entry = (Map.Entry) it.next(); - preds[i] = EqualPredicate.equalPredicate(entry.getKey()); - trs[i] = (Transformer) entry.getValue(); - i++; + for (Map.Entry<I, Transformer<I, O>> entry : objectsAndTransformers.entrySet()) { + preds[i] = EqualPredicate.<I>equalPredicate(entry.getKey()); + trs[i++] = entry.getValue(); } - return switchTransformer(preds, trs, def); + return TransformerUtils.<I, O>switchTransformer(preds, trs, def); } /** @@ -355,8 +364,8 @@ public class TransformerUtils { * * @return the transformer */ - public static Transformer instantiateTransformer() { - return InstantiateTransformer.NO_ARG_INSTANCE; + public static <T> Transformer<Class<? extends T>, T> instantiateTransformer() { + return InstantiateTransformer.<T>getInstance(); } /** @@ -371,8 +380,9 @@ public class TransformerUtils { * @return the transformer * @throws IllegalArgumentException if the paramTypes and args don't match */ - public static Transformer instantiateTransformer(Class[] paramTypes, Object[] args) { - return InstantiateTransformer.getInstance(paramTypes, args); + public static <T> Transformer<Class<? extends T>, T> instantiateTransformer( + Class<?>[] paramTypes, Object[] args) { + return InstantiateTransformer.<T>getInstance(paramTypes, args); } /** @@ -385,7 +395,7 @@ public class TransformerUtils { * @return the transformer * @throws IllegalArgumentException if the map is null */ - public static Transformer mapTransformer(Map map) { + public static <I, O> Transformer<I, O> mapTransformer(Map<? super I, ? extends O> map) { return MapTransformer.getInstance(map); } @@ -404,8 +414,8 @@ public class TransformerUtils { * @return the transformer * @throws IllegalArgumentException if the methodName is null. */ - public static Transformer invokerTransformer(String methodName){ - return InvokerTransformer.getInstance(methodName, null, null); + public static <I, O> Transformer<I, O> invokerTransformer(String methodName){ + return InvokerTransformer.<I, O>getInstance(methodName, null, null); } /** @@ -422,8 +432,8 @@ public class TransformerUtils { * @throws IllegalArgumentException if the method name is null * @throws IllegalArgumentException if the paramTypes and args don't match */ - public static Transformer invokerTransformer(String methodName, Class[] paramTypes, Object[] args){ - return InvokerTransformer.getInstance(methodName, paramTypes, args); + public static <I, O> Transformer<I, O> invokerTransformer(String methodName, Class<?>[] paramTypes, Object[] args){ + return InvokerTransformer.<I, O>getInstance(methodName, paramTypes, args); } /** @@ -435,8 +445,8 @@ public class TransformerUtils { * * @return the transformer */ - public static Transformer stringValueTransformer() { - return StringValueTransformer.INSTANCE; + public static <T> Transformer<T, String> stringValueTransformer() { + return StringValueTransformer.<T>getInstance(); } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/bag/AbstractBagDecorator.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/bag/AbstractBagDecorator.java b/src/java/org/apache/commons/collections/bag/AbstractBagDecorator.java index 95422cc..ae0f55d 100644 --- a/src/java/org/apache/commons/collections/bag/AbstractBagDecorator.java +++ b/src/java/org/apache/commons/collections/bag/AbstractBagDecorator.java @@ -34,6 +34,9 @@ import org.apache.commons.collections.collection.AbstractCollectionDecorator; public abstract class AbstractBagDecorator<E> extends AbstractCollectionDecorator<E> implements Bag<E> { + /** Serialization version */ + private static final long serialVersionUID = -3768146017343785417L; + /** * Constructor only used in deserialization, do not use otherwise. * @since Commons Collections 3.1 http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/bag/AbstractMapBag.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/bag/AbstractMapBag.java b/src/java/org/apache/commons/collections/bag/AbstractMapBag.java index 94569cb..f61c9cc 100644 --- a/src/java/org/apache/commons/collections/bag/AbstractMapBag.java +++ b/src/java/org/apache/commons/collections/bag/AbstractMapBag.java @@ -33,12 +33,13 @@ import org.apache.commons.collections.set.UnmodifiableSet; * Abstract implementation of the {@link Bag} interface to simplify the creation * of subclass implementations. * <p> - * Subclasses specify a Map implementation to use as the internal storage. - * The map will be used to map bag elements to a number; the number represents - * the number of occurrences of that element in the bag. - * + * Subclasses specify a Map implementation to use as the internal storage. The + * map will be used to map bag elements to a number; the number represents the + * number of occurrences of that element in the bag. + * * @since Commons Collections 3.0 (previously DefaultMapBag v2.0) - * @version $Revision$ $Date$ + * @version $Revision$ $Date: 2006-10-27 19:52:37 -0500 (Fri, 27 Oct + * 2006) $ * * @author Chuck Burdick * @author Michael A. Smith @@ -46,16 +47,16 @@ import org.apache.commons.collections.set.UnmodifiableSet; * @author Janek Bogucki * @author Steve Clark */ -public abstract class AbstractMapBag implements Bag { - +public abstract class AbstractMapBag<E> implements Bag<E> { + /** The map to use to store the data */ - private transient Map map; + private transient Map<E, MutableInteger> map; /** The current total size of the bag */ private int size; /** The modification count for fail fast iterators */ private transient int modCount; /** The modification count for fail fast iterators */ - private transient Set uniqueSet; + private transient Set<E> uniqueSet; /** * Constructor needed for subclass serialisation. @@ -66,30 +67,30 @@ public abstract class AbstractMapBag implements Bag { } /** - * Constructor that assigns the specified Map as the backing store. - * The map must be empty and non-null. + * Constructor that assigns the specified Map as the backing store. The map + * must be empty and non-null. * - * @param map the map to assign + * @param map the map to assign */ - protected AbstractMapBag(Map map) { + protected AbstractMapBag(Map<E, MutableInteger> map) { super(); this.map = map; } /** - * Utility method for implementations to access the map that backs - * this bag. Not intended for interactive use outside of subclasses. + * Utility method for implementations to access the map that backs this bag. + * Not intended for interactive use outside of subclasses. * * @return the map being used by the Bag */ - protected Map getMap() { + protected Map<E, MutableInteger> getMap() { return map; } //----------------------------------------------------------------------- /** * Returns the number of elements in this bag. - * + * * @return current size of the bag */ public int size() { @@ -98,7 +99,7 @@ public abstract class AbstractMapBag implements Bag { /** * Returns true if the underlying map is empty. - * + * * @return true if bag is empty */ public boolean isEmpty() { @@ -106,14 +107,14 @@ public abstract class AbstractMapBag implements Bag { } /** - * Returns the number of occurrence of the given element in this bag - * by looking up its count in the underlying map. - * - * @param object the object to search for + * Returns the number of occurrence of the given element in this bag by + * looking up its count in the underlying map. + * + * @param object the object to search for * @return the number of occurrences of the object, zero if not found */ public int getCount(Object object) { - MutableInteger count = (MutableInteger) map.get(object); + MutableInteger count = map.get(object); if (count != null) { return count.value; } @@ -124,8 +125,8 @@ public abstract class AbstractMapBag implements Bag { /** * Determines if the bag contains the given element by checking if the * underlying map contains the element as a key. - * - * @param object the object to search for + * + * @param object the object to search for * @return true if the bag contains the given element */ public boolean contains(Object object) { @@ -135,26 +136,27 @@ public abstract class AbstractMapBag implements Bag { /** * Determines if the bag contains the given elements. * - * @param coll the collection to check against + * @param coll the collection to check against * @return <code>true</code> if the Bag contains all the collection */ - public boolean containsAll(Collection coll) { + @SuppressWarnings("unchecked") + public boolean containsAll(Collection<?> coll) { if (coll instanceof Bag) { - return containsAll((Bag) coll); + return containsAll((Bag<?>) coll); } - return containsAll(new HashBag(coll)); + return containsAll(new HashBag<Object>((Collection<Object>) coll)); } /** - * Returns <code>true</code> if the bag contains all elements in - * the given collection, respecting cardinality. + * Returns <code>true</code> if the bag contains all elements in the given + * collection, respecting cardinality. * - * @param other the bag to check against + * @param other the bag to check against * @return <code>true</code> if the Bag contains all the collection */ - boolean containsAll(Bag other) { + boolean containsAll(Bag<?> other) { boolean result = true; - Iterator it = other.uniqueSet().iterator(); + Iterator<?> it = other.uniqueSet().iterator(); while (it.hasNext()) { Object current = it.next(); boolean contains = getCount(current) >= other.getCount(current); @@ -165,22 +167,22 @@ public abstract class AbstractMapBag implements Bag { //----------------------------------------------------------------------- /** - * Gets an iterator over the bag elements. - * Elements present in the Bag more than once will be returned repeatedly. + * Gets an iterator over the bag elements. Elements present in the Bag more + * than once will be returned repeatedly. * * @return the iterator */ - public Iterator iterator() { - return new BagIterator(this); + public Iterator<E> iterator() { + return new BagIterator<E>(this); } /** * Inner class iterator for the Bag. */ - static class BagIterator implements Iterator { - private AbstractMapBag parent; - private Iterator entryIterator; - private Map.Entry current; + static class BagIterator<E> implements Iterator<E> { + private AbstractMapBag<E> parent; + private Iterator<Map.Entry<E, MutableInteger>> entryIterator; + private Map.Entry<E, MutableInteger> current; private int itemCount; private final int mods; private boolean canRemove; @@ -188,9 +190,9 @@ public abstract class AbstractMapBag implements Bag { /** * Constructor. * - * @param parent the parent bag + * @param parent the parent bag */ - public BagIterator(AbstractMapBag parent) { + public BagIterator(AbstractMapBag<E> parent) { this.parent = parent; this.entryIterator = parent.map.entrySet().iterator(); this.current = null; @@ -202,13 +204,13 @@ public abstract class AbstractMapBag implements Bag { return (itemCount > 0 || entryIterator.hasNext()); } - public Object next() { + public E next() { if (parent.modCount != mods) { throw new ConcurrentModificationException(); } if (itemCount == 0) { - current = (Map.Entry) entryIterator.next(); - itemCount = ((MutableInteger) current.getValue()).value; + current = (Map.Entry<E, MutableInteger>) entryIterator.next(); + itemCount = current.getValue().value; } canRemove = true; itemCount--; @@ -222,7 +224,7 @@ public abstract class AbstractMapBag implements Bag { if (canRemove == false) { throw new IllegalStateException(); } - MutableInteger mut = (MutableInteger) current.getValue(); + MutableInteger mut = current.getValue(); if (mut.value > 1) { mut.value--; } else { @@ -235,48 +237,49 @@ public abstract class AbstractMapBag implements Bag { //----------------------------------------------------------------------- /** - * Adds a new element to the bag, incrementing its count in the underlying map. - * - * @param object the object to add - * @return <code>true</code> if the object was not already in the <code>uniqueSet</code> + * Adds a new element to the bag, incrementing its count in the underlying + * map. + * + * @param object the object to add + * @return <code>true</code> if the object was not already in the + * <code>uniqueSet</code> */ - public boolean add(Object object) { + public boolean add(E object) { return add(object, 1); } /** * Adds a new element to the bag, incrementing its count in the map. - * - * @param object the object to search for - * @param nCopies the number of copies to add - * @return <code>true</code> if the object was not already in the <code>uniqueSet</code> + * + * @param object the object to search for + * @param nCopies the number of copies to add + * @return <code>true</code> if the object was not already in the + * <code>uniqueSet</code> */ - public boolean add(Object object, int nCopies) { + public boolean add(E object, int nCopies) { modCount++; if (nCopies > 0) { - MutableInteger mut = (MutableInteger) map.get(object); + MutableInteger mut = map.get(object); size += nCopies; if (mut == null) { map.put(object, new MutableInteger(nCopies)); return true; - } else { - mut.value += nCopies; - return false; } - } else { + mut.value += nCopies; return false; } + return false; } /** * Invokes {@link #add(Object)} for each element in the given collection. - * - * @param coll the collection to add + * + * @param coll the collection to add * @return <code>true</code> if this call changed the bag */ - public boolean addAll(Collection coll) { + public boolean addAll(Collection<? extends E> coll) { boolean changed = false; - Iterator i = coll.iterator(); + Iterator<? extends E> i = coll.iterator(); while (i.hasNext()) { boolean added = add(i.next()); changed = changed || added; @@ -297,11 +300,11 @@ public abstract class AbstractMapBag implements Bag { /** * Removes all copies of the specified object from the bag. * - * @param object the object to remove + * @param object the object to remove * @return true if the bag changed */ public boolean remove(Object object) { - MutableInteger mut = (MutableInteger) map.get(object); + MutableInteger mut = map.get(object); if (mut == null) { return false; } @@ -314,12 +317,12 @@ public abstract class AbstractMapBag implements Bag { /** * Removes a specified number of copies of an object from the bag. * - * @param object the object to remove - * @param nCopies the number of copies to remove + * @param object the object to remove + * @param nCopies the number of copies to remove * @return true if the bag changed */ public boolean remove(Object object, int nCopies) { - MutableInteger mut = (MutableInteger) map.get(object); + MutableInteger mut = map.get(object); if (mut == null) { return false; } @@ -338,15 +341,16 @@ public abstract class AbstractMapBag implements Bag { } /** - * Removes objects from the bag according to their count in the specified collection. + * Removes objects from the bag according to their count in the specified + * collection. * - * @param coll the collection to use + * @param coll the collection to use * @return true if the bag changed */ - public boolean removeAll(Collection coll) { + public boolean removeAll(Collection<?> coll) { boolean result = false; if (coll != null) { - Iterator i = coll.iterator(); + Iterator<?> i = coll.iterator(); while (i.hasNext()) { boolean changed = remove(i.next(), 1); result = result || changed; @@ -356,33 +360,34 @@ public abstract class AbstractMapBag implements Bag { } /** - * Remove any members of the bag that are not in the given - * bag, respecting cardinality. - * - * @param coll the collection to retain + * Remove any members of the bag that are not in the given bag, respecting + * cardinality. + * + * @param coll the collection to retain * @return true if this call changed the collection */ - public boolean retainAll(Collection coll) { + @SuppressWarnings("unchecked") + public boolean retainAll(Collection<?> coll) { if (coll instanceof Bag) { - return retainAll((Bag) coll); + return retainAll((Bag<?>) coll); } - return retainAll(new HashBag(coll)); + return retainAll(new HashBag<Object>((Collection<Object>) coll)); } /** - * Remove any members of the bag that are not in the given - * bag, respecting cardinality. + * Remove any members of the bag that are not in the given bag, respecting + * cardinality. * @see #retainAll(Collection) * - * @param other the bag to retain + * @param other the bag to retain * @return <code>true</code> if this call changed the collection */ - boolean retainAll(Bag other) { + boolean retainAll(Bag<?> other) { boolean result = false; - Bag excess = new HashBag(); - Iterator i = uniqueSet().iterator(); + Bag<E> excess = new HashBag<E>(); + Iterator<E> i = uniqueSet().iterator(); while (i.hasNext()) { - Object current = i.next(); + E current = i.next(); int myCount = getCount(current); int otherCount = other.getCount(current); if (1 <= otherCount && otherCount <= myCount) { @@ -404,15 +409,15 @@ public abstract class AbstractMapBag implements Bag { protected static class MutableInteger { /** The value of this mutable. */ protected int value; - + /** * Constructor. - * @param value the initial value + * @param value the initial value */ MutableInteger(int value) { this.value = value; } - + public boolean equals(Object obj) { if (obj instanceof MutableInteger == false) { return false; @@ -424,19 +429,19 @@ public abstract class AbstractMapBag implements Bag { return value; } } - + //----------------------------------------------------------------------- /** * Returns an array of all of this bag's elements. - * + * * @return an array of all of this bag's elements */ public Object[] toArray() { Object[] result = new Object[size()]; int i = 0; - Iterator it = map.keySet().iterator(); + Iterator<E> it = map.keySet().iterator(); while (it.hasNext()) { - Object current = it.next(); + E current = it.next(); for (int index = getCount(current); index > 0; index--) { result[i++] = current; } @@ -446,38 +451,39 @@ public abstract class AbstractMapBag implements Bag { /** * Returns an array of all of this bag's elements. - * - * @param array the array to populate + * + * @param array the array to populate * @return an array of all of this bag's elements */ - public Object[] toArray(Object[] array) { + @SuppressWarnings("unchecked") + public <T> T[] toArray(T[] array) { int size = size(); if (array.length < size) { - array = (Object[]) Array.newInstance(array.getClass().getComponentType(), size); + array = (T[]) Array.newInstance(array.getClass().getComponentType(), size); } int i = 0; - Iterator it = map.keySet().iterator(); + Iterator<E> it = map.keySet().iterator(); while (it.hasNext()) { - Object current = it.next(); + E current = it.next(); for (int index = getCount(current); index > 0; index--) { - array[i++] = current; + array[i++] = (T) current; } } - if (array.length > size) { - array[size] = null; + while (i < array.length) { + array[i++] = null; } return array; } /** * Returns an unmodifiable view of the underlying map's key set. - * + * * @return the set of unique elements in this bag */ - public Set uniqueSet() { + public Set<E> uniqueSet() { if (uniqueSet == null) { - uniqueSet = UnmodifiableSet.decorate(map.keySet()); + uniqueSet = UnmodifiableSet.<E> decorate(map.keySet()); } return uniqueSet; } @@ -485,26 +491,28 @@ public abstract class AbstractMapBag implements Bag { //----------------------------------------------------------------------- /** * Write the map out using a custom routine. - * @param out the output stream + * @param out the output stream * @throws IOException */ protected void doWriteObject(ObjectOutputStream out) throws IOException { out.writeInt(map.size()); - for (Iterator it = map.entrySet().iterator(); it.hasNext();) { - Map.Entry entry = (Map.Entry) it.next(); + for (Iterator<Map.Entry<E, MutableInteger>> it = map.entrySet().iterator(); it.hasNext();) { + Map.Entry<E, MutableInteger> entry = it.next(); out.writeObject(entry.getKey()); - out.writeInt(((MutableInteger) entry.getValue()).value); + out.writeInt(entry.getValue().value); } } /** * Read the map in using a custom routine. - * @param map the map to use - * @param in the input stream + * @param map the map to use + * @param in the input stream * @throws IOException * @throws ClassNotFoundException */ - protected void doReadObject(Map map, ObjectInputStream in) throws IOException, ClassNotFoundException { + @SuppressWarnings("unchecked") + protected void doReadObject(Map map, ObjectInputStream in) throws IOException, + ClassNotFoundException { this.map = map; int entrySize = in.readInt(); for (int i = 0; i < entrySize; i++) { @@ -514,14 +522,13 @@ public abstract class AbstractMapBag implements Bag { size += count; } } - + //----------------------------------------------------------------------- /** - * Compares this Bag to another. - * This Bag equals another Bag if it contains the same number of occurrences of - * the same elements. + * Compares this Bag to another. This Bag equals another Bag if it contains + * the same number of occurrences of the same elements. * - * @param object the Bag to compare to + * @param object the Bag to compare to * @return true if equal */ public boolean equals(Object object) { @@ -531,12 +538,12 @@ public abstract class AbstractMapBag implements Bag { if (object instanceof Bag == false) { return false; } - Bag other = (Bag) object; + Bag<?> other = (Bag<?>) object; if (other.size() != size()) { return false; } - for (Iterator it = map.keySet().iterator(); it.hasNext();) { - Object element = it.next(); + for (Iterator<E> it = map.keySet().iterator(); it.hasNext();) { + E element = it.next(); if (other.getCount(element) != getCount(element)) { return false; } @@ -546,19 +553,19 @@ public abstract class AbstractMapBag implements Bag { /** * Gets a hash code for the Bag compatible with the definition of equals. - * The hash code is defined as the sum total of a hash code for each element. - * The per element hash code is defined as - * <code>(e==null ? 0 : e.hashCode()) ^ noOccurances)</code>. - * This hash code is compatible with the Set interface. + * The hash code is defined as the sum total of a hash code for each + * element. The per element hash code is defined as + * <code>(e==null ? 0 : e.hashCode()) ^ noOccurances)</code>. This hash code + * is compatible with the Set interface. * * @return the hash code of the Bag */ public int hashCode() { int total = 0; - for (Iterator it = map.entrySet().iterator(); it.hasNext();) { - Map.Entry entry = (Map.Entry) it.next(); - Object element = entry.getKey(); - MutableInteger count = (MutableInteger) entry.getValue(); + for (Iterator<Map.Entry<E, MutableInteger>> it = map.entrySet().iterator(); it.hasNext();) { + Map.Entry<E, MutableInteger> entry = it.next(); + E element = entry.getKey(); + MutableInteger count = entry.getValue(); total += (element == null ? 0 : element.hashCode()) ^ count.value; } return total; @@ -575,7 +582,7 @@ public abstract class AbstractMapBag implements Bag { } StringBuffer buf = new StringBuffer(); buf.append('['); - Iterator it = uniqueSet().iterator(); + Iterator<E> it = uniqueSet().iterator(); while (it.hasNext()) { Object current = it.next(); int count = getCount(current); @@ -589,5 +596,5 @@ public abstract class AbstractMapBag implements Bag { buf.append(']'); return buf.toString(); } - + } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/bag/AbstractSortedBagDecorator.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/bag/AbstractSortedBagDecorator.java b/src/java/org/apache/commons/collections/bag/AbstractSortedBagDecorator.java index 0a3fc25..5446842 100644 --- a/src/java/org/apache/commons/collections/bag/AbstractSortedBagDecorator.java +++ b/src/java/org/apache/commons/collections/bag/AbstractSortedBagDecorator.java @@ -33,6 +33,9 @@ import org.apache.commons.collections.SortedBag; public abstract class AbstractSortedBagDecorator<E> extends AbstractBagDecorator<E> implements SortedBag<E> { + /** Serialization version */ + private static final long serialVersionUID = -8223473624050467718L; + /** * Constructor only used in deserialization, do not use otherwise. * @since Commons Collections 3.1 http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/bag/HashBag.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/bag/HashBag.java b/src/java/org/apache/commons/collections/bag/HashBag.java index 4d47512..b92b0bd 100644 --- a/src/java/org/apache/commons/collections/bag/HashBag.java +++ b/src/java/org/apache/commons/collections/bag/HashBag.java @@ -41,8 +41,8 @@ import org.apache.commons.collections.Bag; * @author Chuck Burdick * @author Stephen Colebourne */ -public class HashBag - extends AbstractMapBag implements Bag, Serializable { +public class HashBag<E> + extends AbstractMapBag<E> implements Bag<E>, Serializable { /** Serial version lock */ private static final long serialVersionUID = -6561115435802554013L; @@ -51,7 +51,7 @@ public class HashBag * Constructs an empty <code>HashBag</code>. */ public HashBag() { - super(new HashMap()); + super(new HashMap<E, MutableInteger>()); } /** @@ -59,7 +59,7 @@ public class HashBag * * @param coll a collection to copy into this bag */ - public HashBag(Collection coll) { + public HashBag(Collection<? extends E> coll) { this(); addAll(coll); } @@ -78,7 +78,7 @@ public class HashBag */ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); - super.doReadObject(new HashMap(), in); + super.doReadObject(new HashMap<E, MutableInteger>(), in); } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/bag/SynchronizedBag.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/bag/SynchronizedBag.java b/src/java/org/apache/commons/collections/bag/SynchronizedBag.java index 2bc47c0..cd4958f 100644 --- a/src/java/org/apache/commons/collections/bag/SynchronizedBag.java +++ b/src/java/org/apache/commons/collections/bag/SynchronizedBag.java @@ -115,6 +115,9 @@ public class SynchronizedBag<E> * Synchronized Set for the Bag class. */ class SynchronizedBagSet extends SynchronizedSet<E> { + /** Serialization version */ + private static final long serialVersionUID = 2990565892366827855L; + /** * Constructor. * @param set the set to decorate http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/bag/SynchronizedSortedBag.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/bag/SynchronizedSortedBag.java b/src/java/org/apache/commons/collections/bag/SynchronizedSortedBag.java index d4c84ad..86bfc09 100644 --- a/src/java/org/apache/commons/collections/bag/SynchronizedSortedBag.java +++ b/src/java/org/apache/commons/collections/bag/SynchronizedSortedBag.java @@ -48,8 +48,8 @@ public class SynchronizedSortedBag<E> * @return a new synchronized SortedBag * @throws IllegalArgumentException if bag is null */ - public static <T> SortedBag<T> decorate(SortedBag<T> bag) { - return new SynchronizedSortedBag<T>(bag); + public static <E> SortedBag<E> decorate(SortedBag<E> bag) { + return new SynchronizedSortedBag<E>(bag); } //----------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/bag/TransformedBag.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/bag/TransformedBag.java b/src/java/org/apache/commons/collections/bag/TransformedBag.java index e0a7689..8663d8b 100644 --- a/src/java/org/apache/commons/collections/bag/TransformedBag.java +++ b/src/java/org/apache/commons/collections/bag/TransformedBag.java @@ -38,8 +38,8 @@ import org.apache.commons.collections.set.TransformedSet; * * @author Stephen Colebourne */ -public class TransformedBag - extends TransformedCollection implements Bag { +public class TransformedBag<E> + extends TransformedCollection<E> implements Bag<E> { /** Serialization version */ private static final long serialVersionUID = 5421170911299074185L; @@ -55,8 +55,8 @@ public class TransformedBag * @return a new transformed Bag * @throws IllegalArgumentException if bag or transformer is null */ - public static Bag decorate(Bag bag, Transformer transformer) { - return new TransformedBag(bag, transformer); + public static <E> Bag<E> decorate(Bag<E> bag, Transformer<? super E, ? extends E> transformer) { + return new TransformedBag<E>(bag, transformer); } //----------------------------------------------------------------------- @@ -70,7 +70,7 @@ public class TransformedBag * @param transformer the transformer to use for conversion, must not be null * @throws IllegalArgumentException if bag or transformer is null */ - protected TransformedBag(Bag bag, Transformer transformer) { + protected TransformedBag(Bag<E> bag, Transformer<? super E, ? extends E> transformer) { super(bag, transformer); } @@ -79,8 +79,8 @@ public class TransformedBag * * @return the decorated bag */ - protected Bag getBag() { - return (Bag) collection; + protected Bag<E> getBag() { + return (Bag<E>) collection; } //----------------------------------------------------------------------- @@ -93,14 +93,13 @@ public class TransformedBag } //----------------------------------------------------------------------- - public boolean add(Object object, int nCopies) { - object = transform(object); - return getBag().add(object, nCopies); + public boolean add(E object, int nCopies) { + return getBag().add(transform(object), nCopies); } - public Set uniqueSet() { - Set set = getBag().uniqueSet(); - return TransformedSet.decorate(set, transformer); + public Set<E> uniqueSet() { + Set<E> set = getBag().uniqueSet(); + return TransformedSet.<E>decorate(set, transformer); } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/bag/TransformedSortedBag.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/bag/TransformedSortedBag.java b/src/java/org/apache/commons/collections/bag/TransformedSortedBag.java index a978129..9bdb922 100644 --- a/src/java/org/apache/commons/collections/bag/TransformedSortedBag.java +++ b/src/java/org/apache/commons/collections/bag/TransformedSortedBag.java @@ -36,8 +36,8 @@ import org.apache.commons.collections.Transformer; * * @author Stephen Colebourne */ -public class TransformedSortedBag - extends TransformedBag implements SortedBag { +public class TransformedSortedBag<E> + extends TransformedBag<E> implements SortedBag<E> { /** Serialization version */ private static final long serialVersionUID = -251737742649401930L; @@ -53,8 +53,8 @@ public class TransformedSortedBag * @return a new transformed SortedBag * @throws IllegalArgumentException if bag or transformer is null */ - public static SortedBag decorate(SortedBag bag, Transformer transformer) { - return new TransformedSortedBag(bag, transformer); + public static <E> SortedBag<E> decorate(SortedBag<E> bag, Transformer<? super E, ? extends E> transformer) { + return new TransformedSortedBag<E>(bag, transformer); } //----------------------------------------------------------------------- @@ -68,7 +68,7 @@ public class TransformedSortedBag * @param transformer the transformer to use for conversion, must not be null * @throws IllegalArgumentException if bag or transformer is null */ - protected TransformedSortedBag(SortedBag bag, Transformer transformer) { + protected TransformedSortedBag(SortedBag<E> bag, Transformer<? super E, ? extends E> transformer) { super(bag, transformer); } @@ -77,20 +77,20 @@ public class TransformedSortedBag * * @return the decorated bag */ - protected SortedBag getSortedBag() { - return (SortedBag) collection; + protected SortedBag<E> getSortedBag() { + return (SortedBag<E>) collection; } //----------------------------------------------------------------------- - public Object first() { + public E first() { return getSortedBag().first(); } - public Object last() { + public E last() { return getSortedBag().last(); } - public Comparator comparator() { + public Comparator<? super E> comparator() { return getSortedBag().comparator(); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/bag/TreeBag.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/bag/TreeBag.java b/src/java/org/apache/commons/collections/bag/TreeBag.java index b73eac7..f2091a5 100644 --- a/src/java/org/apache/commons/collections/bag/TreeBag.java +++ b/src/java/org/apache/commons/collections/bag/TreeBag.java @@ -34,63 +34,72 @@ import org.apache.commons.collections.SortedBag; * Order will be maintained among the bag members and can be viewed through the * iterator. * <p> - * A <code>Bag</code> stores each object in the collection together with a - * count of occurrences. Extra methods on the interface allow multiple copies - * of an object to be added or removed at once. It is important to read the - * interface javadoc carefully as several methods violate the - * <code>Collection</code> interface specification. - * + * A <code>Bag</code> stores each object in the collection together with a count + * of occurrences. Extra methods on the interface allow multiple copies of an + * object to be added or removed at once. It is important to read the interface + * javadoc carefully as several methods violate the <code>Collection</code> + * interface specification. + * * @since Commons Collections 3.0 (previously in main package v2.0) - * @version $Revision$ $Date$ + * @version $Revision$ $Date: 2006-10-27 19:52:37 -0500 (Fri, 27 Oct + * 2006) $ * * @author Chuck Burdick * @author Stephen Colebourne */ -public class TreeBag - extends AbstractMapBag implements SortedBag, Serializable { +public class TreeBag<E> extends AbstractMapBag<E> implements SortedBag<E>, Serializable { /** Serial version lock */ private static final long serialVersionUID = -7740146511091606676L; - + /** * Constructs an empty <code>TreeBag</code>. */ public TreeBag() { - super(new TreeMap()); + super(new TreeMap<E, MutableInteger>()); } /** - * Constructs an empty bag that maintains order on its unique - * representative members according to the given {@link Comparator}. + * Constructs an empty bag that maintains order on its unique representative + * members according to the given {@link Comparator}. * - * @param comparator the comparator to use + * @param comparator the comparator to use */ - public TreeBag(Comparator comparator) { - super(new TreeMap(comparator)); + public TreeBag(Comparator<? super E> comparator) { + super(new TreeMap<E, MutableInteger>(comparator)); } /** * Constructs a <code>TreeBag</code> containing all the members of the * specified collection. * - * @param coll the collection to copy into the bag + * @param coll the collection to copy into the bag */ - public TreeBag(Collection coll) { + public TreeBag(Collection<? extends E> coll) { this(); addAll(coll); } //----------------------------------------------------------------------- - public Object first() { - return ((SortedMap) getMap()).firstKey(); + public E first() { + return getMap().firstKey(); } - public Object last() { - return ((SortedMap) getMap()).lastKey(); + public E last() { + return getMap().lastKey(); } - public Comparator comparator() { - return ((SortedMap) getMap()).comparator(); + public Comparator<? super E> comparator() { + return getMap().comparator(); + } + + /** + * {@inheritDoc} + */ + @Override + protected SortedMap<E, org.apache.commons.collections.bag.AbstractMapBag.MutableInteger> getMap() { + return (SortedMap<E, org.apache.commons.collections.bag.AbstractMapBag.MutableInteger>) super + .getMap(); } //----------------------------------------------------------------------- @@ -106,10 +115,11 @@ public class TreeBag /** * Read the bag in using a custom routine. */ + @SuppressWarnings("unchecked") private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); Comparator comp = (Comparator) in.readObject(); super.doReadObject(new TreeMap(comp), in); } - + } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/bag/UnmodifiableBag.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/bag/UnmodifiableBag.java b/src/java/org/apache/commons/collections/bag/UnmodifiableBag.java index ee85e82..b2c9609 100644 --- a/src/java/org/apache/commons/collections/bag/UnmodifiableBag.java +++ b/src/java/org/apache/commons/collections/bag/UnmodifiableBag.java @@ -39,8 +39,8 @@ import org.apache.commons.collections.set.UnmodifiableSet; * * @author Stephen Colebourne */ -public final class UnmodifiableBag - extends AbstractBagDecorator implements Unmodifiable, Serializable { +public final class UnmodifiableBag<E> + extends AbstractBagDecorator<E> implements Unmodifiable, Serializable { /** Serialization version */ private static final long serialVersionUID = -1873799975157099624L; @@ -54,11 +54,11 @@ public final class UnmodifiableBag * @return an unmodifiable Bag * @throws IllegalArgumentException if bag is null */ - public static Bag decorate(Bag bag) { + public static <E> Bag<E> decorate(Bag<E> bag) { if (bag instanceof Unmodifiable) { return bag; } - return new UnmodifiableBag(bag); + return new UnmodifiableBag<E>(bag); } //----------------------------------------------------------------------- @@ -68,7 +68,7 @@ public final class UnmodifiableBag * @param bag the bag to decorate, must not be null * @throws IllegalArgumentException if bag is null */ - private UnmodifiableBag(Bag bag) { + private UnmodifiableBag(Bag<E> bag) { super(bag); } @@ -91,21 +91,22 @@ public final class UnmodifiableBag * @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() { - return UnmodifiableIterator.decorate(decorated().iterator()); + public Iterator<E> iterator() { + return UnmodifiableIterator.<E>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(); } @@ -117,16 +118,16 @@ public final class UnmodifiableBag 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 add(Object object, int count) { + public boolean add(E object, int count) { throw new UnsupportedOperationException(); } @@ -134,9 +135,9 @@ public final class UnmodifiableBag throw new UnsupportedOperationException(); } - public Set uniqueSet() { - Set set = decorated().uniqueSet(); - return UnmodifiableSet.decorate(set); + public Set<E> uniqueSet() { + Set<E> set = decorated().uniqueSet(); + return UnmodifiableSet.<E>decorate(set); } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/bag/UnmodifiableSortedBag.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/bag/UnmodifiableSortedBag.java b/src/java/org/apache/commons/collections/bag/UnmodifiableSortedBag.java index 5018a58..023f470 100644 --- a/src/java/org/apache/commons/collections/bag/UnmodifiableSortedBag.java +++ b/src/java/org/apache/commons/collections/bag/UnmodifiableSortedBag.java @@ -54,11 +54,11 @@ public final class UnmodifiableSortedBag<E> * @return an unmodifiable SortedBag * @throws IllegalArgumentException if bag is null */ - public static <T> SortedBag<T> decorate(SortedBag<T> bag) { + public static <E> SortedBag<E> decorate(SortedBag<E> bag) { if (bag instanceof Unmodifiable) { return bag; } - return new UnmodifiableSortedBag<T>(bag); + return new UnmodifiableSortedBag<E>(bag); } //----------------------------------------------------------------------- @@ -91,13 +91,14 @@ public final class UnmodifiableSortedBag<E> * @throws IOException * @throws ClassNotFoundException */ + @SuppressWarnings("unchecked") private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); collection = (Collection<E>) in.readObject(); } //----------------------------------------------------------------------- - public Iterator iterator() { + public Iterator<E> iterator() { return UnmodifiableIterator.decorate(decorated().iterator()); }