From 8283d966f47280f682a1407d499346a383d1b1a5 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Sun, 5 May 2013 15:06:50 +0000 Subject: [PATCH] [COLLECTIONS-453] Change ChainedTransformer and ChainedClosure constructors to copy input arguments, add private constructor for factory methods to not copy twice. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1479336 13f79535-47bb-0310-9956-ffa450edef68 --- .../collections4/functors/ChainedClosure.java | 22 ++++++++++++++----- .../functors/ChainedTransformer.java | 22 ++++++++++++++----- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/functors/ChainedClosure.java b/src/main/java/org/apache/commons/collections4/functors/ChainedClosure.java index ca415d63b..2e4436a29 100644 --- a/src/main/java/org/apache/commons/collections4/functors/ChainedClosure.java +++ b/src/main/java/org/apache/commons/collections4/functors/ChainedClosure.java @@ -49,7 +49,7 @@ public class ChainedClosure implements Closure, Serializable { if (closures.length == 0) { return NOPClosure.nopClosure(); } - return new ChainedClosure(FunctorUtils.copy(closures)); + return new ChainedClosure(closures); } /** @@ -78,18 +78,28 @@ public class ChainedClosure implements Closure, Serializable { cmds[i++] = closure; } FunctorUtils.validate(cmds); - return new ChainedClosure(cmds); + return new ChainedClosure(false, cmds); + } + + /** + * Hidden constructor for the use by the static factory methods. + * + * @param clone if {@code true} the input argument will be cloned + * @param closures the closures to chain, no nulls + */ + private ChainedClosure(final boolean clone, final Closure... closures) { + super(); + iClosures = clone ? FunctorUtils.copy(closures) : closures; } /** * Constructor that performs no validation. * Use chainedClosure if you want that. * - * @param closures the closures to chain, not copied, no nulls + * @param closures the closures to chain, copied, no nulls */ - public ChainedClosure(final Closure[] closures) { - super(); - iClosures = closures; + public ChainedClosure(final Closure... closures) { + this(true, closures); } /** diff --git a/src/main/java/org/apache/commons/collections4/functors/ChainedTransformer.java b/src/main/java/org/apache/commons/collections4/functors/ChainedTransformer.java index 5f270538c..045a80644 100644 --- a/src/main/java/org/apache/commons/collections4/functors/ChainedTransformer.java +++ b/src/main/java/org/apache/commons/collections4/functors/ChainedTransformer.java @@ -52,7 +52,7 @@ public class ChainedTransformer implements Transformer, Serializable { if (transformers.length == 0) { return NOPTransformer.nopTransformer(); } - return new ChainedTransformer(FunctorUtils.copy(transformers)); + return new ChainedTransformer(transformers); } /** @@ -77,18 +77,28 @@ public class ChainedTransformer implements Transformer, Serializable { // convert to array like this to guarantee iterator() ordering final Transformer[] cmds = transformers.toArray(new Transformer[transformers.size()]); FunctorUtils.validate(cmds); - return new ChainedTransformer(cmds); + return new ChainedTransformer(false, cmds); + } + + /** + * 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 + */ + private ChainedTransformer(final boolean clone, final Transformer[] transformers) { + super(); + iTransformers = clone ? FunctorUtils.copy(transformers) : transformers; } /** * Constructor that performs no validation. * Use chainedTransformer if you want that. * - * @param transformers the transformers to chain, not copied, no nulls + * @param transformers the transformers to chain, copied, no nulls */ - public ChainedTransformer(final Transformer[] transformers) { - super(); - iTransformers = transformers; + public ChainedTransformer(final Transformer... transformers) { + this(true, transformers); } /**