1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 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 }