IOIntConsumer.java

  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.  *      http://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. package org.apache.commons.io.function;

  18. import java.io.IOException;
  19. import java.io.UncheckedIOException;
  20. import java.util.Objects;
  21. import java.util.function.Consumer;
  22. import java.util.function.IntConsumer;

  23. /**
  24.  * Like {@link IntConsumer} but throws {@link IOException}.
  25.  *
  26.  * @since 2.18.0
  27.  */
  28. @FunctionalInterface
  29. public interface IOIntConsumer {

  30.     /**
  31.      * The constant no-op consumer.
  32.      */
  33.     IOIntConsumer NOOP = i -> {
  34.         // noop
  35.     };

  36.     /**
  37.      * Performs this operation on the given argument.
  38.      *
  39.      * @param value the input argument
  40.      * @throws IOException if an I/O error occurs.
  41.      */
  42.     void accept(int value) throws IOException;

  43.     /**
  44.      * Returns a composed {@code IOIntConsumer} that performs, in sequence, this operation followed by the {@code after} operation. If performing either
  45.      * operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, the {@code after}
  46.      * operation will not be performed.
  47.      *
  48.      * @param after the operation to perform after this operation
  49.      * @return a composed {@code IOIntConsumer} that performs in sequence this operation followed by the {@code after} operation
  50.      * @throws NullPointerException if {@code after} is null
  51.      */
  52.     default IOIntConsumer andThen(final IOIntConsumer after) {
  53.         Objects.requireNonNull(after);
  54.         return (final int i) -> {
  55.             accept(i);
  56.             after.accept(i);
  57.         };
  58.     }

  59.     /**
  60.      * Creates a {@link Consumer} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
  61.      *
  62.      * @return an UncheckedIOException IntConsumer.
  63.      */
  64.     default Consumer<Integer> asConsumer() {
  65.         return i -> Uncheck.accept(this, i);
  66.     }

  67.     /**
  68.      * Creates an {@link IntConsumer} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
  69.      *
  70.      * @return an UncheckedIOException IntConsumer.
  71.      */
  72.     default IntConsumer asIntConsumer() {
  73.         return i -> Uncheck.accept(this, i);
  74.     }

  75. }