📄 closureutils.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.collections.functors.ChainedClosure;
import org.apache.commons.collections.functors.EqualPredicate;
import org.apache.commons.collections.functors.ExceptionClosure;
import org.apache.commons.collections.functors.ForClosure;
import org.apache.commons.collections.functors.IfClosure;
import org.apache.commons.collections.functors.InvokerTransformer;
import org.apache.commons.collections.functors.NOPClosure;
import org.apache.commons.collections.functors.SwitchClosure;
import org.apache.commons.collections.functors.TransformerClosure;
import org.apache.commons.collections.functors.WhileClosure;
/**
* <code>ClosureUtils</code> provides reference implementations and utilities
* for the Closure functor interface. The supplied closures are:
* <ul>
* <li>Invoker - invokes a method on the input object
* <li>For - repeatedly calls a closure for a fixed number of times
* <li>While - repeatedly calls a closure while a predicate is true
* <li>DoWhile - repeatedly calls a closure while a predicate is true
* <li>Chained - chains two or more closures together
* <li>Switch - calls one closure based on one or more predicates
* <li>SwitchMap - calls one closure looked up from a Map
* <li>Transformer - wraps a Transformer as a Closure
* <li>NOP - does nothing
* <li>Exception - always throws an exception
* </ul>
* All the supplied closures are Serializable.
*
* @since Commons Collections 3.0
* @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
*
* @author Stephen Colebourne
* @author Matt Benson
*/
public class ClosureUtils {
/**
* This class is not normally instantiated.
*/
public ClosureUtils() {
super();
}
/**
* Gets a Closure that always throws an exception.
* This could be useful during testing as a placeholder.
*
* @see org.apache.commons.collections.functors.ExceptionClosure
*
* @return the closure
*/
public static Closure exceptionClosure() {
return ExceptionClosure.INSTANCE;
}
/**
* Gets a Closure that will do nothing.
* This could be useful during testing as a placeholder.
*
* @see org.apache.commons.collections.functors.NOPClosure
*
* @return the closure
*/
public static Closure nopClosure() {
return NOPClosure.INSTANCE;
}
/**
* Creates a Closure that calls a Transformer each time it is called.
* The transformer will be called using the closure's input object.
* The transformer's result will be ignored.
*
* @see org.apache.commons.collections.functors.TransformerClosure
*
* @param transformer the transformer to run each time in the closure, null means nop
* @return the closure
*/
public static Closure asClosure(Transformer transformer) {
return TransformerClosure.getInstance(transformer);
}
/**
* Creates a Closure that will call the closure <code>count</code> times.
* <p>
* A null closure or zero count returns the <code>NOPClosure</code>.
*
* @see org.apache.commons.collections.functors.ForClosure
*
* @param count the number of times to loop
* @param closure the closure to call repeatedly
* @return the <code>for</code> closure
*/
public static Closure forClosure(int count, Closure closure) {
return ForClosure.getInstance(count, closure);
}
/**
* Creates a Closure that will call the closure repeatedly until the
* predicate returns false.
*
* @see org.apache.commons.collections.functors.WhileClosure
*
* @param predicate the predicate to use as an end of loop test, not null
* @param closure the closure to call repeatedly, not null
* @return the <code>while</code> closure
* @throws IllegalArgumentException if either argument is null
*/
public static Closure whileClosure(Predicate predicate, Closure closure) {
return WhileClosure.getInstance(predicate, closure, false);
}
/**
* Creates a Closure that will call the closure once and then repeatedly
* until the predicate returns false.
*
* @see org.apache.commons.collections.functors.WhileClosure
*
* @param closure the closure to call repeatedly, not null
* @param predicate the predicate to use as an end of loop test, not null
* @return the <code>do-while</code> closure
* @throws IllegalArgumentException if either argument is null
*/
public static Closure doWhileClosure(Closure closure, Predicate predicate) {
return WhileClosure.getInstance(predicate, closure, true);
}
/**
* Creates a Closure that will invoke a specific method on the closure's
* input object by reflection.
*
* @see org.apache.commons.collections.functors.InvokerTransformer
* @see org.apache.commons.collections.functors.TransformerClosure
*
* @param methodName the name of the method
* @return the <code>invoker</code> closure
* @throws IllegalArgumentException if the method name is null
*/
public static Closure invokerClosure(String methodName) {
// reuse transformer as it has caching - this is lazy really, should have inner class here
return asClosure(InvokerTransformer.getInstance(methodName));
}
/**
* Creates a Closure that will invoke a specific method on the closure's
* input object by reflection.
*
* @see org.apache.commons.collections.functors.InvokerTransformer
* @see org.apache.commons.collections.functors.TransformerClosure
*
* @param methodName the name of the method
* @param paramTypes the parameter types
* @param args the arguments
* @return the <code>invoker</code> closure
* @throws IllegalArgumentException if the method name is null
* @throws IllegalArgumentException if the paramTypes and args don't match
*/
public static Closure invokerClosure(String methodName, Class[] paramTypes, Object[] args) {
// reuse transformer as it has caching - this is lazy really, should have inner class here
return asClosure(InvokerTransformer.getInstance(methodName, paramTypes, args));
}
/**
* Create a new Closure that calls two Closures, passing the result of
* the first into the second.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -