I am trying to understand why my code throws IllegalStateException, and not my user code , which I hoped it would throw.
public final class CollectorUtils {
private CollectorUtils() {
throw new UnsupportedOperationException();
}
public static <E, R, X extends Throwable> Collector<E, ?, R> listAndThenCollector(final Predicate<List<E>> listPredicate, final Function<List<E>, R> listFunction, final Function<Throwable, X> exceptionWrapper) throws X {
Objects.requireNonNull(listPredicate);
Objects.requireNonNull(listFunction);
Objects.requireNonNull(exceptionWrapper);
try {
return Collectors.collectingAndThen(Collectors.toList(), list -> {
if (!listPredicate.test(list)) {
throw new IllegalStateException();
}
return listFunction.apply(list);
});
} catch (IllegalStateException ex) {
throw exceptionWrapper.apply(ex);
}
}
public static <E> Collector<E, ?, E> singleElementCollector() throws NotASingleElementException {
return listAndThenCollector(list -> list.size() == 1, list -> list.get(0), NotASingleElementException::new);
}
}
Dropped IllegalStateExceptionin the line: throw new IllegalStateException().
Usage example:
public static void test() {
try {
Integer result = IntStream.range(0, 2)
.boxed()
.collect(CollectorUtils.singleElementCollector());
} catch (NotASingleElementException ex) {
Logger.getLogger(CollectorUtils.class.getName()).log(Level.SEVERE, null, ex);
}
}
This code should call NotASingleElementException, instead it throws IllegalStateException, how can I make it work?
The straightforwardness of the wrong behavior when doing the actual work:
Exception in thread "pool-3-thread-1" java.lang.IllegalStateException
at dpc2.base.utils.CollectorUtils.lambda$listAndThenCollector$0(CollectorUtils.java:28)
at dpc2.base.utils.CollectorUtils$$Lambda$21/2071035411.apply(Unknown Source)
at java.util.function.Function.lambda$andThen$6(Function.java:88)
at java.util.function.Function$$Lambda$22/63121782.apply(Unknown Source)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:503)
at dpc2.base.utils.ImageMagickUtils.convertPDFToTIFF(ImageMagickUtils.java:30)
at dpc2.server.convert.ConvertConsumer.accept(ConvertConsumer.java:20)
at dpc2.server.convert.ConvertConsumer.accept(ConvertConsumer.java:14)
at dpc2.base.checker.BaseChecker.lambda$null$0(BaseChecker.java:116)
at dpc2.base.checker.BaseChecker$$Lambda$15/2121862243.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
source
share