001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.io.function; 019 020import java.io.IOException; 021import java.io.UncheckedIOException; 022import java.util.Objects; 023import java.util.function.BiConsumer; 024 025/** 026 * Like {@link BiConsumer} but throws {@link IOException}. 027 * 028 * @param <T> the type of the first argument to the operation 029 * @param <U> the type of the second argument to the operation 030 * @see BiConsumer 031 * @since 2.12.0 032 */ 033@FunctionalInterface 034public interface IOBiConsumer<T, U> { 035 036 /** 037 * Returns the no-op singleton. 038 * 039 * @param <T> the type of the first argument to the operation 040 * @param <U> the type of the second argument to the operation 041 * @return The no-op singleton. 042 */ 043 @SuppressWarnings("unchecked") 044 static <T, U> IOBiConsumer<T, U> noop() { 045 return Constants.IO_BI_CONSUMER; 046 } 047 048 /** 049 * Performs this operation on the given arguments. 050 * 051 * @param t the first input argument 052 * @param u the second input argument 053 * @throws IOException if an I/O error occurs. 054 */ 055 void accept(T t, U u) throws IOException; 056 057 /** 058 * Creates a composed {@link IOBiConsumer} that performs, in sequence, this operation followed by the {@code after} 059 * operation. If performing either operation throws an exception, it is relayed to the caller of the composed operation. 060 * If performing this operation throws an exception, the {@code after} operation will not be performed. 061 * 062 * @param after the operation to perform after this operation 063 * @return a composed {@link IOBiConsumer} that performs in sequence this operation followed by the {@code after} 064 * operation 065 * @throws NullPointerException if {@code after} is null 066 */ 067 default IOBiConsumer<T, U> andThen(final IOBiConsumer<? super T, ? super U> after) { 068 Objects.requireNonNull(after); 069 return (t, u) -> { 070 accept(t, u); 071 after.accept(t, u); 072 }; 073 } 074 075 /** 076 * Creates a {@link BiConsumer} for this instance that throws {@link UncheckedIOException} instead of 077 * {@link IOException}. 078 * 079 * @return an UncheckedIOException BiConsumer. 080 * @throws UncheckedIOException Wraps an {@link IOException}. 081 * @since 2.12.0 082 */ 083 default BiConsumer<T, U> asBiConsumer() { 084 return (t, u) -> Uncheck.accept(this, t, u); 085 } 086 087}