Class PredicatedConsumer<T>

java.lang.Object
com.norconex.commons.lang.function.PredicatedConsumer<T>
Type Parameters:
T - type being predicated and consumed
All Implemented Interfaces:
Consumer<T>

public class PredicatedConsumer<T> extends Object implements Consumer<T>

A Consumer that only gets triggered if a given predicate is true.

A null predicate evaluates to true (before applying negation if "negate" is true).

A null consumer is simply ignored.

Setting the "negate" constructor argument to true will perform the same predicate evaluation, but will only trigger the "then" consumer if the predicate returns false.

Since:
2.0.0
  • Constructor Details

    • PredicatedConsumer

      @Deprecated(since="3.0.0") public PredicatedConsumer(Predicate<T> predicate, Consumer<T> consumer)
      Deprecated.
      Since 3.0.0, use ifTrue(Predicate, Consumer) instead.
      Constructor.
      Parameters:
      predicate - predicate
      consumer - consumer
    • PredicatedConsumer

      @Deprecated(since="3.0.0") public PredicatedConsumer(Predicate<T> predicate, Consumer<T> consumer, boolean negate)
      Deprecated.
      Since 3.0.0, use ifTrue(Predicate, Consumer, Consumer) instead.
      Constructor.
      Parameters:
      predicate - predicate
      consumer - consumer
      negate - whether to negate the condition
    • PredicatedConsumer

      public PredicatedConsumer(Predicate<T> predicate, Consumer<T> thenConsumer, Consumer<T> elseConsumer, boolean negate)
      Constructor.
      Parameters:
      predicate - predicate condition
      thenConsumer - consumer invoked when condition is true
      elseConsumer - consumer invoked when condition is false
      negate - whether to negate the condition
  • Method Details

    • getPredicate

      public Predicate<T> getPredicate()
    • getConsumer

      @Deprecated(since="3.0.0") public Consumer<T> getConsumer()
      Deprecated.
      Use getThenConsumer() instead.
      Gets the "then" consumer.
      Returns:
      consumer
    • accept

      public final void accept(T t)
      Specified by:
      accept in interface Consumer<T>
    • ifTrue

      public static <T> PredicatedConsumer<T> ifTrue(Predicate<T> condition, Consumer<T> thenConsumer)
      If condition is true, then execute consumer.
      Type Parameters:
      T - type of object consumed
      Parameters:
      condition - the condition deciding whether to consume
      thenConsumer - the object consumer
      Returns:
      a predicated consumer
      Since:
      3.0.0
    • ifTrue

      public static <T> PredicatedConsumer<T> ifTrue(Predicate<T> condition, Consumer<T> thenConsumer, Consumer<T> elseConsumer)
      If condition is true, then execute the "then" consumer, else,execute the "else" consumer.
      Type Parameters:
      T - type of object consumed
      Parameters:
      condition - the condition deciding which consumer to use
      thenConsumer - the object consumer when condition is true
      elseConsumer - the object consumer when condition is false
      Returns:
      a predicated consumer
      Since:
      3.0.0
    • ifFalse

      public static <T> PredicatedConsumer<T> ifFalse(Predicate<T> condition, Consumer<T> thenConsumer)
      If condition is false, then execute consumer.
      Type Parameters:
      T - type of object consumed
      Parameters:
      condition - the condition deciding whether to consume
      thenConsumer - the object consumer
      Returns:
      a predicated consumer
      Since:
      3.0.0
    • ifFalse

      public static <T> PredicatedConsumer<T> ifFalse(Predicate<T> condition, Consumer<T> thenConsumer, Consumer<T> elseConsumer)
      If condition is false, then execute the "then" consumer, else, execute the "else" consumer.
      Type Parameters:
      T - type of object consumed
      Parameters:
      condition - the condition deciding which consumer to use
      thenConsumer - the object consumer when condition is true
      elseConsumer - the object consumer when condition is false
      Returns:
      a predicated consumer
      Since:
      3.0.0
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • canEqual

      protected boolean canEqual(Object other)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getThenConsumer

      public Consumer<T> getThenConsumer()
    • getElseConsumer

      public Consumer<T> getElseConsumer()
    • isNegate

      public boolean isNegate()