Author: tn Date: Sun Dec 8 11:41:03 2013 New Revision: 1549021 URL: http://svn.apache.org/r1549021 Log: [COLLECTIONS-503] Added new IfTransformer and deprecate TransformerUtils.switchTransformer(Predicate, Transformer, Transformer). Thanks to Josh Cain.
Added: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/IfTransformer.java (with props) Modified: commons/proper/collections/trunk/src/changes/changes.xml commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/TransformerUtils.java commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/TransformerUtilsTest.java Modified: commons/proper/collections/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1549021&r1=1549020&r2=1549021&view=diff ============================================================================== --- commons/proper/collections/trunk/src/changes/changes.xml (original) +++ commons/proper/collections/trunk/src/changes/changes.xml Sun Dec 8 11:41:03 2013 @@ -22,8 +22,12 @@ <body> <release version="4.0.1" date="TBD" description=""> + <action issue="COLLECTIONS-503" dev="tn" type="add" due-to="Josh Cain"> + Added new transformer "IfTransformer" and factory methods "TransformerUtils#ifTransformer(...)" + which replace "TransformerUtils#switchTransformer(Predicate, Transformer, Transformer)". + </action> <action issue="COLLECTIONS-471" dev="tn" type="add" due-to="Radford Tam"> - Added new decorator "BoundedIterator". + Added new decorator "BoundedIterator" and factory methods "IteratorUtils#boundedIterator(...)". </action> </release> <release version="4.0" date="2013-11-27" description=" Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/TransformerUtils.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/TransformerUtils.java?rev=1549021&r1=1549020&r2=1549021&view=diff ============================================================================== --- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/TransformerUtils.java (original) +++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/TransformerUtils.java Sun Dec 8 11:41:03 2013 @@ -26,6 +26,7 @@ import org.apache.commons.collections4.f import org.apache.commons.collections4.functors.EqualPredicate; import org.apache.commons.collections4.functors.ExceptionTransformer; import org.apache.commons.collections4.functors.FactoryTransformer; +import org.apache.commons.collections4.functors.IfTransformer; import org.apache.commons.collections4.functors.InstantiateTransformer; import org.apache.commons.collections4.functors.InvokerTransformer; import org.apache.commons.collections4.functors.MapTransformer; @@ -45,6 +46,7 @@ import org.apache.commons.collections4.f * <li>Predicate - returns the result of the predicate as a Boolean * <li>Factory - returns a new object from a factory * <li>Chained - chains two or more transformers together + * <li>If - calls one transformer or another based on a predicate * <li>Switch - calls one transformer based on one or more predicates * <li>SwitchMap - calls one transformer looked up from a Map * <li>Instantiate - the Class input object is instantiated @@ -211,6 +213,43 @@ public class TransformerUtils { } /** + * Create a new Transformer that calls the transformer if the predicate is true, + * otherwise the input object is returned unchanged. + * + * @param <T> the input / output type + * @param predicate the predicate to switch on + * @param trueTransformer the transformer called if the predicate is true + * @return the transformer + * @throws IllegalArgumentException if either the predicate or transformer is null + * @see org.apache.commons.collections4.functors.IfTransformer + * @since 4.0.1 + */ + public static <T> Transformer<T, T> ifTransformer(final Predicate<? super T> predicate, + final Transformer<? super T, ? extends T> trueTransformer) { + return IfTransformer.ifTransformer(predicate, trueTransformer); + } + + /** + * Create a new Transformer that calls one of two transformers depending + * on the specified predicate. + * + * @param <I> the input type + * @param <O> the output type + * @param predicate the predicate to switch on + * @param trueTransformer the transformer called if the predicate is true + * @param falseTransformer the transformer called if the predicate is false + * @return the transformer + * @throws IllegalArgumentException if either the predicate or transformer is null + * @see org.apache.commons.collections4.functors.IfTransformer + * @since 4.0.1 + */ + public static <I, O> Transformer<I, O> ifTransformer(final Predicate<? super I> predicate, + final Transformer<? super I, ? extends O> trueTransformer, + final Transformer<? super I, ? extends O> falseTransformer) { + return IfTransformer.ifTransformer(predicate, trueTransformer, falseTransformer); + } + + /** * Create a new Transformer that calls one of two transformers depending * on the specified predicate. * @@ -222,8 +261,10 @@ public class TransformerUtils { * @return the transformer * @throws IllegalArgumentException if either the predicate or transformer is null * @see org.apache.commons.collections4.functors.SwitchTransformer + * @deprecated as of 4.0.1, use {@link #ifTransformer(Predicate, Transformer, Transformer)) */ @SuppressWarnings("unchecked") + @Deprecated public static <I, O> Transformer<I, O> switchTransformer(final Predicate<? super I> predicate, final Transformer<? super I, ? extends O> trueTransformer, final Transformer<? super I, ? extends O> falseTransformer) { Added: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/IfTransformer.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/IfTransformer.java?rev=1549021&view=auto ============================================================================== --- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/IfTransformer.java (added) +++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/IfTransformer.java Sun Dec 8 11:41:03 2013 @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.collections4.functors; + +import org.apache.commons.collections4.Predicate; +import org.apache.commons.collections4.Transformer; + +import java.io.Serializable; + +/** + * Transformer implementation that will call one of two closures based on whether a predicate evaluates + * as true or false. + * + * @param <I> The input type for the transformer + * @param <O> The output type for the transformer + * + * @since 4.0.1 + * @version $Id$ + */ +public class IfTransformer<I, O> implements Transformer<I, O>, Serializable { + + /** Serial version UID */ + private static final long serialVersionUID = 8069309411242014252L; + + /** The test */ + private final Predicate<? super I> iPredicate; + /** The transformer to use if true */ + private final Transformer<? super I, ? extends O> iTrueTransformer; + /** The transformer to use if false */ + private final Transformer<? super I, ? extends O> iFalseTransformer; + + /** + * Factory method that performs validation. + * + * @param <I> input type for the transformer + * @param <O> output type for the transformer + * @param predicate predicate to switch on + * @param trueTransformer transformer used if true + * @param falseTransformer transformer used if false + * @return the <code>if</code> transformer + */ + public static <I, O> Transformer<I, O> ifTransformer(final Predicate<? super I> predicate, + final Transformer<? super I, ? extends O> trueTransformer, + final Transformer<? super I, ? extends O> falseTransformer) { + if (predicate == null) { + throw new IllegalArgumentException("Predicate must not be null"); + } + if (trueTransformer == null || falseTransformer == null) { + throw new IllegalArgumentException("Transformers must not be null"); + } + + return new IfTransformer<I, O>(predicate, trueTransformer, falseTransformer); + } + + /** + * Factory method that performs validation. + * <p> + * This factory creates a transformer that just returns the input object when + * the predicate is false. + * + * @param <T> input and output type for the transformer + * @param predicate predicate to switch on + * @param trueTransformer transformer used if true + * @return the <code>if</code> transformer + */ + public static <T> Transformer<T, T> ifTransformer( + final Predicate<? super T> predicate, + final Transformer<? super T, ? extends T> trueTransformer) { + + if (predicate == null) { + throw new IllegalArgumentException("Predicate must not be null"); + } + if (trueTransformer == null) { + throw new IllegalArgumentException("Transformer must not be null"); + } + + return new IfTransformer<T, T>(predicate, trueTransformer, NOPTransformer.<T>nopTransformer()); + } + + /** + * Constructor that performs no validation. + * Use the static factory method <code>ifTransformer</code> if you want that. + * + * @param predicate predicate to switch on, not null + * @param trueTransformer transformer used if true, not null + * @param falseTransformer transformer used if false, not null + */ + public IfTransformer(final Predicate<? super I> predicate, + final Transformer<? super I, ? extends O> trueTransformer, + final Transformer<? super I, ? extends O> falseTransformer) { + + super(); + iPredicate = predicate; + iTrueTransformer = trueTransformer; + iFalseTransformer = falseTransformer; + } + + /** + * Transforms the input using the true or false transformer based to the result of the predicate. + * + * @param input the input object to transform + * @return the transformed result + */ + public O transform(final I input) { + if(iPredicate.evaluate(input)){ + return iTrueTransformer.transform(input); + } else { + return iFalseTransformer.transform(input); + } + } + + /** + * Gets the predicate. + * + * @return the predicate + */ + public Predicate<? super I> getPredicate(){ + return iPredicate; + } + + /** + * Gets the transformer used when true. + * + * @return the transformer + */ + public Transformer<? super I, ? extends O> getTrueTransformer() { + return iTrueTransformer; + } + + /** + * Gets the transformer used when false. + * + * @return the transformer + */ + public Transformer<? super I, ? extends O> getFalseTransformer() { + return iFalseTransformer; + } +} Propchange: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/IfTransformer.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/IfTransformer.java ------------------------------------------------------------------------------ svn:keywords = Id Revision HeadURL Propchange: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/functors/IfTransformer.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/TransformerUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/TransformerUtilsTest.java?rev=1549021&r1=1549020&r2=1549021&view=diff ============================================================================== --- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/TransformerUtilsTest.java (original) +++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/TransformerUtilsTest.java Sun Dec 8 11:41:03 2013 @@ -235,6 +235,49 @@ public class TransformerUtilsTest extend } catch (final IllegalArgumentException ex) {} } + // ifTransformer + //------------------------------------------------------------------ + + public void testIfTransformer() { + final Transformer<Object, String> a = TransformerUtils.constantTransformer("A"); + final Transformer<Object, String> b = TransformerUtils.constantTransformer("B"); + final Transformer<Object, String> c = TransformerUtils.constantTransformer("C"); + + assertEquals("A", TransformerUtils.ifTransformer(TruePredicate.truePredicate(), a, b).transform(null)); + assertEquals("B", TransformerUtils.ifTransformer(FalsePredicate.falsePredicate(), a, b).transform(null)); + + Predicate<Integer> lessThanFivePredicate = new Predicate<Integer>() { + public boolean evaluate(Integer value) { + return value < 5; + } + }; + // if/else tests + assertEquals("A", TransformerUtils.<Integer, String>ifTransformer(lessThanFivePredicate, a, b).transform(1)); + assertEquals("B", TransformerUtils.<Integer, String>ifTransformer(lessThanFivePredicate, a, b).transform(5)); + + // if tests + Predicate<String> equalsAPredicate = EqualPredicate.equalPredicate("A"); + assertEquals("C", TransformerUtils.<String>ifTransformer(equalsAPredicate, c).transform("A")); + assertEquals("B", TransformerUtils.<String>ifTransformer(equalsAPredicate, c).transform("B")); + + try { + TransformerUtils.ifTransformer(null, null); + fail(); + } catch (final IllegalArgumentException ex) {} + try { + TransformerUtils.ifTransformer(TruePredicate.truePredicate(), null); + fail(); + } catch (final IllegalArgumentException ex) {} + try { + TransformerUtils.ifTransformer(null, ConstantTransformer.constantTransformer("A")); + fail(); + } catch (final IllegalArgumentException ex) {} + try { + TransformerUtils.ifTransformer(null, null, null); + fail(); + } catch (final IllegalArgumentException ex) {} + } + // switchTransformer //------------------------------------------------------------------