All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.yudichev.jiotty.common.lang.CompositeException Maven / Gradle / Ivy

package net.yudichev.jiotty.common.lang;

import com.google.common.collect.ImmutableList;

import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static com.google.common.collect.ImmutableList.builder;

public final class CompositeException extends RuntimeException {
    private CompositeException(Collection exceptions) {
        super(exceptions.stream()
                .map(Throwable::getMessage)
                .collect(Collectors.joining("; ")));
    }

    public static  void runForAll(Iterable items, Consumer consumer) {
        ImmutableList.Builder exceptionListBuilder = builder();
        for (T item : items) {
            try {
                consumer.accept(item);
            } catch (RuntimeException e) {
                exceptionListBuilder.add(e);
            }
        }
        List exceptions = exceptionListBuilder.build();
        if (!exceptions.isEmpty()) {
            throw new CompositeException(exceptions);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy