From 84f48d54e1ef49c9dac532e30ce04785bd6f70e0 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sun, 23 Nov 2003 23:25:33 +0000 Subject: [PATCH] Refactor functors from inner classes to subpackage git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131371 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/collections/TransformerUtils.java | 605 ++---------------- .../functors/ChainedTransformer.java | 165 +++++ .../functors/CloneTransformer.java | 99 +++ .../functors/ClosureTransformer.java | 115 ++++ .../functors/ConstantTransformer.java | 118 ++++ .../functors/FactoryTransformer.java | 113 ++++ .../collections/functors/FunctorUtils.java | 34 +- .../functors/InstantiateTransformer.java | 155 +++++ .../functors/InvokerTransformer.java | 159 +++++ .../collections/functors/MapTransformer.java | 113 ++++ .../collections/functors/NOPTransformer.java | 94 +++ .../functors/PredicateTransformer.java | 114 ++++ .../functors/StringValueTransformer.java | 94 +++ .../functors/SwitchTransformer.java | 183 ++++++ .../collections/TestTransformerUtils.java | 209 ++---- 15 files changed, 1649 insertions(+), 721 deletions(-) create mode 100644 src/java/org/apache/commons/collections/functors/ChainedTransformer.java create mode 100644 src/java/org/apache/commons/collections/functors/CloneTransformer.java create mode 100644 src/java/org/apache/commons/collections/functors/ClosureTransformer.java create mode 100644 src/java/org/apache/commons/collections/functors/ConstantTransformer.java create mode 100644 src/java/org/apache/commons/collections/functors/FactoryTransformer.java create mode 100644 src/java/org/apache/commons/collections/functors/InstantiateTransformer.java create mode 100644 src/java/org/apache/commons/collections/functors/InvokerTransformer.java create mode 100644 src/java/org/apache/commons/collections/functors/MapTransformer.java create mode 100644 src/java/org/apache/commons/collections/functors/NOPTransformer.java create mode 100644 src/java/org/apache/commons/collections/functors/PredicateTransformer.java create mode 100644 src/java/org/apache/commons/collections/functors/StringValueTransformer.java create mode 100644 src/java/org/apache/commons/collections/functors/SwitchTransformer.java diff --git a/src/java/org/apache/commons/collections/TransformerUtils.java b/src/java/org/apache/commons/collections/TransformerUtils.java index 4bf486242..e7d43ff7d 100644 --- a/src/java/org/apache/commons/collections/TransformerUtils.java +++ b/src/java/org/apache/commons/collections/TransformerUtils.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/TransformerUtils.java,v 1.8 2003/11/23 17:48:20 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/TransformerUtils.java,v 1.9 2003/11/23 23:25:33 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -57,15 +57,23 @@ */ package org.apache.commons.collections; -import java.io.Serializable; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.Collection; import java.util.Iterator; import java.util.Map; +import org.apache.commons.collections.functors.ChainedTransformer; +import org.apache.commons.collections.functors.CloneTransformer; +import org.apache.commons.collections.functors.ClosureTransformer; +import org.apache.commons.collections.functors.ConstantTransformer; import org.apache.commons.collections.functors.ExceptionTransformer; -import org.apache.commons.collections.functors.FunctorException; +import org.apache.commons.collections.functors.FactoryTransformer; +import org.apache.commons.collections.functors.InstantiateTransformer; +import org.apache.commons.collections.functors.InvokerTransformer; +import org.apache.commons.collections.functors.MapTransformer; +import org.apache.commons.collections.functors.NOPTransformer; +import org.apache.commons.collections.functors.PredicateTransformer; +import org.apache.commons.collections.functors.StringValueTransformer; +import org.apache.commons.collections.functors.SwitchTransformer; /** * TransformerUtils provides reference implementations and @@ -90,36 +98,13 @@ import org.apache.commons.collections.functors.FunctorException; * All the supplied transformers are Serializable. * * @since Commons Collections 3.0 - * @version $Revision: 1.8 $ $Date: 2003/11/23 17:48:20 $ + * @version $Revision: 1.9 $ $Date: 2003/11/23 23:25:33 $ * * @author Stephen Colebourne * @author James Carman */ public class TransformerUtils { - /** - * A transformer that always returns null - */ - private static final Transformer NULL_TRANSFORMER = new ConstantTransformer(null); - /** - * A transformer that returns the input object - */ - private static final Transformer NOP_TRANSFORMER = new NOPTransformer(); - /** - * A transformer that clones the input object - */ - private static final Transformer CLONE_TRANSFORMER = new CloneTransformer(); - /** - * A transformer that creates an object from a Class - */ - private static final Transformer INSTANTIATE_TRANSFORMER = new InstantiateTransformer(null, null); - - /** - * A transformer that returns a java.lang.String representation - * of the input object. - */ - private static final Transformer STRING_VALUE_TRANSFORMER = new StringValueTransformer(); - /** * This class is not normally instantiated. */ @@ -143,7 +128,7 @@ public class TransformerUtils { * @return the transformer */ public static Transformer nullTransformer() { - return NULL_TRANSFORMER; + return ConstantTransformer.NULL_INSTANCE; } /** @@ -154,7 +139,7 @@ public class TransformerUtils { * @return the transformer */ public static Transformer nopTransformer() { - return NOP_TRANSFORMER; + return NOPTransformer.INSTANCE; } /** @@ -170,7 +155,7 @@ public class TransformerUtils { * @return the transformer */ public static Transformer cloneTransformer() { - return CLONE_TRANSFORMER; + return CloneTransformer.INSTANCE; } /** @@ -181,49 +166,43 @@ public class TransformerUtils { * @return the transformer. */ public static Transformer constantTransformer(Object constantToReturn) { - return new ConstantTransformer(constantToReturn); + return ConstantTransformer.getInstance(constantToReturn); } /** * Creates a Transformer that calls a Closure each time the transformer is used. * The transformer returns the input object. * - * @param closure the closure to run each time in the transformer - * @return the transformer. + * @param closure the closure to run each time in the transformer, not null + * @return the transformer + * @throws IllegalArgumentException if the closure is null */ public static Transformer asTransformer(Closure closure) { - if (closure == null) { - throw new IllegalArgumentException("The closure must not be null"); - } - return new ClosureTransformer(closure); + return ClosureTransformer.getInstance(closure); } /** * Creates a Transformer that calls a Predicate each time the transformer is used. * The transformer will return either Boolean.TRUE or Boolean.FALSE. * - * @param predicate the predicate to run each time in the transformer - * @return the transformer. + * @param predicate the predicate to run each time in the transformer, not null + * @return the transformer + * @throws IllegalArgumentException if the predicate is null */ public static Transformer asTransformer(Predicate predicate) { - if (predicate == null) { - throw new IllegalArgumentException("The predicate must not be null"); - } - return new PredicateTransformer(predicate); + return PredicateTransformer.getInstance(predicate); } /** * Creates a Transformer that calls a Factory each time the transformer is used. * The transformer will return the value returned by the factory. * - * @param factory the factory to run each time in the transformer - * @return the transformer. + * @param factory the factory to run each time in the transformer, not null + * @return the transformer + * @throws IllegalArgumentException if the factory is null */ public static Transformer asTransformer(Factory factory) { - if (factory == null) { - throw new IllegalArgumentException("The factory must not be null"); - } - return new FactoryTransformer(factory); + return FactoryTransformer.getInstance(factory); } /** @@ -236,9 +215,7 @@ public class TransformerUtils { * @throws IllegalArgumentException if either transformer is null */ public static Transformer chainedTransformer(Transformer transformer1, Transformer transformer2) { - Transformer[] trs = new Transformer[] {transformer1, transformer2}; - validate(trs); - return new ChainedTransformer(trs); + return ChainedTransformer.getInstance(transformer1, transformer2); } /** @@ -248,13 +225,10 @@ public class TransformerUtils { * @param transformers an array of transformers to chain * @return the transformer * @throws IllegalArgumentException if the transformers array is null - * @throws IllegalArgumentException if the transformers array has 0 elements * @throws IllegalArgumentException if any transformer in the array is null */ public static Transformer chainedTransformer(Transformer[] transformers) { - Transformer[] trs = copy(transformers); - validate(trs); - return new ChainedTransformer(trs); + return ChainedTransformer.getInstance(transformers); } /** @@ -265,22 +239,10 @@ public class TransformerUtils { * @param transformers a collection of transformers to chain * @return the transformer * @throws IllegalArgumentException if the transformers collection is null - * @throws IllegalArgumentException if the transformers collection is empty * @throws IllegalArgumentException if any transformer in the collection is null */ public static Transformer chainedTransformer(Collection transformers) { - Transformer[] trs = null; - if (transformers == null) { - throw new IllegalArgumentException("The transformer collection must not be null"); - } - // convert to array like this to guarantee iterator() ordering - trs = new Transformer[transformers.size()]; - int i = 0; - for (Iterator it = transformers.iterator(); it.hasNext();) { - trs[i++] = (Transformer) it.next(); - } - validate(trs); - return new ChainedTransformer(trs); + return ChainedTransformer.getInstance(transformers); } /** @@ -295,7 +257,7 @@ public class TransformerUtils { * @throws IllegalArgumentException if either transformer is null */ public static Transformer switchTransformer(Predicate predicate, Transformer trueTransformer, Transformer falseTransformer) { - return switchTransformerInternal(new Predicate[] { predicate }, new Transformer[] { trueTransformer }, falseTransformer); + return SwitchTransformer.getInstance(new Predicate[] { predicate }, new Transformer[] { trueTransformer }, falseTransformer); } /** @@ -313,7 +275,7 @@ public class TransformerUtils { * @throws IllegalArgumentException if the arrays are different sizes */ public static Transformer switchTransformer(Predicate[] predicates, Transformer[] transformers) { - return switchTransformerInternal(copy(predicates), copy(transformers), null); + return SwitchTransformer.getInstance(predicates, transformers, null); } /** @@ -321,11 +283,11 @@ public class TransformerUtils { * on the predicates. The transformer at array location 0 is called if the * predicate at array location 0 returned true. Each predicate is evaluated * until one returns true. If no predicates evaluate to true, the default - * transformer is called. + * transformer is called. If the default transformer is null, null is returned. * * @param predicates an array of predicates to check * @param transformers an array of transformers to call - * @param defaultTransformer the default to call if no predicate matches + * @param defaultTransformer the default to call if no predicate matches, null means return null * @return the transformer * @throws IllegalArgumentException if the either array is null * @throws IllegalArgumentException if the either array has 0 elements @@ -333,7 +295,7 @@ public class TransformerUtils { * @throws IllegalArgumentException if the arrays are different sizes */ public static Transformer switchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) { - return switchTransformerInternal(copy(predicates), copy(transformers), defaultTransformer); + return SwitchTransformer.getInstance(predicates, transformers, defaultTransformer); } /** @@ -356,41 +318,9 @@ public class TransformerUtils { * @throws ClassCastException if the map elements are of the wrong type */ public static Transformer switchTransformer(Map predicatesAndTransformers) { - Transformer[] trs = null; - Predicate[] preds = null; - if (predicatesAndTransformers == null) { - throw new IllegalArgumentException("The predicate and transformer map must not be null"); - } - // convert to array like this to guarantee iterator() ordering - Transformer def = (Transformer) predicatesAndTransformers.remove(null); - int size = predicatesAndTransformers.size(); - trs = new Transformer[size]; - preds = new Predicate[size]; - int i = 0; - for (Iterator it = predicatesAndTransformers.entrySet().iterator(); it.hasNext();) { - Map.Entry entry = (Map.Entry) it.next(); - preds[i] = (Predicate) entry.getKey(); - trs[i] = (Transformer) entry.getValue(); - i++; - } - return switchTransformerInternal(preds, trs, def); + return SwitchTransformer.getInstance(predicatesAndTransformers); } - /** - * Validate input and create transformer - */ - private static Transformer switchTransformerInternal(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) { - validate(predicates); - validate(transformers); - if (predicates.length != transformers.length) { - throw new IllegalArgumentException("The predicate and transformer arrays must be the same size"); - } - if (defaultTransformer == null) { - defaultTransformer = nullTransformer(); - } - return new SwitchTransformer(predicates, transformers, defaultTransformer); - } - /** * Create a new Transformer that uses the input object as a key to find the * transformer to call. @@ -432,7 +362,7 @@ public class TransformerUtils { * @return the transformer */ public static Transformer instantiateTransformer() { - return INSTANTIATE_TRANSFORMER; + return InstantiateTransformer.NO_ARG_INSTANCE; } /** @@ -446,7 +376,7 @@ public class TransformerUtils { * @throws IllegalArgumentException if the paramTypes and args don't match */ public static Transformer instantiateTransformer(Class[] paramTypes, Object[] args) { - return new InstantiateTransformer(paramTypes, args); + return InstantiateTransformer.getInstance(paramTypes, args); } /** @@ -458,10 +388,7 @@ public class TransformerUtils { * @throws IllegalArgumentException if the map is null */ public static Transformer mapTransformer(Map map) { - if (map == null) { - throw new IllegalArgumentException("The map must not be null"); - } - return new MapTransformer(map); + return MapTransformer.getInstance(map); } /** @@ -478,7 +405,7 @@ public class TransformerUtils { * @throws IllegalArgumentException if the methodName is null. */ public static Transformer invokerTransformer(String methodName){ - return new InvokerTransformer(methodName, null, null); + return InvokerTransformer.getInstance(methodName, null, null); } /** @@ -494,7 +421,7 @@ public class TransformerUtils { * @throws IllegalArgumentException if the paramTypes and args don't match */ public static Transformer invokerTransformer(String methodName, Class[] paramTypes, Object[] args){ - return new InvokerTransformer(methodName, paramTypes, args); + return InvokerTransformer.getInstance(methodName, paramTypes, args); } /** @@ -505,447 +432,7 @@ public class TransformerUtils { * @return the transformer */ public static Transformer stringValueTransformer() { - return STRING_VALUE_TRANSFORMER; + return StringValueTransformer.INSTANCE; } - /** - * Copy method - * - * @param predicates the predicates to copy - */ - private static Predicate[] copy(Predicate[] predicates) { - if (predicates == null) { - return null; - } - return (Predicate[]) predicates.clone(); - } - - /** - * Validate method - * - * @param predicates the predicates to validate - */ - private static void validate(Predicate[] predicates) { - if (predicates == null) { - throw new IllegalArgumentException("The predicate array must not be null"); - } - if (predicates.length < 1) { - throw new IllegalArgumentException( - "At least 1 predicate must be specified in the predicate array, size was " + predicates.length); - } - for (int i = 0; i < predicates.length; i++) { - if (predicates[i] == null) { - throw new IllegalArgumentException( - "The predicate array must not contain a null predicate, index " + i + " was null"); - } - } - } - - /** - * Copy method - * - * @param transformers the transformers to copy - */ - private static Transformer[] copy(Transformer[] transformers) { - if (transformers == null) { - return null; - } - return (Transformer[]) transformers.clone(); - } - - /** - * Validate method - * - * @param transformers the transformers to validate - */ - private static void validate(Transformer[] transformers) { - if (transformers == null) { - throw new IllegalArgumentException("The transformer array must not be null"); - } - if (transformers.length < 1) { - throw new IllegalArgumentException( - "At least 1 transformer must be specified in the transformer array, size was " + transformers.length); - } - for (int i = 0; i < transformers.length; i++) { - if (transformers[i] == null) { - throw new IllegalArgumentException( - "The transformer array must not contain a null transformer, index " + i + " was null"); - } - } - } - - // NOPTransformer - //---------------------------------------------------------------------------------- - - /** - * NOPTransformer returns the input object. - */ - private static class NOPTransformer implements Transformer, Serializable { - - /** - * Constructor - */ - private NOPTransformer() { - super(); - } - - /** - * Return the input object - */ - public Object transform(Object input) { - return input; - } - } - - // CloneTransformer - //---------------------------------------------------------------------------------- - - /** - * CloneTransformer returns a clone of the input object. - */ - private static class CloneTransformer implements Transformer, Serializable { - - /** - * Constructor - */ - private CloneTransformer() { - super(); - } - - /** - * Returns a clone of the input object - */ - public Object transform(Object input) { - if (input == null) { - return null; - } - return FactoryUtils.prototypeFactory(input).create(); - } - } - - // ConstantTransformer - //---------------------------------------------------------------------------------- - - /** - * ConstantTransformer returns the same instance each time. - */ - private static class ConstantTransformer implements Transformer, Serializable { - /** The constant to return each time */ - private final Object iConstant; - - /** - * Constructor to store constant. - */ - private ConstantTransformer(Object constant) { - super(); - iConstant = constant; - } - - /** - * Always return constant. - */ - public Object transform(Object input) { - return iConstant; - } - } - - // ClosureTransformer - //---------------------------------------------------------------------------------- - - /** - * ClosureTransformer executes a Closure object. - */ - private static class ClosureTransformer implements Transformer, Serializable { - /** The closure to call each time */ - private final Closure iClosure; - - /** - * Constructor to store closure. - */ - private ClosureTransformer(Closure closure) { - super(); - iClosure = closure; - } - - /** - * Exceute the closure and return the input. - */ - public Object transform(Object input) { - iClosure.execute(input); - return input; - } - } - - // PredicateTransformer - //---------------------------------------------------------------------------------- - - /** - * PredicateTransformer evaluates a Predicate object. - */ - private static class PredicateTransformer implements Transformer, Serializable { - /** The predicate to call each time */ - private final Predicate iPredicate; - - /** - * Constructor to store predicate. - */ - private PredicateTransformer(Predicate predicate) { - super(); - iPredicate = predicate; - } - - /** - * Evaluate the predicate and return the result as a Boolean. - */ - public Object transform(Object input) { - return new Boolean(iPredicate.evaluate(input)); - } - } - - // FactoryTransformer - //---------------------------------------------------------------------------------- - - /** - * FactoryTransformer returns the result of calling a Factory. - */ - private static class FactoryTransformer implements Transformer, Serializable { - /** The factory to call each time */ - private final Factory iFactory; - - /** - * Constructor to store factory. - */ - private FactoryTransformer(Factory factory) { - super(); - iFactory = factory; - } - - /** - * Return the result of calling the factory. - */ - public Object transform(Object input) { - return iFactory.create(); - } - } - - // ChainedTransformer - //---------------------------------------------------------------------------------- - - /** - * ChainedTransformer returns the result of calling a list of transformers. - */ - private static class ChainedTransformer implements Transformer, Serializable { - /** The array of transformers to call */ - private final Transformer[] iTransformers; - - /** - * Constructor to store params. - */ - private ChainedTransformer(Transformer[] transformers) { - super(); - iTransformers = transformers; - } - - /** - * Returns the result of calling a list of transformers. - */ - public Object transform(Object object) { - for (int i = 0; i < iTransformers.length; i++) { - object = iTransformers[i].transform(object); - } - return object; - } - } - - // SwitchTransformer - //---------------------------------------------------------------------------------- - - /** - * SwitchTransformer returns the result of the transformer whose predicate returns true. - */ - private static class SwitchTransformer implements Transformer, Serializable { - /** The array of predicates to switch on */ - private final Predicate[] iPredicates; - /** The array of transformers to call */ - private final Transformer[] iTransformers; - /** The default transformer called if no predicate matches */ - private final Transformer iDefault; - - /** - * Constructor to store params. - */ - private SwitchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) { - super(); - iPredicates = predicates; - iTransformers = transformers; - iDefault = defaultTransformer; - } - - /** - * Returns the result of the transformer whose predicate returns true. - */ - public Object transform(Object input) { - for (int i = 0; i < iPredicates.length; i++) { - if (iPredicates[i].evaluate(input) == true) { - return iTransformers[i].transform(input); - } - } - return iDefault.transform(input); - } - } - - // InstantiateTransformer - //---------------------------------------------------------------------------------- - - /** - * InstantiateTransformer returns the result of instantiating the input Class object. - */ - private static class InstantiateTransformer implements Transformer, Serializable { - /** The array of reflection parameter types */ - private final Class[] iParamTypes; - /** The array of reflection arguments */ - private final Object[] iArgs; - - /** - * Constructor to store params. - */ - private InstantiateTransformer(Class[] paramTypes, Object[] args) { - super(); - if (((paramTypes == null) && (args != null)) - || ((paramTypes != null) && (args == null)) - || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) { - throw new IllegalArgumentException("InstantiateTransformer: The parameter types must match the arguments"); - } - if ((paramTypes == null) && (args == null)) { - iParamTypes = null; - iArgs = null; - } else { - iParamTypes = (Class[]) paramTypes.clone(); - iArgs = (Object[]) args.clone(); - } - } - - /** - * Return the result of instantiating the input Class object. - */ - public Object transform(Object input) { - try { - if (input instanceof Class == false) { - throw new FunctorException( - "InstantiateTransformer: Input object was not an instanceof Class, it was a " - + (input == null ? "null object" : input.getClass().getName())); - } - return FactoryUtils.instantiateFactory((Class) input, iParamTypes, iArgs).create(); - - } catch (IllegalArgumentException ex) { - throw new FunctorException("InstantiateTransformer", ex); - } - } - } - - // MapTransformer - //---------------------------------------------------------------------------------- - - /** - * MapTransformer returns the result by looking up in the map. - */ - private static class MapTransformer implements Transformer, Serializable { - /** The map of data to lookup in */ - private final Map iMap; - - /** - * Constructor to store map. - */ - private MapTransformer(Map map) { - super(); - iMap = map; - } - - /** - * Returns the result by looking up in the map. - */ - public Object transform(Object input) { - return iMap.get(input); - } - } - - // InvokerTransformer - //---------------------------------------------------------------------------------- - - /** - * InvokerTransformer returns the result of invoking the specified method on - * the input object. - */ - private static class InvokerTransformer implements Transformer, Serializable { - /** The method name to call */ - private final String iMethodName; - /** The array of reflection parameter types */ - private final Class[] iParamTypes; - /** The array of reflection arguments */ - private final Object[] iArgs; - - /** - * Constructor. - */ - public InvokerTransformer(String methodName, Class[] paramTypes, Object[] args) { - super(); - if (methodName == null) { - throw new IllegalArgumentException("InvokerTransformer: The method to invoke must not be null"); - } - if (((paramTypes == null) && (args != null)) - || ((paramTypes != null) && (args == null)) - || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) { - throw new IllegalArgumentException("InvokerTransformer: The parameter types must match the arguments"); - } - - iMethodName = methodName; - if ((paramTypes == null) && (args == null)) { - iParamTypes = null; - iArgs = null; - } else { - iParamTypes = (Class[]) paramTypes.clone(); - iArgs = (Object[]) args.clone(); - } - } - - /** - * Invoke the specified method on the input object. - */ - public Object transform(Object input) { - if (input == null) { - return null; - } - try { - Class cls = input.getClass(); - Method method = cls.getMethod(iMethodName, iParamTypes); - return method.invoke(input, iArgs); - - } catch (NoSuchMethodException ex) { - throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' does not exist"); - } catch (IllegalAccessException ex) { - throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' cannot be accessed"); - } catch (InvocationTargetException ex) { - throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' threw an exception", ex); - } - } - } - - // StringValueTransformer - //---------------------------------------------------------------------------------- - - /** - * StringValueTransformer returns a java.lang.String representation - * of the input object using the String.valueOf() method. - */ - private static class StringValueTransformer implements Transformer, Serializable { - - /** - * returns a java.lang.String representation of the input object - * using the String.valueOf() method. - */ - public Object transform(Object input) { - return String.valueOf(input); - } - } - -} \ No newline at end of file +} diff --git a/src/java/org/apache/commons/collections/functors/ChainedTransformer.java b/src/java/org/apache/commons/collections/functors/ChainedTransformer.java new file mode 100644 index 000000000..891e2b8de --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/ChainedTransformer.java @@ -0,0 +1,165 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/ChainedTransformer.java,v 1.1 2003/11/23 23:25:33 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; +import java.util.Collection; +import java.util.Iterator; + +import org.apache.commons.collections.Transformer; + +/** + * Transformer implementation that chains the specifed closures together. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 23:25:33 $ + * + * @author Stephen Colebourne + */ +public class ChainedTransformer implements Transformer, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = 3514945074733160196L; + + /** The transformers to call in turn */ + private final Transformer[] iTransformers; + + /** + * Factory method that performs validation and copies the parameter array. + * + * @param transformers the transformers to chain, copied, no nulls + * @return the chained transformer + * @throws IllegalArgumentException if the transformers array is null + * @throws IllegalArgumentException if any transformer in the array is null + */ + public static Transformer getInstance(Transformer[] transformers) { + FunctorUtils.validate(transformers); + if (transformers.length == 0) { + return NOPTransformer.INSTANCE; + } + transformers = FunctorUtils.copy(transformers); + return new ChainedTransformer(transformers); + } + + /** + * Create a new Transformer that calls each transformer in turn, passing the + * result into the next transformer. The ordering is that of the iterator() + * method on the collection. + * + * @param transformers a collection of transformers to chain + * @return the chained transformer + * @throws IllegalArgumentException if the transformers collection is null + * @throws IllegalArgumentException if any transformer in the collection is null + */ + public static Transformer getInstance(Collection transformers) { + if (transformers == null) { + throw new IllegalArgumentException("Transformer collection must not be null"); + } + if (transformers.size() == 0) { + return NOPTransformer.INSTANCE; + } + // convert to array like this to guarantee iterator() ordering + Transformer[] cmds = new Transformer[transformers.size()]; + int i = 0; + for (Iterator it = transformers.iterator(); it.hasNext();) { + cmds[i++] = (Transformer) it.next(); + } + FunctorUtils.validate(cmds); + return new ChainedTransformer(cmds); + } + + /** + * Factory method that performs validation. + * + * @param transformer1 the first transformer, not null + * @param transformer2 the second transformer, not null + * @return the chained transformer + * @throws IllegalArgumentException if either transformer is null + */ + public static Transformer getInstance(Transformer transformer1, Transformer transformer2) { + if (transformer1 == null || transformer2 == null) { + throw new IllegalArgumentException("Transformers must not be null"); + } + Transformer[] transformers = new Transformer[] { transformer1, transformer2 }; + return new ChainedTransformer(transformers); + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param transformers the transformers to chain, not copied, no nulls + */ + public ChainedTransformer(Transformer[] transformers) { + super(); + iTransformers = transformers; + } + + /** + * Execute a list of transformers. + * + * @param input the input object passed to each transformer + */ + public Object transform(Object object) { + for (int i = 0; i < iTransformers.length; i++) { + object = iTransformers[i].transform(object); + } + return object; + } + +} diff --git a/src/java/org/apache/commons/collections/functors/CloneTransformer.java b/src/java/org/apache/commons/collections/functors/CloneTransformer.java new file mode 100644 index 000000000..b10b7f238 --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/CloneTransformer.java @@ -0,0 +1,99 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/CloneTransformer.java,v 1.1 2003/11/23 23:25:33 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Transformer; + +/** + * Transformer implementation that returns a clone of the input object. + *

+ * Clone is performed using PrototypeFactory.getInstance(input).create(). + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 23:25:33 $ + * + * @author Stephen Colebourne + */ +public class CloneTransformer implements Transformer, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = -8188742709499652567L; + + /** Singleton predicate instance */ + public static final Transformer INSTANCE = new CloneTransformer(); + + /** + * Constructor + */ + private CloneTransformer() { + super(); + } + + /** + * Do nothing + */ + public Object transform(Object input) { + if (input == null) { + return null; + } + return PrototypeFactory.getInstance(input).create(); + } + +} diff --git a/src/java/org/apache/commons/collections/functors/ClosureTransformer.java b/src/java/org/apache/commons/collections/functors/ClosureTransformer.java new file mode 100644 index 000000000..db314a11c --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/ClosureTransformer.java @@ -0,0 +1,115 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/ClosureTransformer.java,v 1.1 2003/11/23 23:25:33 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Closure; +import org.apache.commons.collections.Transformer; + +/** + * Transformer implementation that calls a Closure using the input object + * and then returns the input. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 23:25:33 $ + * + * @author Stephen Colebourne + */ +public class ClosureTransformer implements Transformer, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = 478466901448617286L; + + /** The closure to wrap */ + private final Closure iClosure; + + /** + * Factory method that performs validation. + * + * @param closure the closure to call, not null + * @return the closure transformer + * @throws IllegalArgumentException if the closure is null + */ + public static Transformer getInstance(Closure closure) { + if (closure == null) { + throw new IllegalArgumentException("Closure must not be null"); + } + return new ClosureTransformer(closure); + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param closure the closure to call, not null + */ + public ClosureTransformer(Closure closure) { + super(); + iClosure = closure; + } + + /** + * Call the closure. + */ + public Object transform(Object input) { + iClosure.execute(input); + return input; + } + +} diff --git a/src/java/org/apache/commons/collections/functors/ConstantTransformer.java b/src/java/org/apache/commons/collections/functors/ConstantTransformer.java new file mode 100644 index 000000000..c264b148e --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/ConstantTransformer.java @@ -0,0 +1,118 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/ConstantTransformer.java,v 1.1 2003/11/23 23:25:33 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Transformer; + +/** + * Transformer implementation that returns the same constant each time. + *

+ * No check is made that the object is immutable. In general, only immutable + * objects should use the constant factory. Mutable objects should + * use the prototype factory. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 23:25:33 $ + * + * @author Stephen Colebourne + */ +public class ConstantTransformer implements Transformer, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = 6374440726369055124L; + + /** Returns null each time */ + public static final Transformer NULL_INSTANCE = new ConstantTransformer(null); + + /** The closures to call in turn */ + private final Object iConstant; + + /** + * Transformer method that performs validation. + * + * @param constantToReturn the constant object to return each time in the factory + * @return the constant factory. + */ + public static Transformer getInstance(Object constantToReturn) { + if (constantToReturn == null) { + return NULL_INSTANCE; + } + return new ConstantTransformer(constantToReturn); + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param constantToReturn the constant to return each time + */ + public ConstantTransformer(Object constantToReturn) { + super(); + iConstant = constantToReturn; + } + + /** + * Always return constant. + */ + public Object transform(Object input) { + return iConstant; + } + +} diff --git a/src/java/org/apache/commons/collections/functors/FactoryTransformer.java b/src/java/org/apache/commons/collections/functors/FactoryTransformer.java new file mode 100644 index 000000000..1d89b1f26 --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/FactoryTransformer.java @@ -0,0 +1,113 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/FactoryTransformer.java,v 1.1 2003/11/23 23:25:33 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Factory; +import org.apache.commons.collections.Transformer; + +/** + * Transformer implementation that calls a Factory and returns the result. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 23:25:33 $ + * + * @author Stephen Colebourne + */ +public class FactoryTransformer implements Transformer, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = -6817674502475353160L; + + /** The closure to wrap */ + private final Factory iFactory; + + /** + * Factory method that performs validation. + * + * @param factory the factory to call, not null + * @return the factory transformer + * @throws IllegalArgumentException if the factory is null + */ + public static Transformer getInstance(Factory factory) { + if (factory == null) { + throw new IllegalArgumentException("Factory must not be null"); + } + return new FactoryTransformer(factory); + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param factory the factory to call, not null + */ + public FactoryTransformer(Factory factory) { + super(); + iFactory = factory; + } + + /** + * Call the factory. + */ + public Object transform(Object input) { + return iFactory.create(); + } + +} diff --git a/src/java/org/apache/commons/collections/functors/FunctorUtils.java b/src/java/org/apache/commons/collections/functors/FunctorUtils.java index 0af696fcf..65bad0d8a 100644 --- a/src/java/org/apache/commons/collections/functors/FunctorUtils.java +++ b/src/java/org/apache/commons/collections/functors/FunctorUtils.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/FunctorUtils.java,v 1.2 2003/11/23 22:05:24 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/FunctorUtils.java,v 1.3 2003/11/23 23:25:33 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -62,12 +62,13 @@ import java.util.Iterator; import org.apache.commons.collections.Closure; import org.apache.commons.collections.Predicate; +import org.apache.commons.collections.Transformer; /** * Internal utilities for functors. * * @since Commons Collections 3.0 - * @version $Revision: 1.2 $ $Date: 2003/11/23 22:05:24 $ + * @version $Revision: 1.3 $ $Date: 2003/11/23 23:25:33 $ * * @author Stephen Colebourne */ @@ -181,4 +182,33 @@ class FunctorUtils { } } + /** + * Copy method + * + * @param transformers the transformers to copy + */ + static Transformer[] copy(Transformer[] transformers) { + if (transformers == null) { + return null; + } + return (Transformer[]) transformers.clone(); + } + + /** + * Validate method + * + * @param transformers the transformers to validate + */ + static void validate(Transformer[] transformers) { + if (transformers == null) { + throw new IllegalArgumentException("The transformer array must not be null"); + } + for (int i = 0; i < transformers.length; i++) { + if (transformers[i] == null) { + throw new IllegalArgumentException( + "The transformer array must not contain a null transformer, index " + i + " was null"); + } + } + } + } diff --git a/src/java/org/apache/commons/collections/functors/InstantiateTransformer.java b/src/java/org/apache/commons/collections/functors/InstantiateTransformer.java new file mode 100644 index 000000000..99b5426f7 --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/InstantiateTransformer.java @@ -0,0 +1,155 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/InstantiateTransformer.java,v 1.1 2003/11/23 23:25:33 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +import org.apache.commons.collections.Transformer; + +/** + * Transformer implementation that creates a new object instance by reflection. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 23:25:33 $ + * + * @author Stephen Colebourne + */ +public class InstantiateTransformer implements Transformer, Serializable { + + /** The serial version */ + static final long serialVersionUID = 3786388740793356347L; + + /** Singleton instance that uses the no arg constructor */ + public static final Transformer NO_ARG_INSTANCE = new InstantiateTransformer(); + + /** The constructor parameter types */ + private final Class[] iParamTypes; + /** The constructor arguments */ + private final Object[] iArgs; + + /** + * Transformer method that performs validation. + * + * @param paramTypes the constructor parameter types + * @param args the constructor arguments + */ + public static Transformer getInstance(Class[] paramTypes, Object[] args) { + if (((paramTypes == null) && (args != null)) + || ((paramTypes != null) && (args == null)) + || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) { + throw new IllegalArgumentException("Parameter types must match the arguments"); + } + + if (paramTypes == null || paramTypes.length == 0) { + return NO_ARG_INSTANCE; + } else { + paramTypes = (Class[]) paramTypes.clone(); + args = (Object[]) args.clone(); + } + return new InstantiateTransformer(paramTypes, args); + } + + /** + * Constructor for no arg instance. + */ + private InstantiateTransformer() { + super(); + iParamTypes = null; + iArgs = null; + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param paramTypes the constructor parameter types, not cloned + * @param args the constructor arguments, not cloned + */ + public InstantiateTransformer(Class[] paramTypes, Object[] args) { + super(); + iParamTypes = paramTypes; + iArgs = args; + } + + /** + * Return the result of instantiating the input Class object. + */ + public Object transform(Object input) { + try { + if (input instanceof Class == false) { + throw new FunctorException( + "InstantiateTransformer: Input object was not an instanceof Class, it was a " + + (input == null ? "null object" : input.getClass().getName())); + } + Constructor con = ((Class) input).getConstructor(iParamTypes); + return con.newInstance(iArgs); + + } catch (NoSuchMethodException ex) { + throw new IllegalArgumentException("InstantiateTransformer: The constructor must exist and be public "); + } catch (InstantiationException ex) { + throw new FunctorException("InstantiateTransformer: InstantiationException", ex); + } catch (IllegalAccessException ex) { + throw new FunctorException("InstantiateTransformer: Constructor must be public", ex); + } catch (InvocationTargetException ex) { + throw new FunctorException("InstantiateTransformer: Constructor threw an exception", ex); + } + } + +} diff --git a/src/java/org/apache/commons/collections/functors/InvokerTransformer.java b/src/java/org/apache/commons/collections/functors/InvokerTransformer.java new file mode 100644 index 000000000..b8fbb9c9d --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/InvokerTransformer.java @@ -0,0 +1,159 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/InvokerTransformer.java,v 1.1 2003/11/23 23:25:33 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.apache.commons.collections.Transformer; + +/** + * Transformer implementation that creates a new object instance by reflection. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 23:25:33 $ + * + * @author Stephen Colebourne + */ +public class InvokerTransformer implements Transformer, Serializable { + + /** The serial version */ + static final long serialVersionUID = -8653385846894047688L; + + /** The method name to call */ + private final String iMethodName; + /** The array of reflection parameter types */ + private final Class[] iParamTypes; + /** The array of reflection arguments */ + private final Object[] iArgs; + + /** + * Transformer method that performs validation. + * + * @param paramTypes the constructor parameter types + * @param args the constructor arguments + */ + public static Transformer getInstance(String methodName, Class[] paramTypes, Object[] args) { + if (methodName == null) { + throw new IllegalArgumentException("The method to invoke must not be null"); + } + if (((paramTypes == null) && (args != null)) + || ((paramTypes != null) && (args == null)) + || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) { + throw new IllegalArgumentException("The parameter types must match the arguments"); + } + if (paramTypes == null || paramTypes.length == 0) { + paramTypes = null; + args = null; + } else { + paramTypes = (Class[]) paramTypes.clone(); + args = (Object[]) args.clone(); + } + return new InvokerTransformer(methodName, paramTypes, args); + } + + /** + * Constructor for no arg instance. + * + * @param methodName the method to call + */ + private InvokerTransformer(String methodName) { + super(); + iMethodName = methodName; + iParamTypes = null; + iArgs = null; + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param methodName the method to call + * @param paramTypes the constructor parameter types, not cloned + * @param args the constructor arguments, not cloned + */ + public InvokerTransformer(String methodName, Class[] paramTypes, Object[] args) { + super(); + iMethodName = methodName; + iParamTypes = paramTypes; + iArgs = args; + } + + /** + * Return the result of instantiating the input Class object. + */ + public Object transform(Object input) { + if (input == null) { + return null; + } + try { + Class cls = input.getClass(); + Method method = cls.getMethod(iMethodName, iParamTypes); + return method.invoke(input, iArgs); + + } catch (NoSuchMethodException ex) { + throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' does not exist"); + } catch (IllegalAccessException ex) { + throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' cannot be accessed"); + } catch (InvocationTargetException ex) { + throw new FunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' threw an exception", ex); + } + } + +} diff --git a/src/java/org/apache/commons/collections/functors/MapTransformer.java b/src/java/org/apache/commons/collections/functors/MapTransformer.java new file mode 100644 index 000000000..62014fcc2 --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/MapTransformer.java @@ -0,0 +1,113 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/MapTransformer.java,v 1.1 2003/11/23 23:25:33 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; +import java.util.Map; + +import org.apache.commons.collections.Transformer; + +/** + * Predicate implementation that returns true the first time an object is + * passed into the predicate. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 23:25:33 $ + * + * @author Stephen Colebourne + */ +public final class MapTransformer implements Transformer, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = 862391807045468939L; + + /** The map of data to lookup in */ + private final Map iMap; + + /** + * Factory to create the transformer. + * + * @return the map, not cloned, not null + * @throws IllegalArgumentException if the map is null + */ + public static Transformer getInstance(Map map) { + if (map == null) { + throw new IllegalArgumentException("The map must not be null"); + } + return new MapTransformer(map); + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param map the map to use for lookup, not cloned + */ + private MapTransformer(Map map) { + super(); + iMap = map; + } + + /** + * Returns the result by looking up in the map. + */ + public Object transform(Object input) { + return iMap.get(input); + } + +} diff --git a/src/java/org/apache/commons/collections/functors/NOPTransformer.java b/src/java/org/apache/commons/collections/functors/NOPTransformer.java new file mode 100644 index 000000000..2c6cf86f1 --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/NOPTransformer.java @@ -0,0 +1,94 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/NOPTransformer.java,v 1.1 2003/11/23 23:25:33 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Transformer; + +/** + * Transformer implementation that does nothing. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 23:25:33 $ + * + * @author Stephen Colebourne + */ +public class NOPTransformer implements Transformer, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = 2133891748318574490L; + + /** Singleton predicate instance */ + public static final Transformer INSTANCE = new NOPTransformer(); + + /** + * Constructor + */ + private NOPTransformer() { + super(); + } + + /** + * Do nothing + */ + public Object transform(Object input) { + return input; + } + +} diff --git a/src/java/org/apache/commons/collections/functors/PredicateTransformer.java b/src/java/org/apache/commons/collections/functors/PredicateTransformer.java new file mode 100644 index 000000000..281e8e9f0 --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/PredicateTransformer.java @@ -0,0 +1,114 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/PredicateTransformer.java,v 1.1 2003/11/23 23:25:33 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Predicate; +import org.apache.commons.collections.Transformer; + +/** + * Transformer implementation that calls a Predicate using the input object + * and then returns the input. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 23:25:33 $ + * + * @author Stephen Colebourne + */ +public class PredicateTransformer implements Transformer, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = 5278818408044349346L; + + /** The closure to wrap */ + private final Predicate iPredicate; + + /** + * Factory method that performs validation. + * + * @param predicate the predicate to call, not null + * @return the predicate transformer + * @throws IllegalArgumentException if the predicate is null + */ + public static Transformer getInstance(Predicate predicate) { + if (predicate == null) { + throw new IllegalArgumentException("Predicate must not be null"); + } + return new PredicateTransformer(predicate); + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param predicate the predicate to call, not null + */ + public PredicateTransformer(Predicate predicate) { + super(); + iPredicate = predicate; + } + + /** + * Call the predicate. + */ + public Object transform(Object input) { + return (iPredicate.evaluate(input) ? Boolean.TRUE : Boolean.FALSE); + } + +} diff --git a/src/java/org/apache/commons/collections/functors/StringValueTransformer.java b/src/java/org/apache/commons/collections/functors/StringValueTransformer.java new file mode 100644 index 000000000..cda74fe15 --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/StringValueTransformer.java @@ -0,0 +1,94 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/StringValueTransformer.java,v 1.1 2003/11/23 23:25:33 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Transformer; + +/** + * Transformer implementation that returns the String.valueof. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 23:25:33 $ + * + * @author Stephen Colebourne + */ +public final class StringValueTransformer implements Transformer, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = 7511110693171758606L; + + /** Singleton predicate instance */ + public static final Transformer INSTANCE = new StringValueTransformer(); + + /** + * Restricted constructor. + */ + private StringValueTransformer() { + super(); + } + + /** + * Return the String.valueOf for the object. + */ + public Object transform(Object input) { + return String.valueOf(input); + } + +} diff --git a/src/java/org/apache/commons/collections/functors/SwitchTransformer.java b/src/java/org/apache/commons/collections/functors/SwitchTransformer.java new file mode 100644 index 000000000..215521e9d --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/SwitchTransformer.java @@ -0,0 +1,183 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/SwitchTransformer.java,v 1.1 2003/11/23 23:25:33 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; +import java.util.Iterator; +import java.util.Map; + +import org.apache.commons.collections.Predicate; +import org.apache.commons.collections.Transformer; + +/** + * Transformer implementation calls the transformer whose predicate returns true, + * like a switch statement. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 23:25:33 $ + * + * @author Stephen Colebourne + */ +public class SwitchTransformer implements Transformer, Serializable { + + /** Serial version UID */ + + + /** The tests to consider */ + private final Predicate[] iPredicates; + /** The matching transformers to call */ + private final Transformer[] iTransformers; + /** The default transformer to call if no tests match */ + private final Transformer iDefault; + + /** + * Factory method that performs validation and copies the parameter arrays. + * + * @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 nop + * @return the chained transformer + * @throws IllegalArgumentException if array is null + * @throws IllegalArgumentException if any element in the array is null + */ + public static Transformer getInstance(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) { + FunctorUtils.validate(predicates); + FunctorUtils.validate(transformers); + if (predicates.length != transformers.length) { + throw new IllegalArgumentException("The predicate and transformer arrays must be the same size"); + } + if (predicates.length == 0) { + return (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer); + } + predicates = FunctorUtils.copy(predicates); + transformers = FunctorUtils.copy(transformers); + return new SwitchTransformer(predicates, transformers, defaultTransformer); + } + + /** + * Create a new Transformer that calls one of the transformers depending + * on the predicates. + *

+ * The Map consists of Predicate keys and Transformer values. A transformer + * is called if its matching predicate returns true. Each predicate is evaluated + * until one returns true. If no predicates evaluate to true, the default + * transformer is called. The default transformer is set in the map with a + * null key. The ordering is that of the iterator() method on the entryset + * collection of the map. + * + * @param predicatesAndTransformers a map of predicates to transformers + * @return the switch transformer + * @throws IllegalArgumentException if the map is null + * @throws IllegalArgumentException if any transformer in the map is null + * @throws ClassCastException if the map elements are of the wrong type + */ + public static Transformer getInstance(Map predicatesAndTransformers) { + Transformer[] transformers = null; + Predicate[] preds = null; + if (predicatesAndTransformers == null) { + throw new IllegalArgumentException("The predicate and transformer map must not be null"); + } + if (predicatesAndTransformers.size() == 0) { + return ConstantTransformer.NULL_INSTANCE; + } + // convert to array like this to guarantee iterator() ordering + Transformer defaultTransformer = (Transformer) predicatesAndTransformers.remove(null); + int size = predicatesAndTransformers.size(); + if (size == 0) { + return (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer); + } + transformers = new Transformer[size]; + preds = new Predicate[size]; + int i = 0; + for (Iterator it = predicatesAndTransformers.entrySet().iterator(); it.hasNext();) { + Map.Entry entry = (Map.Entry) it.next(); + preds[i] = (Predicate) entry.getKey(); + transformers[i] = (Transformer) entry.getValue(); + i++; + } + return new SwitchTransformer(preds, transformers, defaultTransformer); + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param predicates array of predicates, not cloned, no nulls + * @param transformers matching array of transformers, not cloned, no nulls + * @param defaultTransformer the transformer to use if no match, null means nop + */ + public SwitchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) { + super(); + iPredicates = predicates; + iTransformers = transformers; + iDefault = (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer); + } + + /** + * Execute the transformer whose predicate returns true. + */ + public Object transform(Object input) { + for (int i = 0; i < iPredicates.length; i++) { + if (iPredicates[i].evaluate(input) == true) { + return iTransformers[i].transform(input); + } + } + return iDefault.transform(input); + } + +} diff --git a/src/test/org/apache/commons/collections/TestTransformerUtils.java b/src/test/org/apache/commons/collections/TestTransformerUtils.java index f66cdc239..23375747d 100644 --- a/src/test/org/apache/commons/collections/TestTransformerUtils.java +++ b/src/test/org/apache/commons/collections/TestTransformerUtils.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestTransformerUtils.java,v 1.5 2003/11/23 14:41:27 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestTransformerUtils.java,v 1.6 2003/11/23 23:25:33 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -68,13 +68,15 @@ import junit.framework.Test; import junit.framework.TestSuite; import junit.textui.TestRunner; +import org.apache.commons.collections.functors.ConstantTransformer; import org.apache.commons.collections.functors.FunctorException; +import org.apache.commons.collections.functors.NOPTransformer; /** * Tests the org.apache.commons.collections.TransformerUtils class. * * @since Commons Collections 3.0 - * @version $Revision: 1.5 $ $Date: 2003/11/23 14:41:27 $ + * @version $Revision: 1.6 $ $Date: 2003/11/23 23:25:33 $ * * @author Stephen Colebourne * @author James Carman @@ -262,72 +264,33 @@ public class TestTransformerUtils extends junit.framework.TestCase { coll.add(b); coll.add(a); assertEquals("A", TransformerUtils.chainedTransformer(coll).transform(null)); - } - public void testChainedTransformerEx1a() { + assertSame(NOPTransformer.INSTANCE, TransformerUtils.chainedTransformer(new Transformer[0])); + assertSame(NOPTransformer.INSTANCE, TransformerUtils.chainedTransformer(Collections.EMPTY_LIST)); + try { TransformerUtils.chainedTransformer(null, null); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testChainedTransformerEx1b() { + fail(); + } catch (IllegalArgumentException ex) {} try { TransformerUtils.chainedTransformer((Transformer[]) null); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testChainedTransformerEx1c() { + fail(); + } catch (IllegalArgumentException ex) {} try { TransformerUtils.chainedTransformer((Collection) null); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testChainedTransformerEx2() { - try { - TransformerUtils.chainedTransformer(new Transformer[0]); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testChainedTransformerEx3() { + fail(); + } catch (IllegalArgumentException ex) {} try { TransformerUtils.chainedTransformer(new Transformer[] {null, null}); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testChainedTransformerEx4() { + fail(); + } catch (IllegalArgumentException ex) {} try { - TransformerUtils.chainedTransformer(Collections.EMPTY_LIST); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testChainedTransformerEx5() { - try { - Collection coll = new ArrayList(); + coll = new ArrayList(); coll.add(null); coll.add(null); TransformerUtils.chainedTransformer(coll); - } catch (IllegalArgumentException ex) { - return; - } - fail(); + fail(); + } catch (IllegalArgumentException ex) {} } // switchTransformer @@ -363,81 +326,33 @@ public class TestTransformerUtils extends junit.framework.TestCase { assertEquals("B", TransformerUtils.switchTransformer(map).transform("THERE")); map.put(null, c); assertEquals("C", TransformerUtils.switchTransformer(map).transform("WELL")); - } - public void testSwitchTransformerEx1a() { + assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchTransformer(new Predicate[0], new Transformer[0])); + assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchTransformer(new HashMap())); + map = new HashMap(); + map.put(null, null); + assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchTransformer(map)); + try { TransformerUtils.switchTransformer(null, null); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testSwitchTransformerEx1b() { + fail(); + } catch (IllegalArgumentException ex) {} try { TransformerUtils.switchTransformer((Predicate[]) null, (Transformer[]) null); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testSwitchTransformerEx1c() { + fail(); + } catch (IllegalArgumentException ex) {} try { TransformerUtils.switchTransformer((Map) null); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testSwitchTransformerEx2() { - try { - TransformerUtils.switchTransformer(new Predicate[0], new Transformer[0]); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testSwitchTransformerEx3() { + fail(); + } catch (IllegalArgumentException ex) {} try { TransformerUtils.switchTransformer(new Predicate[2], new Transformer[2]); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testSwitchTransformerEx4() { - try { - TransformerUtils.switchTransformer(new HashMap()); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testSwitchTransformerEx5() { - try { - Map map = new HashMap(); - map.put(null, null); - map.put(null, null); - TransformerUtils.switchTransformer(map); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testSwitchTransformerEx6() { + fail(); + } catch (IllegalArgumentException ex) {} try { TransformerUtils.switchTransformer(new Predicate[2], new Transformer[1]); - } catch (IllegalArgumentException ex) { - return; - } - fail(); + fail(); + } catch (IllegalArgumentException ex) {} } // switchMapTransformer @@ -456,24 +371,16 @@ public class TestTransformerUtils extends junit.framework.TestCase { assertEquals("B", TransformerUtils.switchMapTransformer(map).transform("THERE")); map.put(null, c); assertEquals("C", TransformerUtils.switchMapTransformer(map).transform("WELL")); - } - public void testSwitchMapTransformerEx1() { + assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchMapTransformer(new HashMap())); + map = new HashMap(); + map.put(null, null); + assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchMapTransformer(map)); + try { TransformerUtils.switchMapTransformer(null); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testSwitchMapTransformerEx2() { - try { - TransformerUtils.switchMapTransformer(new HashMap()); - } catch (IllegalArgumentException ex) { - return; - } - fail(); + fail(); + } catch (IllegalArgumentException ex) {} } // invokerTransformer @@ -485,24 +392,15 @@ public class TestTransformerUtils extends junit.framework.TestCase { list.add(new Object()); assertEquals(new Integer(1), TransformerUtils.invokerTransformer("size").transform(list)); assertEquals(null, TransformerUtils.invokerTransformer("size").transform(null)); - } - public void testInvokerTransformerEx1() { try { TransformerUtils.invokerTransformer(null); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testInvokerTransformerEx3() { + fail(); + } catch (IllegalArgumentException ex) {} try { TransformerUtils.invokerTransformer("noSuchMethod").transform(new Object()); - } catch (FunctorException ex) { - return; - } - fail(); + fail(); + } catch (FunctorException ex) {} } // invokerTransformer2 @@ -517,25 +415,16 @@ public class TestTransformerUtils extends junit.framework.TestCase { "contains", new Class[] {Object.class}, new Object[] {cString}).transform(list)); assertEquals(null, TransformerUtils.invokerTransformer( "contains", new Class[] {Object.class}, new Object[] {cString}).transform(null)); - } - public void testInvokerTransformer2Ex1() { try { TransformerUtils.invokerTransformer(null, null, null); - } catch (IllegalArgumentException ex) { - return; - } - fail(); - } - - public void testInvokerTransformer2Ex3() { + fail(); + } catch (IllegalArgumentException ex) {} try { TransformerUtils.invokerTransformer( "noSuchMethod", new Class[] {Object.class}, new Object[] {cString}).transform(new Object()); - } catch (FunctorException ex) { - return; - } - fail(); + fail(); + } catch (FunctorException ex) {} } // stringValueTransformer