@Override public void configure(final Env env, final Config conf, final Binder binder) throws Throwable { this.watcher = FileSystems.getDefault().newWatchService(); binder.bind(WatchService.class).toInstance(watcher); List<FileEventOptions> paths = new ArrayList<>(); paths(env.getClass().getClassLoader(), conf, "filewatcher.register", options -> paths.add(register(binder, options))); for (Throwing.Function2<Config, Binder, FileEventOptions> binding : bindings) { paths.add(binding.apply(conf, binder)); } binder.bind(FileMonitor.class).asEagerSingleton(); paths.forEach(it -> log.info("Watching: {}", it)); }
/** * A function that remember/cache previous executions. * * @return A memo function. */ default Function2<V1, V2, R> memoized() { if (this instanceof Memoized) { return this; } Map<Object, R> cache = new HashMap<>(); return (Function2<V1, V2, R> & Memoized) (v1, v2) -> memo(cache, Arrays.asList(v1, v2), () -> tryApply(v1, v2)); } }
/** * * Apply this function and run the given action in case of exception. * * @param type Exception filter. * @param action Action to run when exception occurs. * @param <X> Exception type. * @return A new function. */ default <X extends Throwable> Function2<V1, V2, R> onFailure(Class<X> type, java.util.function.Consumer<X> action) { return (v1, v2) -> fnOnFailure(() -> tryApply(v1, v2), type, action); }
public MappedHandler(final Throwing.Function2<Request, Response, Object> supplier, final Route.Mapper mapper) { this((req, rsp, chain) -> supplier.apply(req, rsp), mapper); }
/** * Apply this function and wrap any resulting exception. * * @param wrapper Exception wrapper. * @return A new function. */ default Function2<V1, V2, R> wrap(java.util.function.Function<Throwable, Exception> wrapper) { return (v1, v2) -> fnWrap(() -> tryApply(v1, v2), wrapper); }
public <V> Value<V> apply(Throwing.Function2<R1, R2, V> fn) { return Try.apply(() -> { try (R1 r1 = this.r1.get(); R2 r2 = this.r2.get()) { return fn.apply(r1, r2); } }); }
/** * Apply this function or recover from it in case of exception. * * @param fn Exception recover. * @return A new function. */ default Function2<V1, V2, R> recover(java.util.function.Function<Throwable, R> fn) { return recover(Throwable.class, fn); }
/** * Apply this function and returns the given default value in case of exception. * * @param defaultValue Exceptional default value. * @return A new function. */ default Function2<V1, V2, R> orElse(Supplier<R> defaultValue) { return (v1, v2) -> fn(() -> tryApply(v1, v2), defaultValue); }
/** * Apply this function to the given argument and produces a result. * * @param v1 Input argument. * @param v2 Input argument. * @return Result. */ @Override default R apply(V1 v1, V2 v2) { return fn(() -> tryApply(v1, v2)); }
/** * Apply this function and run the given action in case of exception. * * @param action Action to run when exception occurs. * @return A new function. */ default Function2<V1, V2, R> onFailure(java.util.function.Consumer<Throwable> action) { return onFailure(Throwable.class, action); }
@Override public void configure(final Env env, final Config conf, final Binder binder) throws Throwable { this.watcher = FileSystems.getDefault().newWatchService(); binder.bind(WatchService.class).toInstance(watcher); List<FileEventOptions> paths = new ArrayList<>(); paths(env.getClass().getClassLoader(), conf, "filewatcher.register", options -> paths.add(register(binder, options))); for (Throwing.Function2<Config, Binder, FileEventOptions> binding : bindings) { paths.add(binding.apply(conf, binder)); } binder.bind(FileMonitor.class).asEagerSingleton(); paths.forEach(it -> log.info("Watching: {}", it)); }
/** * Apply this function or recover from a specific exception in case of exception. * * @param type Exception filter. * @param fn Exception recover. * @param <X> Exception type. * @return A new function. */ default <X extends Throwable> Function2<V1, V2, R> recover(Class<? extends X> type, java.util.function.Function<X, R> fn) { return (v1, v2) -> fnRecover(() -> tryApply(v1, v2), type, fn); }
private URL resolve(final Request req, final String path) throws Throwable { String target = fn.apply(req, path); return resolve(target); }
/** * Apply this function and returns the given default value in case of exception. * * @param defaultValue Exceptional default value. * @return A new function. */ default Function2<V1, V2, R> orElse(R defaultValue) { return orElse(() -> defaultValue); }
/** * Apply this function and unwrap any resulting exception. Useful to get clean/shorter stacktrace. * * @param type Exception to unwrap. * @param <X> Exception type. * @return A new function. */ default <X extends Throwable> Function2<V1, V2, R> unwrap(Class<? extends X> type) { return (v1, v2) -> fnUnwrap(() -> tryApply(v1, v2), type); }
private URL resolve(final Request req, final String path) throws Throwable { String target = fn.apply(req, path); return resolve(target); }
public MappedHandler(final Throwing.Function2<Request, Response, Object> supplier, final Route.Mapper mapper) { this((req, rsp, chain) -> supplier.apply(req, rsp), mapper); }