| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package org.apache.commons.io.function; |
| |
| import java.io.IOException; |
| import java.util.ArrayList; |
| import java.util.List; |
| import java.util.concurrent.atomic.AtomicInteger; |
| import java.util.concurrent.atomic.AtomicReference; |
| import java.util.function.BiFunction; |
| import java.util.stream.Stream; |
| |
| import org.apache.commons.io.IOExceptionList; |
| |
| /** |
| * Keep this code package-private for now. |
| */ |
| class IOStreams { |
| |
| static <T> void forEach(final Stream<T> stream, final IOConsumer<T> action) throws IOException { |
| forEachIndexed(stream, action, (i, e) -> e); |
| } |
| |
| static <T> void forEachIndexed(final Stream<T> stream, final IOConsumer<T> action, final BiFunction<Integer, IOException, IOException> exSupplier) |
| throws IOExceptionList { |
| final AtomicReference<List<IOException>> causeList = new AtomicReference<>(); |
| final AtomicInteger index = new AtomicInteger(); |
| stream.forEach(e -> { |
| try { |
| action.accept(e); |
| } catch (final IOException ioex) { |
| if (causeList.get() == null) { |
| causeList.set(new ArrayList<>()); |
| } |
| causeList.get().add(exSupplier.apply(index.get(), ioex)); |
| } |
| index.incrementAndGet(); |
| }); |
| IOExceptionList.checkEmpty(causeList.get(), null); |
| } |
| |
| /** |
| * Null-safe version of {@link Stream#of(Object[])}. |
| * |
| * Copied from Apache Commons Lang. |
| * |
| * @param <T> the type of stream elements. |
| * @param values the elements of the new stream, may be {@code null}. |
| * @return the new stream on {@code values} or {@link Stream#empty()}. |
| */ |
| @SafeVarargs // Creating a stream from an array is safe |
| static <T> Stream<T> of(final T... values) { |
| return values == null ? Stream.empty() : Stream.of(values); |
| } |
| |
| } |