📄 transformerutils.java
字号:
* @throws IllegalArgumentException if the transformers collection is null
* @throws IllegalArgumentException if any transformer in the collection is null
*/
public static Transformer chainedTransformer(Collection transformers) {
return ChainedTransformer.getInstance(transformers);
}
/**
* Create a new Transformer that calls one of two transformers depending
* on the specified predicate.
*
* @see org.apache.commons.collections.functors.SwitchTransformer
*
* @param predicate the predicate to switch on
* @param trueTransformer the transformer called if the predicate is true
* @param falseTransformer the transformer called if the predicate is false
* @return the transformer
* @throws IllegalArgumentException if the predicate is null
* @throws IllegalArgumentException if either transformer is null
*/
public static Transformer switchTransformer(Predicate predicate, Transformer trueTransformer, Transformer falseTransformer) {
return SwitchTransformer.getInstance(new Predicate[] { predicate }, new Transformer[] { 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.
*
* @see org.apache.commons.collections.functors.SwitchTransformer
*
* @param predicates an array of predicates to check
* @param transformers an array of transformers to call
* @return the transformer
* @throws IllegalArgumentException if the either array is null
* @throws IllegalArgumentException if the either array has 0 elements
* @throws IllegalArgumentException if any element in the arrays is null
* @throws IllegalArgumentException if the arrays are different sizes
*/
public static Transformer switchTransformer(Predicate[] predicates, Transformer[] transformers) {
return SwitchTransformer.getInstance(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.
*
* @see org.apache.commons.collections.functors.SwitchTransformer
*
* @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 IllegalArgumentException if the either array is null
* @throws IllegalArgumentException if the either array has 0 elements
* @throws IllegalArgumentException if any element in the arrays is null
* @throws IllegalArgumentException if the arrays are different sizes
*/
public static Transformer switchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) {
return SwitchTransformer.getInstance(predicates, transformers, defaultTransformer);
}
/**
* Create a new Transformer that calls one of the transformers depending
* on the predicates.
* <p>
* 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.
*
* @see org.apache.commons.collections.functors.SwitchTransformer
*
* @param predicatesAndTransformers a map of predicates to transformers
* @return the transformer
* @throws IllegalArgumentException if the map is null
* @throws IllegalArgumentException if the map is empty
* @throws IllegalArgumentException if any transformer in the map is null
* @throws ClassCastException if the map elements are of the wrong type
*/
public static Transformer switchTransformer(Map predicatesAndTransformers) {
return SwitchTransformer.getInstance(predicatesAndTransformers);
}
/**
* Create a new Transformer that uses the input object as a key to find the
* transformer to call.
* <p>
* 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.
*
* @see org.apache.commons.collections.functors.SwitchTransformer
*
* @param objectsAndTransformers a map of objects to transformers
* @return the transformer
* @throws IllegalArgumentException if the map is null
* @throws IllegalArgumentException if the map is empty
* @throws IllegalArgumentException if any transformer in the map is null
*/
public static Transformer switchMapTransformer(Map objectsAndTransformers) {
Transformer[] trs = null;
Predicate[] preds = null;
if (objectsAndTransformers == null) {
throw new IllegalArgumentException("The object and transformer map must not be null");
}
Transformer def = (Transformer) objectsAndTransformers.remove(null);
int size = objectsAndTransformers.size();
trs = new Transformer[size];
preds = new Predicate[size];
int i = 0;
for (Iterator it = objectsAndTransformers.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
preds[i] = EqualPredicate.getInstance(entry.getKey());
trs[i] = (Transformer) entry.getValue();
i++;
}
return switchTransformer(preds, trs, def);
}
/**
* Gets a Transformer that expects an input Class object that it will instantiate.
*
* @see org.apache.commons.collections.functors.InstantiateTransformer
*
* @return the transformer
*/
public static Transformer instantiateTransformer() {
return InstantiateTransformer.NO_ARG_INSTANCE;
}
/**
* 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.
*
* @see org.apache.commons.collections.functors.InstantiateTransformer
*
* @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
*/
public static Transformer instantiateTransformer(Class[] paramTypes, Object[] args) {
return InstantiateTransformer.getInstance(paramTypes, args);
}
/**
* Creates a Transformer that uses the passed in Map to transform the input
* object (as a simple lookup).
*
* @see org.apache.commons.collections.functors.MapTransformer
*
* @param map the map to use to transform the objects
* @return the transformer
* @throws IllegalArgumentException if the map is null
*/
public static Transformer mapTransformer(Map map) {
return MapTransformer.getInstance(map);
}
/**
* Gets a Transformer that invokes a method on the input object.
* The method must have no parameters. If the input object is null,
* null is returned.
* <p>
* For example, <code>TransformerUtils.invokerTransformer("getName");</code>
* will call the <code>getName/code> method on the input object to
* determine the transformer result.
*
* @see org.apache.commons.collections.functors.InvokerTransformer
*
* @param methodName the method name to call on the input object, may not be null
* @return the transformer
* @throws IllegalArgumentException if the methodName is null.
*/
public static Transformer invokerTransformer(String methodName){
return InvokerTransformer.getInstance(methodName, null, null);
}
/**
* Gets a Transformer that invokes a method on the input object.
* The method parameters are specified. If the input object is null,
* null is returned.
*
* @see org.apache.commons.collections.functors.InvokerTransformer
*
* @param methodName the name of the method
* @param paramTypes the parameter types
* @param args the arguments
* @return the transformer
* @throws IllegalArgumentException if the method name is null
* @throws IllegalArgumentException if the paramTypes and args don't match
*/
public static Transformer invokerTransformer(String methodName, Class[] paramTypes, Object[] args){
return InvokerTransformer.getInstance(methodName, paramTypes, args);
}
/**
* Gets a transformer that returns a <code>java.lang.String</code>
* representation of the input object. This is achieved via the
* <code>toString</code> method, <code>null</code> returns 'null'.
*
* @see org.apache.commons.collections.functors.StringValueTransformer
*
* @return the transformer
*/
public static Transformer stringValueTransformer() {
return StringValueTransformer.INSTANCE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -