IOFunction.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.Function;
  23. import java.util.function.Supplier;

  24. /**
  25.  * Like {@link Function} but throws {@link IOException}.
  26.  *
  27.  * @param <T> the type of the input to the operations.
  28.  * @param <R> the return type of the operations.
  29.  * @since 2.7
  30.  */
  31. @FunctionalInterface
  32. public interface IOFunction<T, R> {

  33.     /**
  34.      * Returns a {@link IOFunction} that always returns its input argument.
  35.      *
  36.      * @param <T> the type of the input and output objects to the function
  37.      * @return a function that always returns its input argument
  38.      */
  39.     @SuppressWarnings("unchecked")
  40.     static <T> IOFunction<T, T> identity() {
  41.         return Constants.IO_FUNCTION_ID;
  42.     }

  43.     /**
  44.      * Returns a composed {@link IOFunction} that first applies this function to its input, and then applies the
  45.      * {@code after} consumer to the result. If evaluation of either function throws an exception, it is relayed to the
  46.      * caller of the composed function.
  47.      *
  48.      * @param after the consumer to apply after this function is applied
  49.      * @return a composed function that first applies this function and then applies the {@code after} consumer
  50.      * @throws NullPointerException if after is null
  51.      * @see #compose(IOFunction)
  52.      */
  53.     default IOConsumer<T> andThen(final Consumer<? super R> after) {
  54.         Objects.requireNonNull(after, "after");
  55.         return (final T t) -> after.accept(apply(t));
  56.     }

  57.     /**
  58.      * Returns a composed {@link IOFunction} that first applies this function to its input, and then applies the
  59.      * {@code after} function to the result. If evaluation of either function throws an exception, it is relayed to the
  60.      * caller of the composed function.
  61.      *
  62.      * @param <V> the type of output of the {@code after} function, and of the composed function
  63.      * @param after the function to apply after this function is applied
  64.      * @return a composed function that first applies this function and then applies the {@code after} function
  65.      * @throws NullPointerException if after is null
  66.      * @see #compose(IOFunction)
  67.      */
  68.     default <V> IOFunction<T, V> andThen(final Function<? super R, ? extends V> after) {
  69.         Objects.requireNonNull(after, "after");
  70.         return (final T t) -> after.apply(apply(t));
  71.     }

  72.     /**
  73.      * Returns a composed {@link IOFunction} that first applies this function to its input, and then applies the
  74.      * {@code after} consumer to the result. If evaluation of either function throws an exception, it is relayed to the
  75.      * caller of the composed function.
  76.      *
  77.      * @param after the consumer to apply after this function is applied
  78.      * @return a composed function that first applies this function and then applies the {@code after} consumer
  79.      * @throws NullPointerException if after is null
  80.      * @see #compose(IOFunction)
  81.      */
  82.     default IOConsumer<T> andThen(final IOConsumer<? super R> after) {
  83.         Objects.requireNonNull(after, "after");
  84.         return (final T t) -> after.accept(apply(t));
  85.     }

  86.     /**
  87.      * Returns a composed {@link IOFunction} that first applies this function to its input, and then applies the
  88.      * {@code after} function to the result. If evaluation of either function throws an exception, it is relayed to the
  89.      * caller of the composed function.
  90.      *
  91.      * @param <V> the type of output of the {@code after} function, and of the composed function
  92.      * @param after the function to apply after this function is applied
  93.      * @return a composed function that first applies this function and then applies the {@code after} function
  94.      * @throws NullPointerException if after is null
  95.      * @see #compose(IOFunction)
  96.      */
  97.     default <V> IOFunction<T, V> andThen(final IOFunction<? super R, ? extends V> after) {
  98.         Objects.requireNonNull(after, "after");
  99.         return (final T t) -> after.apply(apply(t));
  100.     }

  101.     /**
  102.      * Applies this function to the given argument.
  103.      *
  104.      * @param t the function argument
  105.      * @return the function result
  106.      * @throws IOException if an I/O error occurs.
  107.      */
  108.     R apply(T t) throws IOException;

  109.     /**
  110.      * Creates a {@link Function} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
  111.      *
  112.      * @return an UncheckedIOException Function.
  113.      * @since 2.12.0
  114.      */
  115.     default Function<T, R> asFunction() {
  116.         return t -> Uncheck.apply(this, t);
  117.     }

  118.     /**
  119.      * Returns a composed {@link IOFunction} that first applies the {@code before} function to its input, and then applies
  120.      * this function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the
  121.      * composed function.
  122.      *
  123.      * @param <V> the type of input to the {@code before} function, and to the composed function
  124.      * @param before the function to apply before this function is applied
  125.      * @return a composed function that first applies the {@code before} function and then applies this function
  126.      * @throws NullPointerException if before is null
  127.      * @see #andThen(IOFunction)
  128.      */
  129.     default <V> IOFunction<V, R> compose(final Function<? super V, ? extends T> before) {
  130.         Objects.requireNonNull(before, "before");
  131.         return (final V v) -> apply(before.apply(v));
  132.     }

  133.     /**
  134.      * Returns a composed {@link IOFunction} that first applies the {@code before} function to its input, and then applies
  135.      * this function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the
  136.      * composed function.
  137.      *
  138.      * @param <V> the type of input to the {@code before} function, and to the composed function
  139.      * @param before the function to apply before this function is applied
  140.      * @return a composed function that first applies the {@code before} function and then applies this function
  141.      * @throws NullPointerException if before is null
  142.      * @see #andThen(IOFunction)
  143.      */
  144.     default <V> IOFunction<V, R> compose(final IOFunction<? super V, ? extends T> before) {
  145.         Objects.requireNonNull(before, "before");
  146.         return (final V v) -> apply(before.apply(v));
  147.     }

  148.     /**
  149.      * Returns a composed {@link IOFunction} that first applies the {@code before} function to its input, and then applies
  150.      * this function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the
  151.      * composed function.
  152.      *
  153.      * @param before the supplier which feeds the application of this function
  154.      * @return a composed function that first applies the {@code before} function and then applies this function
  155.      * @throws NullPointerException if before is null
  156.      * @see #andThen(IOFunction)
  157.      */
  158.     default IOSupplier<R> compose(final IOSupplier<? extends T> before) {
  159.         Objects.requireNonNull(before, "before");
  160.         return () -> apply(before.get());
  161.     }

  162.     /**
  163.      * Returns a composed {@link IOFunction} that first applies the {@code before} function to its input, and then applies
  164.      * this function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the
  165.      * composed function.
  166.      *
  167.      * @param before the supplier which feeds the application of this function
  168.      * @return a composed function that first applies the {@code before} function and then applies this function
  169.      * @throws NullPointerException if before is null
  170.      * @see #andThen(IOFunction)
  171.      */
  172.     default IOSupplier<R> compose(final Supplier<? extends T> before) {
  173.         Objects.requireNonNull(before, "before");
  174.         return () -> apply(before.get());
  175.     }
  176. }