public interface FunctionWithExceptions<A, B, T extends Throwable>{ B apply(A a) throws T; }
public static <A, B, T extends Throwable> Collection<B> map(Collection<A> source, FunctionWithExceptions<A, B, T> function) throws T { Collection<B> result = new ArrayList<>(); for (A a : source) { result.add(function.apply(a)); } return result; }
public Collection<byte[]> singleException(Collection<String> filenames) throws IOException { return map(filenames, f -> Files.readAllBytes(new File(f).toPath()); }
public static byte[] waitAndRead(String filename, long time) throws InterruptedException, IOException { Thread.sleep(time); return Files.readAllBytes(new File(filename).toPath()); } public Collection<byte[]> joinedExceptions(Collection<String> filenames) throws Exception { return map(filenames, f -> waitAndRead(f, 1000L)); }
private <T> T wait(T t, long time) throws InterruptedException { Thread.sleep(time); return t; } private byte[] read(String filename) throws IOException { return Files.readAllBytes(new File(filename).toPath()); } public Collection<byte[]> separatedExceptions(Collection<String> filenames) throws InterruptedException { try { return map(map(filenames, f -> wait(f, 1000L)), f -> read(f)); } catch (IOException e) { return Collections.emptyList(); } }
public Collection<Boolean> noExceptions(Collection<String> filenames) { return Mapper.map(filenames, f -> new File(f).exists()); }
public class ErStream<S, T extends Throwable> { private final Stream<S> mainStream; public ErStream(Stream<S> mainStream) { this.mainStream = mainStream; } public static <S, R, T extends Throwable, T1 extends T, T2 extends T> ErStream<R, T> map(ErStream<S, T1> erStream, FunctionWithExceptions<S, R, T2> function) { Function<S, R> f = uncheck(function); return new ErStream<>(erStream.mainStream.map(f)); } public static <S, T extends Throwable> Optional<S> findAny(ErStream<S, T> erStream) throws T { return erStream.mainStream.findAny(); } // Djaler private static <S, R> Function<S, R> uncheck(FunctionWithExceptions<S, R, ?> function) { return t -> { try { return function.apply(t); } catch (Throwable exception) { throwAsUnchecked(exception); return null; } }; } // Djaler @SuppressWarnings("unchecked") private static <E extends Throwable> void throwAsUnchecked(Throwable exception) throws E { throw (E) exception; }
private static byte[] read(String filename) throws EOFException { return null; } private static String search(String filename) throws FileNotFoundException { return filename; } public static void main(String[] args) { List<String> list = Arrays.asList("1.txt"); //autogenerated code with wrong generic types //ErStream<Object, Throwable> temp = map(new ErStream<>(list.stream()), s -> search(s)); //store single exception ErStream<String, FileNotFoundException> temp = map(new ErStream<>(list.stream()), s -> search(s)); try {//exception will be thrown here findAny(temp); } catch (FileNotFoundException e) {/*NOTHING*/} temp = map(new ErStream<>(list.stream()), s -> search(s)); try {//most general exception will be thrown here findAny(map(temp,s -> read(s))); } catch (IOException e) {/*NOTHING*/} //without exception, nothing thrown findAny(map(new ErStream<>(list.stream()), i -> i + 1)); }
Source: https://habr.com/ru/post/341524/