Class ClosureUtils


  • @Deprecated(since="2021-04-30")
    public class ClosureUtils
    extends java.lang.Object
    Deprecated.
    Commons Collections 3 is in maintenance mode. Commons Collections 4 should be used instead.
    ClosureUtils provides reference implementations and utilities for the Closure functor interface. The supplied closures are:
    • Invoker - invokes a method on the input object
    • For - repeatedly calls a closure for a fixed number of times
    • While - repeatedly calls a closure while a predicate is true
    • DoWhile - repeatedly calls a closure while a predicate is true
    • Chained - chains two or more closures together
    • Switch - calls one closure based on one or more predicates
    • SwitchMap - calls one closure looked up from a Map
    • Transformer - wraps a Transformer as a Closure
    • NOP - does nothing
    • Exception - always throws an exception
    All the supplied closures are Serializable.
    Since:
    Commons Collections 3.0
    • Constructor Summary

      Constructors 
      Constructor Description
      ClosureUtils()
      Deprecated.
      This class is not normally instantiated.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static Closure asClosure​(Transformer transformer)
      Deprecated.
      Creates a Closure that calls a Transformer each time it is called.
      static Closure chainedClosure​(java.util.Collection closures)
      Deprecated.
      Create a new Closure that calls each closure in turn, passing the result into the next closure.
      static Closure chainedClosure​(Closure[] closures)
      Deprecated.
      Create a new Closure that calls each closure in turn, passing the result into the next closure.
      static Closure chainedClosure​(Closure closure1, Closure closure2)
      Deprecated.
      Create a new Closure that calls two Closures, passing the result of the first into the second.
      static Closure doWhileClosure​(Closure closure, Predicate predicate)
      Deprecated.
      Creates a Closure that will call the closure once and then repeatedly until the predicate returns false.
      static Closure exceptionClosure()
      Deprecated.
      Gets a Closure that always throws an exception.
      static Closure forClosure​(int count, Closure closure)
      Deprecated.
      Creates a Closure that will call the closure count times.
      static Closure ifClosure​(Predicate predicate, Closure trueClosure)
      Deprecated.
      Create a new Closure that calls another closure based on the result of the specified predicate.
      static Closure ifClosure​(Predicate predicate, Closure trueClosure, Closure falseClosure)
      Deprecated.
      Create a new Closure that calls one of two closures depending on the specified predicate.
      static Closure invokerClosure​(java.lang.String methodName)
      Deprecated.
      Creates a Closure that will invoke a specific method on the closure's input object by reflection.
      static Closure invokerClosure​(java.lang.String methodName, java.lang.Class[] paramTypes, java.lang.Object[] args)
      Deprecated.
      Creates a Closure that will invoke a specific method on the closure's input object by reflection.
      static Closure nopClosure()
      Deprecated.
      Gets a Closure that will do nothing.
      static Closure switchClosure​(java.util.Map predicatesAndClosures)
      Deprecated.
      Create a new Closure that calls one of the closures depending on the predicates.
      static Closure switchClosure​(Predicate[] predicates, Closure[] closures)
      Deprecated.
      Create a new Closure that calls one of the closures depending on the predicates.
      static Closure switchClosure​(Predicate[] predicates, Closure[] closures, Closure defaultClosure)
      Deprecated.
      Create a new Closure that calls one of the closures depending on the predicates.
      static Closure switchMapClosure​(java.util.Map objectsAndClosures)
      Deprecated.
      Create a new Closure that uses the input object as a key to find the closure to call.
      static Closure whileClosure​(Predicate predicate, Closure closure)
      Deprecated.
      Creates a Closure that will call the closure repeatedly until the predicate returns false.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ClosureUtils

        public ClosureUtils()
        Deprecated.
        This class is not normally instantiated.
    • Method Detail

      • exceptionClosure

        public static Closure exceptionClosure()
        Deprecated.
        Gets a Closure that always throws an exception. This could be useful during testing as a placeholder.
        Returns:
        the closure
        See Also:
        ExceptionClosure
      • nopClosure

        public static Closure nopClosure()
        Deprecated.
        Gets a Closure that will do nothing. This could be useful during testing as a placeholder.
        Returns:
        the closure
        See Also:
        NOPClosure
      • asClosure

        public static Closure asClosure​(Transformer transformer)
        Deprecated.
        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.
        Parameters:
        transformer - the transformer to run each time in the closure, null means nop
        Returns:
        the closure
        See Also:
        TransformerClosure
      • forClosure

        public static Closure forClosure​(int count,
                                         Closure closure)
        Deprecated.
        Creates a Closure that will call the closure count times.

        A null closure or zero count returns the NOPClosure.

        Parameters:
        count - the number of times to loop
        closure - the closure to call repeatedly
        Returns:
        the for closure
        See Also:
        ForClosure
      • whileClosure

        public static Closure whileClosure​(Predicate predicate,
                                           Closure closure)
        Deprecated.
        Creates a Closure that will call the closure repeatedly until the predicate returns false.
        Parameters:
        predicate - the predicate to use as an end of loop test, not null
        closure - the closure to call repeatedly, not null
        Returns:
        the while closure
        Throws:
        java.lang.IllegalArgumentException - if either argument is null
        See Also:
        WhileClosure
      • doWhileClosure

        public static Closure doWhileClosure​(Closure closure,
                                             Predicate predicate)
        Deprecated.
        Creates a Closure that will call the closure once and then repeatedly until the predicate returns false.
        Parameters:
        closure - the closure to call repeatedly, not null
        predicate - the predicate to use as an end of loop test, not null
        Returns:
        the do-while closure
        Throws:
        java.lang.IllegalArgumentException - if either argument is null
        See Also:
        WhileClosure
      • invokerClosure

        public static Closure invokerClosure​(java.lang.String methodName)
        Deprecated.
        Creates a Closure that will invoke a specific method on the closure's input object by reflection.
        Parameters:
        methodName - the name of the method
        Returns:
        the invoker closure
        Throws:
        java.lang.IllegalArgumentException - if the method name is null
        See Also:
        InvokerTransformer, TransformerClosure
      • invokerClosure

        public static Closure invokerClosure​(java.lang.String methodName,
                                             java.lang.Class[] paramTypes,
                                             java.lang.Object[] args)
        Deprecated.
        Creates a Closure that will invoke a specific method on the closure's input object by reflection.
        Parameters:
        methodName - the name of the method
        paramTypes - the parameter types
        args - the arguments
        Returns:
        the invoker closure
        Throws:
        java.lang.IllegalArgumentException - if the method name is null
        java.lang.IllegalArgumentException - if the paramTypes and args don't match
        See Also:
        InvokerTransformer, TransformerClosure
      • chainedClosure

        public static Closure chainedClosure​(Closure closure1,
                                             Closure closure2)
        Deprecated.
        Create a new Closure that calls two Closures, passing the result of the first into the second.
        Parameters:
        closure1 - the first closure
        closure2 - the second closure
        Returns:
        the chained closure
        Throws:
        java.lang.IllegalArgumentException - if either closure is null
        See Also:
        ChainedClosure
      • chainedClosure

        public static Closure chainedClosure​(Closure[] closures)
        Deprecated.
        Create a new Closure that calls each closure in turn, passing the result into the next closure.
        Parameters:
        closures - an array of closures to chain
        Returns:
        the chained closure
        Throws:
        java.lang.IllegalArgumentException - if the closures array is null
        java.lang.IllegalArgumentException - if any closure in the array is null
        See Also:
        ChainedClosure
      • chainedClosure

        public static Closure chainedClosure​(java.util.Collection closures)
        Deprecated.
        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.
        Parameters:
        closures - a collection of closures to chain
        Returns:
        the chained closure
        Throws:
        java.lang.IllegalArgumentException - if the closures collection is null
        java.lang.IllegalArgumentException - if the closures collection is empty
        java.lang.IllegalArgumentException - if any closure in the collection is null
        See Also:
        ChainedClosure
      • ifClosure

        public static Closure ifClosure​(Predicate predicate,
                                        Closure trueClosure)
        Deprecated.
        Create a new Closure that calls another closure based on the result of the specified predicate.
        Parameters:
        predicate - the validating predicate
        trueClosure - the closure called if the predicate is true
        Returns:
        the if closure
        Throws:
        java.lang.IllegalArgumentException - if the predicate is null
        java.lang.IllegalArgumentException - if the closure is null
        Since:
        Commons Collections 3.2
        See Also:
        IfClosure
      • ifClosure

        public static Closure ifClosure​(Predicate predicate,
                                        Closure trueClosure,
                                        Closure falseClosure)
        Deprecated.
        Create a new Closure that calls one of two closures depending on the specified predicate.
        Parameters:
        predicate - the predicate to switch on
        trueClosure - the closure called if the predicate is true
        falseClosure - the closure called if the predicate is false
        Returns:
        the switch closure
        Throws:
        java.lang.IllegalArgumentException - if the predicate is null
        java.lang.IllegalArgumentException - if either closure is null
        See Also:
        IfClosure
      • switchClosure

        public static Closure switchClosure​(Predicate[] predicates,
                                            Closure[] closures)
        Deprecated.
        Create a new Closure that calls one of the closures depending on the predicates.

        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.

        Parameters:
        predicates - an array of predicates to check, not null
        closures - an array of closures to call, not null
        Returns:
        the switch closure
        Throws:
        java.lang.IllegalArgumentException - if the either array is null
        java.lang.IllegalArgumentException - if any element in the arrays is null
        java.lang.IllegalArgumentException - if the arrays are different sizes
        See Also:
        SwitchClosure
      • switchClosure

        public static Closure switchClosure​(Predicate[] predicates,
                                            Closure[] closures,
                                            Closure defaultClosure)
        Deprecated.
        Create a new Closure that calls one of the closures depending on the predicates.

        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.

        Parameters:
        predicates - an array of predicates to check, not null
        closures - an array of closures to call, not null
        defaultClosure - the default to call if no predicate matches
        Returns:
        the switch closure
        Throws:
        java.lang.IllegalArgumentException - if the either array is null
        java.lang.IllegalArgumentException - if any element in the arrays is null
        java.lang.IllegalArgumentException - if the arrays are different sizes
        See Also:
        SwitchClosure
      • switchClosure

        public static Closure switchClosure​(java.util.Map predicatesAndClosures)
        Deprecated.
        Create a new Closure that calls one of the closures depending on the predicates.

        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.

        Parameters:
        predicatesAndClosures - a map of predicates to closures
        Returns:
        the switch closure
        Throws:
        java.lang.IllegalArgumentException - if the map is null
        java.lang.IllegalArgumentException - if the map is empty
        java.lang.IllegalArgumentException - if any closure in the map is null
        java.lang.ClassCastException - if the map elements are of the wrong type
        See Also:
        SwitchClosure
      • switchMapClosure

        public static Closure switchMapClosure​(java.util.Map objectsAndClosures)
        Deprecated.
        Create a new Closure that uses the input object as a key to find the closure to call.

        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.

        Parameters:
        objectsAndClosures - a map of objects to closures
        Returns:
        the closure
        Throws:
        java.lang.IllegalArgumentException - if the map is null
        java.lang.IllegalArgumentException - if the map is empty
        java.lang.IllegalArgumentException - if any closure in the map is null
        See Also:
        SwitchClosure