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 18 package org.apache.commons.io.function; 19 20 import java.io.IOException; 21 import java.util.function.BiFunction; 22 import java.util.stream.Stream; 23 import java.util.stream.StreamSupport; 24 25 import org.apache.commons.io.IOExceptionList; 26 import org.apache.commons.io.IOIndexedException; 27 28 /** 29 * Keep this code package-private for now. 30 */ 31 final class IOStreams { 32 33 static final Object NONE = new Object(); 34 forAll(final Stream<T> stream, final IOConsumer<T> action)35 static <T> void forAll(final Stream<T> stream, final IOConsumer<T> action) throws IOExceptionList { 36 forAll(stream, action, (i, e) -> e); 37 } 38 39 @SuppressWarnings("resource") // adapt() forAll(final Stream<T> stream, final IOConsumer<T> action, final BiFunction<Integer, IOException, IOException> exSupplier)40 static <T> void forAll(final Stream<T> stream, final IOConsumer<T> action, final BiFunction<Integer, IOException, IOException> exSupplier) 41 throws IOExceptionList { 42 IOStream.adapt(stream).forAll(action, IOIndexedException::new); 43 } 44 45 @SuppressWarnings("unused") // IOStreams.rethrow() throws forEach(final Stream<T> stream, final IOConsumer<T> action)46 static <T> void forEach(final Stream<T> stream, final IOConsumer<T> action) throws IOException { 47 final IOConsumer<T> actualAction = toIOConsumer(action); 48 of(stream).forEach(e -> Erase.accept(actualAction, e)); 49 } 50 51 /** 52 * Null-safe version of {@link StreamSupport#stream(java.util.Spliterator, boolean)}. 53 * 54 * Copied from Apache Commons Lang. 55 * 56 * @param <T> the type of stream elements. 57 * @param values the elements of the new stream, may be {@code null}. 58 * @return the new stream on {@code values} or {@link Stream#empty()}. 59 */ of(final Iterable<T> values)60 static <T> Stream<T> of(final Iterable<T> values) { 61 return values == null ? Stream.empty() : StreamSupport.stream(values.spliterator(), false); 62 } 63 of(final Stream<T> stream)64 static <T> Stream<T> of(final Stream<T> stream) { 65 return stream == null ? Stream.empty() : stream; 66 } 67 68 /** 69 * Null-safe version of {@link Stream#of(Object[])}. 70 * 71 * Copied from Apache Commons Lang. 72 * 73 * @param <T> the type of stream elements. 74 * @param values the elements of the new stream, may be {@code null}. 75 * @return the new stream on {@code values} or {@link Stream#empty()}. 76 */ 77 @SafeVarargs // Creating a stream from an array is safe of(final T... values)78 static <T> Stream<T> of(final T... values) { 79 return values == null ? Stream.empty() : Stream.of(values); 80 } 81 toIOConsumer(final IOConsumer<T> action)82 static <T> IOConsumer<T> toIOConsumer(final IOConsumer<T> action) { 83 return action != null ? action : IOConsumer.noop(); 84 } 85 IOStreams()86 private IOStreams() { 87 // no instances 88 } 89 } 90