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 -> { 038 // noop 039 }; 040 041 /** 042 * Performs this operation on the given argument. 043 * 044 * @param value the input argument 045 * @throws IOException if an I/O error occurs. 046 */ 047 void accept(int value) throws IOException; 048 049 /** 050 * Returns a composed {@code IOIntConsumer} that performs, in sequence, this operation followed by the {@code after} operation. If performing either 051 * operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, the {@code after} 052 * operation will not be performed. 053 * 054 * @param after the operation to perform after this operation 055 * @return a composed {@code IOIntConsumer} that performs in sequence this operation followed by the {@code after} operation 056 * @throws NullPointerException if {@code after} is null 057 */ 058 default IOIntConsumer andThen(final IOIntConsumer after) { 059 Objects.requireNonNull(after); 060 return (final int i) -> { 061 accept(i); 062 after.accept(i); 063 }; 064 } 065 066 /** 067 * Creates a {@link Consumer} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}. 068 * 069 * @return an UncheckedIOException IntConsumer. 070 */ 071 default Consumer<Integer> asConsumer() { 072 return i -> Uncheck.accept(this, i); 073 } 074 075 /** 076 * Creates an {@link IntConsumer} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}. 077 * 078 * @return an UncheckedIOException IntConsumer. 079 */ 080 default IntConsumer asIntConsumer() { 081 return i -> Uncheck.accept(this, i); 082 } 083 084}