📄 closureutils.java
字号:
*
* @see org.apache.commons.collections.functors.ChainedClosure
*
* @param closure1 the first closure
* @param closure2 the second closure
* @return the <code>chained</code> closure
* @throws IllegalArgumentException if either closure is null
*/
public static Closure chainedClosure(Closure closure1, Closure closure2) {
return ChainedClosure.getInstance(closure1, closure2);
}
/**
* Create a new Closure that calls each closure in turn, passing the
* result into the next closure.
*
* @see org.apache.commons.collections.functors.ChainedClosure
*
* @param closures an array of closures to chain
* @return the <code>chained</code> closure
* @throws IllegalArgumentException if the closures array is null
* @throws IllegalArgumentException if any closure in the array is null
*/
public static Closure chainedClosure(Closure[] closures) {
return ChainedClosure.getInstance(closures);
}
/**
* Create a new Closure that calls each closure in turn, passing the
* result into the next closure. The ordering is that of the iterator()
* method on the collection.
*
* @see org.apache.commons.collections.functors.ChainedClosure
*
* @param closures a collection of closures to chain
* @return the <code>chained</code> closure
* @throws IllegalArgumentException if the closures collection is null
* @throws IllegalArgumentException if the closures collection is empty
* @throws IllegalArgumentException if any closure in the collection is null
*/
public static Closure chainedClosure(Collection closures) {
return ChainedClosure.getInstance(closures);
}
/**
* Create a new Closure that calls another closure based on the
* result of the specified predicate.
*
* @see org.apache.commons.collections.functors.IfClosure
*
* @param predicate the validating predicate
* @param trueClosure the closure called if the predicate is true
* @return the <code>if</code> closure
* @throws IllegalArgumentException if the predicate is null
* @throws IllegalArgumentException if the closure is null
* @since Commons Collections 3.2
*/
public static Closure ifClosure(Predicate predicate, Closure trueClosure) {
return IfClosure.getInstance(predicate, trueClosure);
}
/**
* Create a new Closure that calls one of two closures depending
* on the specified predicate.
*
* @see org.apache.commons.collections.functors.IfClosure
*
* @param predicate the predicate to switch on
* @param trueClosure the closure called if the predicate is true
* @param falseClosure the closure called if the predicate is false
* @return the <code>switch</code> closure
* @throws IllegalArgumentException if the predicate is null
* @throws IllegalArgumentException if either closure is null
*/
public static Closure ifClosure(Predicate predicate, Closure trueClosure, Closure falseClosure) {
return IfClosure.getInstance(predicate, trueClosure, falseClosure);
}
/**
* Create a new Closure that calls one of the closures depending
* on the predicates.
* <p>
* The closure at array location 0 is called if the predicate at array
* location 0 returned true. Each predicate is evaluated
* until one returns true.
*
* @see org.apache.commons.collections.functors.SwitchClosure
*
* @param predicates an array of predicates to check, not null
* @param closures an array of closures to call, not null
* @return the <code>switch</code> closure
* @throws IllegalArgumentException if the either array is null
* @throws IllegalArgumentException if any element in the arrays is null
* @throws IllegalArgumentException if the arrays are different sizes
*/
public static Closure switchClosure(Predicate[] predicates, Closure[] closures) {
return SwitchClosure.getInstance(predicates, closures, null);
}
/**
* Create a new Closure that calls one of the closures depending
* on the predicates.
* <p>
* The closure 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
* closure is called.
*
* @see org.apache.commons.collections.functors.SwitchClosure
*
* @param predicates an array of predicates to check, not null
* @param closures an array of closures to call, not null
* @param defaultClosure the default to call if no predicate matches
* @return the <code>switch</code> closure
* @throws IllegalArgumentException if the either array is null
* @throws IllegalArgumentException if any element in the arrays is null
* @throws IllegalArgumentException if the arrays are different sizes
*/
public static Closure switchClosure(Predicate[] predicates, Closure[] closures, Closure defaultClosure) {
return SwitchClosure.getInstance(predicates, closures, defaultClosure);
}
/**
* Create a new Closure that calls one of the closures depending
* on the predicates.
* <p>
* The Map consists of Predicate keys and Closure values. A closure
* is called if its matching predicate returns true. Each predicate is evaluated
* until one returns true. If no predicates evaluate to true, the default
* closure is called. The default closure is set in the map with a
* null key. The ordering is that of the iterator() method on the entryset
* collection of the map.
*
* @see org.apache.commons.collections.functors.SwitchClosure
*
* @param predicatesAndClosures a map of predicates to closures
* @return the <code>switch</code> closure
* @throws IllegalArgumentException if the map is null
* @throws IllegalArgumentException if the map is empty
* @throws IllegalArgumentException if any closure in the map is null
* @throws ClassCastException if the map elements are of the wrong type
*/
public static Closure switchClosure(Map predicatesAndClosures) {
return SwitchClosure.getInstance(predicatesAndClosures);
}
/**
* Create a new Closure that uses the input object as a key to find the
* closure to call.
* <p>
* The Map consists of object keys and Closure values. A closure
* is called if the input object equals the key. If there is no match, the
* default closure is called. The default closure is set in the map
* using a null key.
*
* @see org.apache.commons.collections.functors.SwitchClosure
*
* @param objectsAndClosures a map of objects to closures
* @return the closure
* @throws IllegalArgumentException if the map is null
* @throws IllegalArgumentException if the map is empty
* @throws IllegalArgumentException if any closure in the map is null
*/
public static Closure switchMapClosure(Map objectsAndClosures) {
Closure[] trs = null;
Predicate[] preds = null;
if (objectsAndClosures == null) {
throw new IllegalArgumentException("The object and closure map must not be null");
}
Closure def = (Closure) objectsAndClosures.remove(null);
int size = objectsAndClosures.size();
trs = new Closure[size];
preds = new Predicate[size];
int i = 0;
for (Iterator it = objectsAndClosures.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
preds[i] = EqualPredicate.getInstance(entry.getKey());
trs[i] = (Closure) entry.getValue();
i++;
}
return switchClosure(preds, trs, def);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -