src/share/classes/java/util/function/BiConsumer.java

Print this page
rev 7675 : 8019840: Spec updates for java.util.function
Reviewed-by: mduigou
Contributed-by: brian.goetz@oracle.com


  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.util.function;
  26 
  27 import java.util.Objects;
  28 
  29 /**
  30  * An operation which accepts two input arguments and returns no result. This is
  31  * the two-arity specialization of {@link Consumer}. Unlike most other
  32  * functional interfaces, {@code BiConsumer} is expected to operate via
  33  * side-effects.
  34  *
  35  * @param <T> the type of the first argument to the {@code accept} operation
  36  * @param <U> the type of the second argument to the {@code accept} operation



  37  *
  38  * @see Consumer
  39  * @since 1.8
  40  */
  41 @FunctionalInterface
  42 public interface BiConsumer<T, U> {
  43 
  44     /**
  45      * Performs operations upon the provided objects which may modify those
  46      * objects and/or external state.
  47      *
  48      * @param t an input object
  49      * @param u an input object
  50      */
  51     void accept(T t, U u);
  52 
  53     /**
  54      * Returns a {@code BiConsumer} which performs, in sequence, the operation
  55      * represented by this object followed by the operation represented by
  56      * the other {@code BiConsumer}.
  57      *
  58      * <p>Any exceptions thrown by either {@code accept} method are relayed
  59      * to the caller; if performing this operation throws an exception, the
  60      * other operation will not be performed.
  61      *
  62      * @param other a BiConsumer which will be chained after this BiConsumer
  63      * @return a BiConsumer which performs in sequence the {@code accept} method
  64      * of this BiConsumer and the {@code accept} method of the specified
  65      * BiConsumer operation
  66      * @throws NullPointerException if other is null
  67      */
  68     default BiConsumer<T, U> chain(BiConsumer<? super T, ? super U> other) {
  69         Objects.requireNonNull(other);
  70 
  71         return (l, r) -> {
  72             accept(l, r);
  73             other.accept(l, r);
  74         };
  75     }
  76 }


  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.util.function;
  26 
  27 import java.util.Objects;
  28 
  29 /**
  30  * Represents an operation that accepts two input arguments and returns no
  31  * result.  This is the two-arity specialization of {@link Consumer}.
  32  * Unlike most other functional interfaces, {@code BiConsumer} is expected
  33  * to operate via side-effects.
  34  *
  35  * <p>This is a <a href="package-summary.html">functional interface</a>
  36  * whose functional method is {@link #accept(Object, Object)}.
  37  *
  38  * @param <T> the type of the first argument to the operation
  39  * @param <U> the type of the second argument to the operation
  40  *
  41  * @see Consumer
  42  * @since 1.8
  43  */
  44 @FunctionalInterface
  45 public interface BiConsumer<T, U> {
  46 
  47     /**
  48      * Performs this operation on the given arguments.

  49      *
  50      * @param t the first input argument
  51      * @param u the second input argument
  52      */
  53     void accept(T t, U u);
  54 
  55     /**
  56      * Returns a composed {@code BiConsumer} that performs, in sequence, this
  57      * operation followed by the {@code after} operation. If performing either
  58      * operation throws an exception, it is relayed to the caller of the
  59      * composed operation.  If performing this operation throws an exception,
  60      * the {@code after} operation will not be performed.


  61      *
  62      * @param after the operation to perform after this operation
  63      * @return a composed {@code BiConsumer} that performs in sequence this
  64      * operation followed by the {@code after} operation
  65      * @throws NullPointerException if {@code after} is null

  66      */
  67     default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
  68         Objects.requireNonNull(after);
  69 
  70         return (l, r) -> {
  71             accept(l, r);
  72             after.accept(l, r);
  73         };
  74     }
  75 }