com.feilong.lib.collection4.TransformerUtils Maven / Gradle / Ivy
Show all versions of feilong Show documentation
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.feilong.lib.collection4;
import java.util.Collection;
import java.util.Map;
import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.Transformer;
import com.feilong.lib.collection4.functors.ChainedTransformer;
import com.feilong.lib.collection4.functors.ClosureTransformer;
import com.feilong.lib.collection4.functors.ConstantTransformer;
import com.feilong.lib.collection4.functors.EqualPredicate;
import com.feilong.lib.collection4.functors.ExceptionTransformer;
import com.feilong.lib.collection4.functors.IfTransformer;
import com.feilong.lib.collection4.functors.InstantiateTransformer;
import com.feilong.lib.collection4.functors.InvokerTransformer;
import com.feilong.lib.collection4.functors.MapTransformer;
import com.feilong.lib.collection4.functors.NOPTransformer;
import com.feilong.lib.collection4.functors.PredicateTransformer;
import com.feilong.lib.collection4.functors.StringValueTransformer;
import com.feilong.lib.collection4.functors.SwitchTransformer;
/**
* TransformerUtils
provides reference implementations and
* utilities for the Transformer functor interface. The supplied transformers are:
*
* - Invoker - returns the result of a method call on the input object
*
- Clone - returns a clone of the input object
*
- Constant - always returns the same object
*
- Closure - performs a Closure and returns the input object
*
- Predicate - returns the result of the predicate as a Boolean
*
- Factory - returns a new object from a factory
*
- Chained - chains two or more transformers together
*
- If - calls one transformer or another based on a predicate
*
- Switch - calls one transformer based on one or more predicates
*
- SwitchMap - calls one transformer looked up from a Map
*
- Instantiate - the Class input object is instantiated
*
- Map - returns an object from a supplied Map
*
- Null - always returns null
*
- NOP - returns the input object, which should be immutable
*
- Exception - always throws an exception
*
- StringValue - returns a
java.lang.String
representation of the input object
*
*
* Since v4.1 only transformers which are considered to be safe are
* Serializable. Transformers considered to be unsafe for serialization are:
*
*
* - Invoker
*
- Clone
*
- Instantiate
*
*
* @since 3.0
*/
public class TransformerUtils{
/**
* This class is not normally instantiated.
*/
private TransformerUtils(){
}
/**
* Gets a transformer that always throws an exception.
* This could be useful during testing as a placeholder.
*
* @param
* the input type
* @param
* the output type
* @return the transformer
* @see ExceptionTransformer
*/
public static Transformer exceptionTransformer(){
return ExceptionTransformer.exceptionTransformer();
}
/**
* Gets a transformer that always returns null.
*
* @param
* the input type
* @param
* the output type
* @return the transformer
* @see ConstantTransformer
*/
public static Transformer nullTransformer(){
return ConstantTransformer.nullTransformer();
}
/**
* Gets a transformer that returns the input object.
* The input object should be immutable to maintain the
* contract of Transformer (although this is not checked).
*
* @param
* the input/output type
* @return the transformer
* @see NOPTransformer
*/
public static Transformer nopTransformer(){
return NOPTransformer.nopTransformer();
}
/**
* Creates a Transformer that will return the same object each time the
* transformer is used.
*
* @param
* the input type
* @param
* the output type
* @param constantToReturn
* the constant object to return each time in the transformer
* @return the transformer.
* @see ConstantTransformer
*/
public static Transformer constantTransformer(final O constantToReturn){
return ConstantTransformer.constantTransformer(constantToReturn);
}
/**
* Creates a Transformer that calls a Closure each time the transformer is used.
* The transformer returns the input object.
*
* @param
* the input/output type
* @param closure
* the closure to run each time in the transformer, not null
* @return the transformer
* @throws NullPointerException
* if the closure is null
* @see ClosureTransformer
*/
public static Transformer asTransformer(final Closure closure){
return ClosureTransformer.closureTransformer(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
* the input type
* @param predicate
* the predicate to run each time in the transformer, not null
* @return the transformer
* @throws NullPointerException
* if the predicate is null
* @see PredicateTransformer
*/
public static Transformer asTransformer(final Predicate predicate){
return PredicateTransformer.predicateTransformer(predicate);
}
/**
* Create a new Transformer that calls each transformer in turn, passing the
* result into the next transformer.
*
* @param
* the input/output type
* @param transformers
* an array of transformers to chain
* @return the transformer
* @throws NullPointerException
* if the transformers array or any of the transformers is null
* @see ChainedTransformer
*/
public static Transformer chainedTransformer(final Transformer...transformers){
return ChainedTransformer.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
* the input/output type
* @param transformers
* a collection of transformers to chain
* @return the transformer
* @throws NullPointerException
* if the transformers collection or any of the transformers is null
* @see ChainedTransformer
*/
public static Transformer chainedTransformer(final Collection> transformers){
return ChainedTransformer.chainedTransformer(transformers);
}
/**
* Create a new Transformer that calls the transformer if the predicate is true,
* otherwise the input object is returned unchanged.
*
* @param
* the input / output type
* @param predicate
* the predicate to switch on
* @param trueTransformer
* the transformer called if the predicate is true
* @return the transformer
* @throws NullPointerException
* if either the predicate or transformer is null
* @see IfTransformer
* @since 4.1
*/
public static Transformer ifTransformer(
final Predicate predicate,
final Transformer trueTransformer){
return IfTransformer.ifTransformer(predicate, trueTransformer);
}
/**
* Create a new Transformer that calls one of two transformers depending
* on the specified predicate.
*
* @param
* the input type
* @param
* the output type
* @param predicate
* the predicate to switch on
* @param trueTransformer
* the transformer called if the predicate is true
* @param falseTransformer
* the transformer called if the predicate is false
* @return the transformer
* @throws NullPointerException
* if either the predicate or transformer is null
* @see IfTransformer
* @since 4.1
*/
public static Transformer ifTransformer(
final Predicate predicate,
final Transformer trueTransformer,
final Transformer falseTransformer){
return IfTransformer.ifTransformer(predicate, trueTransformer, falseTransformer);
}
/**
* Create a new Transformer that calls one of the transformers depending
* 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, null is returned.
*
* @param
* the input type
* @param
* the output type
* @param predicates
* an array of predicates to check
* @param transformers
* an array of transformers to call
* @return the transformer
* @throws NullPointerException
* if the either array is null
* @throws NullPointerException
* if any element in the arrays is null
* @throws IllegalArgumentException
* if the arrays have different sizes
* @see SwitchTransformer
*/
public static Transformer switchTransformer(
final Predicate[] predicates,
final Transformer[] transformers){
return SwitchTransformer.switchTransformer(predicates, transformers, null);
}
/**
* Create a new Transformer that calls one of the transformers depending
* 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. If the default transformer is null, null is returned.
*
* @param
* the input type
* @param
* the output type
* @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, null means return null
* @return the transformer
* @throws NullPointerException
* if the either array is null
* @throws NullPointerException
* if any element in the arrays is null
* @throws IllegalArgumentException
* if the arrays have different sizes
* @see SwitchTransformer
*/
public static Transformer switchTransformer(
final Predicate[] predicates,
final Transformer[] transformers,
final Transformer defaultTransformer){
return SwitchTransformer.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. If no default transformer is set, null will be returned in a default
* case. The ordering is that of the iterator() method on the entryset collection
* of the map.
*
* @param
* the input type
* @param
* the output type
* @param predicatesAndTransformers
* a map of predicates to transformers
* @return the transformer
* @throws NullPointerException
* if the map is null
* @throws NullPointerException
* if any transformer in the map is null
* @throws ClassCastException
* if the map elements are of the wrong type
* @see SwitchTransformer
*/
public static Transformer switchTransformer(final Map, Transformer> predicatesAndTransformers){
return SwitchTransformer.switchTransformer(predicatesAndTransformers);
}
/**
* Create a new Transformer that uses the input object as a key to find the
* transformer to call.
*
* The Map consists of object keys and Transformer values. A transformer
* is called if the input object equals the key. If there is no match, the
* default transformer is called. The default transformer is set in the map
* using a null key. If no default is set, null will be returned in a default case.
*
* @param
* the input type
* @param
* the output type
* @param objectsAndTransformers
* a map of objects to transformers
* @return the transformer
* @throws NullPointerException
* if the map is null
* @throws NullPointerException
* if any transformer in the map is null
* @see SwitchTransformer
*/
@SuppressWarnings("unchecked")
public static Transformer switchMapTransformer(final Map> objectsAndTransformers){
if (objectsAndTransformers == null){
throw new NullPointerException("The object and transformer map must not be null");
}
final Transformer def = objectsAndTransformers.remove(null);
final int size = objectsAndTransformers.size();
final Transformer[] trs = new Transformer[size];
final Predicate[] preds = new Predicate[size];
int i = 0;
for (final Map.Entry> entry : objectsAndTransformers.entrySet()){
preds[i] = EqualPredicate. equalPredicate(entry.getKey());
trs[i++] = entry.getValue();
}
return TransformerUtils.switchTransformer(preds, trs, def);
}
/**
* Gets a Transformer that expects an input Class object that it will instantiate.
*
* @param
* the output type
* @return the transformer
* @see InstantiateTransformer
*/
public static Transformer, T> instantiateTransformer(){
return InstantiateTransformer.instantiateTransformer();
}
/**
* Creates a Transformer that expects an input Class object that it will
* instantiate. The constructor used is determined by the arguments specified
* to this method.
*
* @param
* the output type
* @param paramTypes
* parameter types for the constructor, can be null
* @param args
* the arguments to pass to the constructor, can be null
* @return the transformer
* @throws IllegalArgumentException
* if the paramTypes and args don't match
* @see InstantiateTransformer
*/
public static Transformer, T> instantiateTransformer(final Class[] paramTypes,final Object[] args){
return InstantiateTransformer.instantiateTransformer(paramTypes, args);
}
/**
* Creates a Transformer that uses the passed in Map to transform the input
* object (as a simple lookup).
*
* @param
* the input type
* @param
* the output type
* @param map
* the map to use to transform the objects
* @return the transformer, or {@link ConstantTransformer#nullTransformer()} if the
* {@code map} is {@code null}
* @see MapTransformer
*/
public static Transformer mapTransformer(final Map map){
return MapTransformer.mapTransformer(map);
}
/**
* Gets a Transformer that invokes a method on the input object.
* The method must have no parameters. If the input object is {@code null},
* {@code null} is returned.
*
*
* For example, TransformerUtils.invokerTransformer("getName");
* will call the getName
method on the input object to
* determine the transformer result.
*
*
* @param
* the input type
* @param
* the output type
* @param methodName
* the method name to call on the input object, may not be null
* @return the transformer
* @throws NullPointerException
* if the methodName is null.
* @see InvokerTransformer
*/
public static Transformer invokerTransformer(final String methodName){
return InvokerTransformer.invokerTransformer(methodName, null, null);
}
/**
* Gets a Transformer that invokes a method on the input object.
* The method parameters are specified. If the input object is {@code null},
* {@code null} is returned.
*
* @param
* the input type
* @param
* the output type
* @param methodName
* the name of the method
* @param paramTypes
* the parameter types
* @param args
* the arguments
* @return the transformer
* @throws NullPointerException
* if the method name is null
* @throws IllegalArgumentException
* if the paramTypes and args don't match
* @see InvokerTransformer
*/
public static Transformer invokerTransformer(final String methodName,final Class[] paramTypes,final Object[] args){
return InvokerTransformer.invokerTransformer(methodName, paramTypes, args);
}
/**
* Gets a transformer that returns a java.lang.String
* representation of the input object. This is achieved via the
* toString
method, null
returns 'null'.
*
* @param
* the input type
* @return the transformer
* @see StringValueTransformer
*/
public static Transformer stringValueTransformer(){
return StringValueTransformer.stringValueTransformer();
}
}