1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.io.function;
19
20 import java.io.IOException;
21 import java.io.UncheckedIOException;
22 import java.util.Objects;
23 import java.util.function.Consumer;
24 import java.util.function.IntConsumer;
25
26 /**
27 * Like {@link IntConsumer} but throws {@link IOException}.
28 *
29 * @since 2.18.0
30 */
31 @FunctionalInterface
32 public interface IOIntConsumer {
33
34 /**
35 * The constant no-op consumer.
36 */
37 IOIntConsumer NOOP = i -> {
38 // noop
39 };
40
41 /**
42 * Performs this operation on the given argument.
43 *
44 * @param value the input argument
45 * @throws IOException if an I/O error occurs.
46 */
47 void accept(int value) throws IOException;
48
49 /**
50 * Returns a composed {@code IOIntConsumer} that performs, in sequence, this operation followed by the {@code after} operation. If performing either
51 * operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, the {@code after}
52 * operation will not be performed.
53 *
54 * @param after the operation to perform after this operation
55 * @return a composed {@code IOIntConsumer} that performs in sequence this operation followed by the {@code after} operation
56 * @throws NullPointerException if {@code after} is null
57 */
58 default IOIntConsumer andThen(final IOIntConsumer after) {
59 Objects.requireNonNull(after);
60 return (final int i) -> {
61 accept(i);
62 after.accept(i);
63 };
64 }
65
66 /**
67 * Creates a {@link Consumer} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
68 *
69 * @return an UncheckedIOException IntConsumer.
70 */
71 default Consumer<Integer> asConsumer() {
72 return i -> Uncheck.accept(this, i);
73 }
74
75 /**
76 * Creates an {@link IntConsumer} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
77 *
78 * @return an UncheckedIOException IntConsumer.
79 */
80 default IntConsumer asIntConsumer() {
81 return i -> Uncheck.accept(this, i);
82 }
83
84 }
85