/** * Creates a timed runnable. * @param meter the call meter to use * @param runnable the original runnable * @return a timed runnable */ static Runnable decorateRunnable(CallMeterBase meter, Runnable runnable){ return () -> { final Timer timer = meter.startTimer(); try { runnable.run(); timer.onSuccess(); }catch (Throwable e){ timer.onError(); throw e; } }; }
/** * Creates a timed checked supplier. * * @param meter the call meter to use * @param supplier the original supplier * @return a timed supplier */ static <T> Supplier<T> decorateSupplier(CallMeterBase meter, Supplier<T> supplier){ return () -> { final Timer timer = meter.startTimer(); try { final T returnValue = supplier.get(); timer.onSuccess(); return returnValue; }catch (Throwable e){ timer.onError(); throw e; } }; }
/** * Creates a timed function. * * @param meter the call meter to use * @param function the original function * @return a timed function */ static <T, R> Function<T, R> decorateFunction(CallMeterBase meter, Function<T, R> function){ return (T t) -> { final Timer timer = meter.startTimer(); try { R returnValue = function.apply(t); timer.onSuccess(); return returnValue; }catch (Throwable e){ timer.onError(); throw e; } }; }
/** * Creates a timed Callable. * * @param meter the call meter to use * @param callable the original Callable * @return a timed Callable */ static <T> Callable<T> decorateCallable(CallMeterBase meter, Callable<T> callable){ return () -> { final Timer timer = meter.startTimer(); try { final T returnValue = callable.call(); timer.onSuccess(); return returnValue; }catch (Throwable e){ timer.onError(); throw e; } }; }
/** * Creates a timed function. * * @param meter the call meter to use * @param function the original function * @return a timed function */ static <T, R> CheckedFunction1<T, R> decorateCheckedFunction(CallMeterBase meter, CheckedFunction1<T, R> function){ return (T t) -> { final Timer timer = meter.startTimer(); try { R returnValue = function.apply(t); timer.onSuccess(); return returnValue; }catch (Throwable e){ timer.onError(); throw e; } }; }
/** * Creates a timed checked supplier. * * @param meter the call meter to use * @param supplier the original supplier * @return a timed supplier */ static <T> CheckedFunction0<T> decorateCheckedSupplier(CallMeterBase meter, CheckedFunction0<T> supplier){ return () -> { final Timer timer = meter.startTimer(); try { final T returnValue = supplier.apply(); timer.onSuccess(); return returnValue; }catch (Throwable e){ timer.onError(); throw e; } }; }
/** * Creates a timed runnable. * * @param meter the call meter to use * @param runnable the original runnable * @return a timed runnable */ static CheckedRunnable decorateCheckedRunnable(CallMeterBase meter, CheckedRunnable runnable){ return () -> { final Timer timer = meter.startTimer(); try { runnable.run(); timer.onSuccess(); }catch (Throwable e){ timer.onError(); throw e; } }; }
/** * Decorates completion stage supplier with call meter * * @param meter the call meter to use * @param stageSupplier the CompletionStage Supplier * @return a decorated completion stage */ static <T> Supplier<CompletionStage<T>> decorateCompletionStageSupplier(CallMeterBase meter, Supplier<CompletionStage<T>> stageSupplier) { return () -> { final Timer timer = meter.startTimer(); try { final CompletionStage<T> stage = stageSupplier.get(); stage.whenComplete((result, throwable) -> { if (throwable != null) { timer.onError(); } else { timer.onSuccess(); } }); return stage; } catch (Throwable throwable) { timer.onError(); throw throwable; } }; }