IOStreams.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.util.function.BiFunction;
  20. import java.util.stream.Stream;
  21. import java.util.stream.StreamSupport;

  22. import org.apache.commons.io.IOExceptionList;
  23. import org.apache.commons.io.IOIndexedException;

  24. /**
  25.  * Keep this code package-private for now.
  26.  */
  27. final class IOStreams {

  28.     static final Object NONE = new Object();

  29.     static <T> void forAll(final Stream<T> stream, final IOConsumer<T> action) throws IOExceptionList {
  30.         forAll(stream, action, (i, e) -> e);
  31.     }

  32.     @SuppressWarnings("resource") // adapt()
  33.     static <T> void forAll(final Stream<T> stream, final IOConsumer<T> action, final BiFunction<Integer, IOException, IOException> exSupplier)
  34.         throws IOExceptionList {
  35.         IOStream.adapt(stream).forAll(action, IOIndexedException::new);
  36.     }

  37.     @SuppressWarnings("unused") // IOStreams.rethrow() throws
  38.     static <T> void forEach(final Stream<T> stream, final IOConsumer<T> action) throws IOException {
  39.         final IOConsumer<T> actualAction = toIOConsumer(action);
  40.         of(stream).forEach(e -> Erase.accept(actualAction, e));
  41.     }

  42.     /**
  43.      * Null-safe version of {@link StreamSupport#stream(java.util.Spliterator, boolean)}.
  44.      *
  45.      * Copied from Apache Commons Lang.
  46.      *
  47.      * @param <T> the type of stream elements.
  48.      * @param values the elements of the new stream, may be {@code null}.
  49.      * @return the new stream on {@code values} or {@link Stream#empty()}.
  50.      */
  51.     static <T> Stream<T> of(final Iterable<T> values) {
  52.         return values == null ? Stream.empty() : StreamSupport.stream(values.spliterator(), false);
  53.     }

  54.     static <T> Stream<T> of(final Stream<T> stream) {
  55.         return stream == null ? Stream.empty() : stream;
  56.     }

  57.     /**
  58.      * Null-safe version of {@link Stream#of(Object[])}.
  59.      *
  60.      * Copied from Apache Commons Lang.
  61.      *
  62.      * @param <T> the type of stream elements.
  63.      * @param values the elements of the new stream, may be {@code null}.
  64.      * @return the new stream on {@code values} or {@link Stream#empty()}.
  65.      */
  66.     @SafeVarargs // Creating a stream from an array is safe
  67.     static <T> Stream<T> of(final T... values) {
  68.         return values == null ? Stream.empty() : Stream.of(values);
  69.     }

  70.     static <T> IOConsumer<T> toIOConsumer(final IOConsumer<T> action) {
  71.         return action != null ? action : IOConsumer.noop();
  72.     }

  73.     private IOStreams() {
  74.         // no instances
  75.     }
  76. }