[COLLECTIONS-453] Fix SwitchClosure and SwitchTransformer.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1479337 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-05-05 15:20:59 +00:00
parent 8283d966f4
commit 5878adfaf3
3 changed files with 47 additions and 22 deletions

View File

@ -84,7 +84,7 @@ public class ChainedTransformer<T> implements Transformer<T, T>, Serializable {
* Hidden constructor for the use by the static factory methods. * Hidden constructor for the use by the static factory methods.
* *
* @param clone if {@code true} the input argument will be cloned * @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) { private ChainedTransformer(final boolean clone, final Transformer<? super T, ? extends T>[] transformers) {
super(); super();

View File

@ -64,7 +64,7 @@ public class SwitchClosure<E> implements Closure<E>, Serializable {
if (predicates.length == 0) { if (predicates.length == 0) {
return (Closure<E>) (defaultClosure == null ? NOPClosure.<E>nopClosure(): defaultClosure); 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 Closure<E>, Serializable {
closures[i] = entry.getValue(); closures[i] = entry.getValue();
i++; 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. * Constructor that performs no validation.
* Use <code>switchClosure</code> if you want that. * Use <code>switchClosure</code> if you want that.
* *
* @param predicates array of predicates, not cloned, no nulls * @param predicates array of predicates, cloned, no nulls
* @param closures matching array of closures, not cloned, no nulls * @param closures matching array of closures, cloned, no nulls
* @param defaultClosure the closure to use if no match, null means nop * @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, public SwitchClosure(final Predicate<? super E>[] predicates, final Closure<? super E>[] closures,
final Closure<? super E> defaultClosure) { final Closure<? super E> defaultClosure) {
super(); this(true, predicates, closures, defaultClosure);
iPredicates = predicates;
iClosures = closures;
iDefault = (Closure<? super E>) (defaultClosure == null ? NOPClosure.<E>nopClosure() : defaultClosure);
} }
/** /**

View File

@ -66,9 +66,7 @@ public class SwitchTransformer<I, O> implements Transformer<I, O>, Serializable
return (Transformer<I, O>) (defaultTransformer == null ? ConstantTransformer.<I, O>nullTransformer() : return (Transformer<I, O>) (defaultTransformer == null ? ConstantTransformer.<I, O>nullTransformer() :
defaultTransformer); defaultTransformer);
} }
return new SwitchTransformer<I, O>(FunctorUtils.copy(predicates), return new SwitchTransformer<I, O>(predicates, transformers, defaultTransformer);
FunctorUtils.copy(transformers),
defaultTransformer);
} }
/** /**
@ -116,26 +114,40 @@ public class SwitchTransformer<I, O> implements Transformer<I, O>, Serializable
transformers[i] = entry.getValue(); transformers[i] = entry.getValue();
i++; 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. * Constructor that performs no validation.
* Use <code>switchTransformer</code> if you want that. * Use <code>switchTransformer</code> if you want that.
* *
* @param predicates array of predicates, not cloned, no nulls * @param predicates array of predicates, cloned, no nulls
* @param transformers matching array of transformers, not 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 * @param defaultTransformer the transformer to use if no match, null means return null
*/ */
@SuppressWarnings("unchecked")
public SwitchTransformer(final Predicate<? super I>[] predicates, public SwitchTransformer(final Predicate<? super I>[] predicates,
final Transformer<? super I, ? extends O>[] transformers, final Transformer<? super I, ? extends O>[] transformers,
final Transformer<? super I, ? extends O> defaultTransformer) { final Transformer<? super I, ? extends O> defaultTransformer) {
super(); this(true, predicates, transformers, defaultTransformer);
iPredicates = predicates;
iTransformers = transformers;
iDefault = (Transformer<? super I, ? extends O>) (defaultTransformer == null ?
ConstantTransformer.<I, O>nullTransformer() : defaultTransformer);
} }
/** /**