Package org.apache.commons.collections4.functors

# Class IfClosure<T>

java.lang.Object
org.apache.commons.collections4.functors.IfClosure<T>

Type Parameters:
  `T` - The type of the input to the operation.

All Implemented Interfaces:
  `Serializable`, `Consumer<T>`, `Closure<T>`

public class IfClosure<T> extends Object implements Closure<T>, Serializable
Closure implementation acts as an if statement calling one or other closure based on a predicate.

Since:
  3.0
See Also:
  - Serialized Form

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `IfClosure(Predicate<? super T> predicate, Closure<? super T> trueClosure)`
  Constructor that performs no validation.
  `IfClosure(Predicate<? super T> predicate, Closure<? super T> trueClosure, Closure<? super T> falseClosure)`
  Constructor that performs no validation.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `execute(T input)`
  Executes the true or false closure according to the result of the predicate.
  `Closure<? super T>`
  `getFalseClosure()`
  Gets the closure called when false.
  `Predicate<? super T>`
  `getPredicate()`
  Gets the predicate.
  `Closure<? super T>`
  `getTrueClosure()`
  Gets the closure called when true.
  `static <E> Closure<E>`
  `ifClosure(Predicate<? super E> predicate, Closure<? super E> trueClosure)`
  Factory method that performs validation.
  `static <E> Closure<E>`
  `ifClosure(Predicate<? super E> predicate, Closure<? super E> trueClosure, Closure<? super E> falseClosure)`
  Factory method that performs validation.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface org.apache.commons.collections4.Closure

  `accept`

  ### Methods inherited from interface java.util.function.Consumer

  `andThen`

- ## Constructor Details

  - ### IfClosure

    public IfClosure(Predicate<? super T> predicate, Closure<? super T> trueClosure)
    Constructor that performs no validation. Use `ifClosure` if you want that.

    This constructor creates a closure that performs no action when the predicate is false.

    Parameters:
      `predicate` - predicate to switch on, not null
      `trueClosure` - closure used if true, not null
    Since:
      3.2

  - ### IfClosure

    public IfClosure(Predicate<? super T> predicate, Closure<? super T> trueClosure, Closure<? super T> falseClosure)
    Constructor that performs no validation. Use `ifClosure` if you want that.
    Parameters:
      `predicate` - predicate to switch on, not null
      `trueClosure` - closure used if true, not null
      `falseClosure` - closure used if false, not null

- ## Method Details

  - ### ifClosure

    public static <E> Closure<E> ifClosure(Predicate<? super E> predicate, Closure<? super E> trueClosure)
    Factory method that performs validation.

    This factory creates a closure that performs no action when the predicate is false.

    Type Parameters:
      `E` - The type that the closure acts on
    Parameters:
      `predicate` - predicate to switch on
      `trueClosure` - closure used if true
    Returns:
      The `if` closure
    Throws:
      `NullPointerException` - if either argument is null
    Since:
      3.2

  - ### ifClosure

    public static <E> Closure<E> ifClosure(Predicate<? super E> predicate, Closure<? super E> trueClosure, Closure<? super E> falseClosure)
    Factory method that performs validation.
    Type Parameters:
      `E` - The type that the closure acts on
    Parameters:
      `predicate` - predicate to switch on
      `trueClosure` - closure used if true
      `falseClosure` - closure used if false
    Returns:
      The `if` closure
    Throws:
      `NullPointerException` - if any argument is null

  - ### execute

    public void execute(T input)
    Executes the true or false closure according to the result of the predicate.
    Specified by:
      `execute` in interface `Closure<T>`
    Parameters:
      `input` - The input object

  - ### getFalseClosure

    public Closure<? super T> getFalseClosure()
    Gets the closure called when false.
    Returns:
      The closure
    Since:
      3.1

  - ### getPredicate

    public Predicate<? super T> getPredicate()
    Gets the predicate.
    Returns:
      The predicate
    Since:
      3.1

  - ### getTrueClosure

    public Closure<? super T> getTrueClosure()
    Gets the closure called when true.
    Returns:
      The closure
    Since:
      3.1