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.Consumer; 024import java.util.function.IntConsumer; 025 026/** 027 * Like {@link IntConsumer} but throws {@link IOException}. 028 * 029 * @since 2.18.0 030 */ 031@FunctionalInterface 032public interface IOIntConsumer { 033 034 /** 035 * The constant no-op consumer. 036 */ 037 IOIntConsumer NOOP = i -> {/* noop */}; 038 039 /** 040 * Performs this operation on the given argument. 041 * 042 * @param value the input argument 043 * @throws IOException if an I/O error occurs. 044 */ 045 void accept(int value) throws IOException; 046 047 /** 048 * Returns a composed {@code IOIntConsumer} that performs, in sequence, this operation followed by the {@code after} operation. If performing either 049 * operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, the {@code after} 050 * operation will not be performed. 051 * 052 * @param after the operation to perform after this operation 053 * @return a composed {@code IOIntConsumer} that performs in sequence this operation followed by the {@code after} operation 054 * @throws NullPointerException if {@code after} is null 055 */ 056 default IOIntConsumer andThen(final IOIntConsumer after) { 057 Objects.requireNonNull(after); 058 return (final int i) -> { 059 accept(i); 060 after.accept(i); 061 }; 062 } 063 064 /** 065 * Creates a {@link Consumer} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}. 066 * 067 * @return an UncheckedIOException IntConsumer. 068 */ 069 default Consumer<Integer> asConsumer() { 070 return i -> Uncheck.accept(this, i); 071 } 072 073 /** 074 * Creates an {@link IntConsumer} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}. 075 * 076 * @return an UncheckedIOException IntConsumer. 077 */ 078 default IntConsumer asIntConsumer() { 079 return i -> Uncheck.accept(this, i); 080 } 081 082}