You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2013/05/05 17:21:00 UTC

svn commit: r1479337 - in /commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors: ChainedTransformer.java SwitchClosure.java SwitchTransformer.java

Author: tn
Date: Sun May  5 15:20:59 2013
New Revision: 1479337

URL: http://svn.apache.org/r1479337
Log:
[COLLECTIONS-453] Fix SwitchClosure and SwitchTransformer.

Modified:
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/ChainedTransformer.java
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/SwitchClosure.java
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/SwitchTransformer.java

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/ChainedTransformer.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/ChainedTransformer.java?rev=1479337&r1=1479336&r2=1479337&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/ChainedTransformer.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/ChainedTransformer.java Sun May  5 15:20:59 2013
@@ -84,7 +84,7 @@ public class ChainedTransformer<T> imple
      * Hidden constructor for the use by the static factory methods.
      *
      * @param clone  if {@code true} the input argument will be cloned
-     * @param transformers  the transformers to chain, not copied, no nulls
+     * @param transformers  the transformers to chain, no nulls
      */
     private ChainedTransformer(final boolean clone, final Transformer<? super T, ? extends T>[] transformers) {
         super();

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/SwitchClosure.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/SwitchClosure.java?rev=1479337&r1=1479336&r2=1479337&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/SwitchClosure.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/SwitchClosure.java Sun May  5 15:20:59 2013
@@ -64,7 +64,7 @@ public class SwitchClosure<E> implements
         if (predicates.length == 0) {
             return (Closure<E>) (defaultClosure == null ? NOPClosure.<E>nopClosure(): defaultClosure);
         }
-        return new SwitchClosure<E>(FunctorUtils.copy(predicates), FunctorUtils.copy(closures), defaultClosure);
+        return new SwitchClosure<E>(predicates, closures, defaultClosure);
     }
 
     /**
@@ -104,24 +104,37 @@ public class SwitchClosure<E> implements
             closures[i] = entry.getValue();
             i++;
         }
-        return new SwitchClosure<E>(preds, closures, defaultClosure);
+        return new SwitchClosure<E>(false, preds, closures, defaultClosure);
+    }
+
+    /**
+     * Hidden constructor for the use by the static factory methods.
+     *
+     * @param clone  if {@code true} the input arguments will be cloned
+     * @param predicates  array of predicates, no nulls
+     * @param closures  matching array of closures, no nulls
+     * @param defaultClosure  the closure to use if no match, null means nop
+     */
+    @SuppressWarnings("unchecked")
+    private SwitchClosure(final boolean clone, final Predicate<? super E>[] predicates,
+                          final Closure<? super E>[] closures, final Closure<? super E> defaultClosure) {
+        super();
+        iPredicates = clone ? FunctorUtils.copy(predicates) : predicates;
+        iClosures = clone ? FunctorUtils.copy(closures) : closures;
+        iDefault = (Closure<? super E>) (defaultClosure == null ? NOPClosure.<E>nopClosure() : defaultClosure);
     }
 
     /**
      * Constructor that performs no validation.
      * Use <code>switchClosure</code> if you want that.
      *
-     * @param predicates  array of predicates, not cloned, no nulls
-     * @param closures  matching array of closures, not cloned, no nulls
+     * @param predicates  array of predicates, cloned, no nulls
+     * @param closures  matching array of closures, cloned, no nulls
      * @param defaultClosure  the closure to use if no match, null means nop
      */
-    @SuppressWarnings("unchecked")
     public SwitchClosure(final Predicate<? super E>[] predicates, final Closure<? super E>[] closures,
                          final Closure<? super E> defaultClosure) {
-        super();
-        iPredicates = predicates;
-        iClosures = closures;
-        iDefault = (Closure<? super E>) (defaultClosure == null ? NOPClosure.<E>nopClosure() : defaultClosure);
+        this(true, predicates, closures, defaultClosure);
     }
 
     /**

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/SwitchTransformer.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/SwitchTransformer.java?rev=1479337&r1=1479336&r2=1479337&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/SwitchTransformer.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/SwitchTransformer.java Sun May  5 15:20:59 2013
@@ -66,9 +66,7 @@ public class SwitchTransformer<I, O> imp
             return (Transformer<I, O>) (defaultTransformer == null ? ConstantTransformer.<I, O>nullTransformer() :
                                                                      defaultTransformer);
         }
-        return new SwitchTransformer<I, O>(FunctorUtils.copy(predicates),
-                                           FunctorUtils.copy(transformers),
-                                           defaultTransformer);
+        return new SwitchTransformer<I, O>(predicates, transformers, defaultTransformer);
     }
 
     /**
@@ -116,26 +114,40 @@ public class SwitchTransformer<I, O> imp
             transformers[i] = entry.getValue();
             i++;
         }
-        return new SwitchTransformer<I, O>(preds, transformers, defaultTransformer);
+        return new SwitchTransformer<I, O>(false, preds, transformers, defaultTransformer);
+    }
+
+    /**
+     * Hidden constructor for the use by the static factory methods.
+     *
+     * @param clone  if {@code true} the input arguments will be cloned
+     * @param predicates  array of predicates, no nulls
+     * @param transformers  matching array of transformers, no nulls
+     * @param defaultTransformer  the transformer to use if no match, null means return null
+     */
+    @SuppressWarnings("unchecked")
+    private SwitchTransformer(final boolean clone, final Predicate<? super I>[] predicates,
+                             final Transformer<? super I, ? extends O>[] transformers,
+                             final Transformer<? super I, ? extends O> defaultTransformer) {
+        super();
+        iPredicates = clone ? FunctorUtils.copy(predicates) : predicates;
+        iTransformers = clone ? FunctorUtils.copy(transformers) : transformers;
+        iDefault = (Transformer<? super I, ? extends O>) (defaultTransformer == null ?
+                ConstantTransformer.<I, O>nullTransformer() : defaultTransformer);
     }
 
     /**
      * Constructor that performs no validation.
      * Use <code>switchTransformer</code> if you want that.
      *
-     * @param predicates  array of predicates, not cloned, no nulls
-     * @param transformers  matching array of transformers, not cloned, no nulls
+     * @param predicates  array of predicates, cloned, no nulls
+     * @param transformers  matching array of transformers, cloned, no nulls
      * @param defaultTransformer  the transformer to use if no match, null means return null
      */
-    @SuppressWarnings("unchecked")
     public SwitchTransformer(final Predicate<? super I>[] predicates,
             final Transformer<? super I, ? extends O>[] transformers,
             final Transformer<? super I, ? extends O> defaultTransformer) {
-        super();
-        iPredicates = predicates;
-        iTransformers = transformers;
-        iDefault = (Transformer<? super I, ? extends O>) (defaultTransformer == null ?
-                ConstantTransformer.<I, O>nullTransformer() : defaultTransformer);
+        this(true, predicates, transformers, defaultTransformer);
     }
 
     /**