Home | History | Annotate | Download | only in util
      1 package org.robolectric.util;
      2 
      3 import java.util.Objects;
      4 
      5 /**
      6  * Represents an operation that accepts a single input argument and returns no
      7  * result. Unlike most other functional interfaces, {@code Consumer} is expected
      8  * to operate via side-effects.
      9  *
     10  * Included in Robolectric since Android doesn't support streams yet (as of O).
     11  *
     12  * @param <T> the type of the input to the operation
     13  */
     14 public interface Consumer<T> {
     15 
     16   /**
     17    * Performs this operation on the given argument.
     18    *
     19    * @param t the input argument
     20    */
     21   void accept(T t);
     22 
     23   /**
     24    * Returns a composed {@code Consumer} that performs, in sequence, this operation followed by the
     25    * {@code after} operation. If performing either operation throws an exception, it is relayed to
     26    * the caller of the composed operation.  If performing this operation throws an exception, the
     27    * {@code after} operation will not be performed.
     28    *
     29    * @param after the operation to perform after this operation
     30    * @return a composed {@code Consumer} that performs in sequence this operation followed by the
     31    * {@code after} operation
     32    * @throws NullPointerException if {@code after} is null
     33    */
     34   default Consumer<T> andThen(Consumer<? super T> after) {
     35     Objects.requireNonNull(after);
     36     return (T t) -> {
     37       accept(t);
     38       after.accept(t);
     39     };
     40   }
     41 }
     42