Created
August 23, 2020 09:22
-
-
Save electroCutie/f1c0a6a89f15f9c024137e6dc92dc2a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cloud.literallya.util; | |
import java.util.function.BiConsumer; | |
import java.util.function.Consumer; | |
import java.util.function.Function; | |
import java.util.function.Predicate; | |
import java.util.function.Supplier; | |
public class Exceptions{ | |
//@formatter:off | |
private Exceptions(){ throw new AssertionError("static only"); } | |
/* Interfaces */ | |
public static interface RunnableWithException{ public void run() throws Exception; } | |
public static interface ConsumerWithException<V> { public void accept(V v) throws Exception; } | |
public static interface SupplierWithException<T> { public T get() throws Exception; } | |
public static interface FunctionWithException<A, B> { public B apply(A a) throws Exception; } | |
public static interface PredicateWithException<A> { public boolean test(A a) throws Exception; } | |
//TODO: can we autogenerate? | |
public static interface BiConsumerWithException<A,B> { public void accept(A a, B b) throws Exception; } | |
public static interface TriConsumerWithException<A,B,C> { public void accept(A a, B b, C c) throws Exception; } | |
public static interface BiFunctionWithException<A,B,C> { public C apply(A a, B b) throws Exception; } | |
public static interface TriFunctionWithException<A,B,C,D> { public D apply(A a, B b, C c) throws Exception; } | |
public static interface QuadFunctionWithException<A,B,C,D,E> { public E apply(A a, B b, C c, D d) throws Exception; } | |
/** | |
* Check if the given exception can be re-thrown in an unchecked way and if not wraps it so it can be thrown unchecked. Then throws it. | |
* This function claims to return an Error, it does not. This is so the caller can prefix this with throw to convince the compier that this will not complete normally | |
* @throws An Unchecked Exception whenver called | |
*/ | |
public static Error throwOrWrap(Throwable e) { | |
if(e instanceof Error) | |
throw (Error) e; | |
if(e instanceof RuntimeException) | |
throw (RuntimeException) e; | |
throw new RuntimeException(e); | |
} | |
/* Wrap */ | |
public static Runnable wrapR(RunnableWithException r){ | |
return () -> { try{ r.run(); }catch(Throwable e){throwOrWrap(e);} }; } | |
public static <V> Consumer<V> wrapC(ConsumerWithException<V> c){ | |
return o -> { try{ c.accept(o); }catch(Throwable e){throwOrWrap(e);} }; } | |
public static <T> Supplier<T> wrapS(SupplierWithException<T> s){ | |
return () -> { try{return s.get(); }catch(Throwable e){throw throwOrWrap(e);} }; } | |
public static <A, B> Function<A, B> wrapF(FunctionWithException<A, B> f){ | |
return a -> { try{ return f.apply(a); }catch(Throwable e){throw throwOrWrap(e);} }; } | |
public static <A> Predicate<A> wrapP(PredicateWithException<A> f){ | |
return a -> { try{ return f.test(a); }catch(Throwable e){throw throwOrWrap(e);} }; } | |
/* Custom Wrap */ | |
public static Runnable wrapR(Function<Throwable, ? extends RuntimeException> factory, RunnableWithException r){ | |
return () -> { try{ r.run(); }catch(Exception e){ throw factory.apply(e); } }; } | |
public static <V> Consumer<V> wrapC(Function<Throwable, ? extends RuntimeException> factory, ConsumerWithException<V> c){ | |
return o -> { try{ c.accept(o); }catch(Exception e){ throw factory.apply(e);} }; } | |
public static <T> Supplier<T> wrapS(Function<Throwable, ? extends RuntimeException> factory, SupplierWithException<T> s){ | |
return () -> { try{return s.get(); }catch(Exception e){ throw factory.apply(e);} }; } | |
public static <A, B> Function<A, B> wrapF(Function<Throwable, ? extends RuntimeException> factory, FunctionWithException<A, B> f){ | |
return a -> { try{ return f.apply(a); }catch(Exception e){ throw factory.apply(e);} }; } | |
public static <A> Predicate<A> wrapP(Function<Throwable, ? extends RuntimeException> factory, PredicateWithException<A> f){ | |
return a -> { try{ return f.test(a); }catch(Exception e){ throw factory.apply(e);} }; } | |
/* Run */ | |
public static void runR(Function<Throwable, ? extends RuntimeException> factory, RunnableWithException r){ | |
try{ r.run(); }catch(RuntimeException | Error e) {throw e;}catch(Exception e){ throw factory.apply(e); } } | |
public static void runR(RunnableWithException r){ runR(RuntimeException::new, r); } | |
public static <V> void runC(Function<Throwable, ? extends RuntimeException> factory, ConsumerWithException<V> c, V o){ | |
try{ c.accept(o); }catch(RuntimeException | Error e) {throw e;}catch(Exception e){ throw factory.apply(e);} } | |
public static <V> void runC(ConsumerWithException<V> c, V o){ runC(RuntimeException::new, c, o); } | |
public static <T> T runS(SupplierWithException<T> s, T errVal){ | |
try{ return s.get(); }catch(Exception e){ return errVal;} } | |
public static <T> T runS(Function<Throwable, ? extends RuntimeException> factory, SupplierWithException<T> s){ | |
try{ return s.get(); }catch(Exception e){ throw factory.apply(e);} } | |
public static <T> T runS(SupplierWithException<T> s){ return runS(e -> new RuntimeException(e), s);} | |
public static <A, B> B runF(FunctionWithException<A, B> f, A a){ | |
try{ return f.apply(a); }catch(Throwable e){throw throwOrWrap(e);} } | |
public static <A, B> B runF(Function<Throwable, ? extends RuntimeException> factory, FunctionWithException<A, B> f, A a){ | |
try{ return f.apply(a); }catch(Throwable e){throw throwOrWrap(e);} } | |
public static <A> boolean runP(PredicateWithException<A> f, A a){ | |
try{ return f.test(a); }catch(Throwable e){throw throwOrWrap(e);} } | |
/* Swallow */ | |
public static Runnable swallowR(RunnableWithException r){ | |
return () -> { try{ r.run(); }catch(Throwable e){}}; } | |
public static <V> Consumer<V> swallowC(ConsumerWithException<V> c){ | |
return o -> { try{ c.accept(o); }catch(Throwable e){} }; } | |
public static <T> Supplier<T> swallowS(SupplierWithException<T> s){ | |
return () -> { try{ return s.get(); }catch(Throwable e){} return null; }; } | |
public static <T> Supplier<T> swallowS(SupplierWithException<T> s, T errVal){ | |
return () -> { try{ return s.get(); }catch(Throwable e){} return errVal; }; } | |
public static <A, B> Function<A, B> swallowF(FunctionWithException<A, B> f){ | |
return a -> { try{ return f.apply(a); }catch(Throwable e){} return null; }; } | |
public static <A> Predicate<A> swallowP(boolean errValue, PredicateWithException<A> f){ | |
return a -> { try{ return f.test(a); }catch(Throwable e){} return errValue; }; } | |
/* Swallow-Run */ | |
public static void swallowRunR(RunnableWithException r){ | |
try{ r.run(); }catch(Throwable e){} } | |
public static <V> void swallowRunC(ConsumerWithException<V> c, V o){ | |
try{ c.accept(o); }catch(Throwable e){} } | |
/* Run-handle */ | |
public static void runHandleR(RunnableWithException r, Consumer<Throwable> handler){ | |
try{ r.run(); }catch(Throwable e){handler.accept(e);} } | |
public static <V> void runHandleC(ConsumerWithException<V> c, Consumer<Throwable> handler, V o){ | |
try{ c.accept(o); }catch(Throwable e){handler.accept(e);} } | |
public static <V> void runHandleC(ConsumerWithException<V> c, BiConsumer<Throwable, V> handler, V o){ | |
try{ c.accept(o); }catch(Throwable e){handler.accept(e, o);} } | |
/* Run-handle-throw */ | |
public static void runPeekR(RunnableWithException r, Consumer<Throwable> handler){ | |
try{ r.run(); }catch(Throwable e){handler.accept(e); throw Exceptions.throwOrWrap(e);} } | |
public static <V> void runPeekC(ConsumerWithException<V> c, Consumer<Throwable> handler, V o){ | |
try{ c.accept(o); }catch(Throwable e){handler.accept(e); throw Exceptions.throwOrWrap(e);} } | |
public static <V> void runPeekC(ConsumerWithException<V> c, BiConsumer<Throwable, V> handler, V o){ | |
try{ c.accept(o); }catch(Throwable e){handler.accept(e, o); throw Exceptions.throwOrWrap(e);} } | |
//@formatter:on | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment