org.jooby.funzy.Try类的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(6.5k)|赞(0)|评价(0)|浏览(354)

本文整理了Java中org.jooby.funzy.Try类的一些代码示例,展示了Try类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Try类的具体详情如下:
包路径:org.jooby.funzy.Try
类名称:Try

Try介绍

[英]Functional try and try-with-resources implementation.
[中]功能性的尝试和资源实现的尝试。

代码示例

代码示例来源:origin: jooby-project/jooby

  1. @Override
  2. public void writeSuccess() {
  3. Try.run(success::invoke)
  4. .onFailure(cause -> log.error("Error while invoking success callback", cause));
  5. }

代码示例来源:origin: jooby-project/jooby

  1. public boolean exists(final Predicate<Path> filter) {
  2. try (Stream<Path> walk = Try.apply(() -> Files.walk(root)).orElse(Stream.empty())) {
  3. return walk
  4. .skip(1)
  5. .anyMatch(filter);
  6. }
  7. }

代码示例来源:origin: jooby-project/jooby

  1. /**
  2. * Execute the given nodejs callback and automatically releaseNow v8 and nodejs resources.
  3. *
  4. * @param basedir Base dir where to deploy a library.
  5. * @param callback Nodejs callback.
  6. */
  7. public static void run(final File basedir, final Throwing.Consumer<Nodejs> callback) {
  8. Nodejs node = new Nodejs(basedir);
  9. Try.run(() -> callback.accept(node))
  10. .onComplete(node::release)
  11. .throwException();
  12. }
  13. }

代码示例来源:origin: jooby-project/jooby

  1. private static void copy(final InputStream in, final OutputStream out) {
  2. Try.of(in, out)
  3. .run(ByteStreams::copy)
  4. .throwException();
  5. }

代码示例来源:origin: jooby-project/jooby

  1. public void release() {
  2. Try.run(mem::release)
  3. .onComplete(() -> releaseNow(v8));
  4. }

代码示例来源:origin: jooby-project/jooby

  1. log.debug("{}", uri);
  2. Path outdir = this.basedir.toPath().resolve("node_modules").resolve(library.replace("/", "."));
  3. Optional<Path> basedir = Try.apply(() -> Paths.get(uri)).toOptional();
  4. String libroot = Route.normalize("/" + library);
  5. try (Library lib = loadLibrary(uri)) {
  6. .toArray(new StandardCopyOption[this.coptions.size()]);
  7. Try.run(() -> Files.copy(it, output, coptions))
  8. .onFailure(x -> log.error("can't copy {}", it, x));

代码示例来源:origin: jooby-project/jooby

  1. public void end() {
  2. try {
  3. if (rollbackOnly) {
  4. rollback();
  5. } else {
  6. commit();
  7. }
  8. } finally {
  9. if (readOnly) {
  10. setConnectionReadOnly(false);
  11. }
  12. String sessionId = oid(session);
  13. log.debug("closing session: {}", sessionId);
  14. Try.run(session::close)
  15. .onFailure(x -> log.error("session.close() resulted in exception: {}", sessionId, x))
  16. .onSuccess(() -> log.debug("session closed: {}", sessionId));
  17. unbind(session.getSessionFactory());
  18. }
  19. }

代码示例来源:origin: jooby-project/jooby

  1. Try.run(() -> {
  2. method.setAccessible(true);
  3. method.invoke(owner);
  4. }).unwrap(InvocationTargetException.class)
  5. .throwException();
  6. });

代码示例来源:origin: jooby-project/jooby

  1. File workdir = new File(Try.apply(() -> conf.getString("application.tmpdir"))
  2. .orElse(System.getProperty("java.io.tmpdir")));
  3. Float h = new Float(params.getDouble(3));
  4. V8Function callback = (V8Function) params.get(4);
  5. Try.run(() -> {
  6. try (FileReader in = new FileReader(svgPath);
  7. OutputStream out = new FileOutputStream(pngPath)) {
  8. .onSuccess(() -> callback.call(null, null))
  9. .onFailure(x -> {
  10. log.debug("png-fallback resulted in exception", x);
  11. callback.call(null, toV8Array(v8, Arrays.asList(x.getMessage())));
  12. sha1files.forEach(it -> Try.run(() -> Files.delete(it)));

代码示例来源:origin: jooby-project/jooby

  1. private Jooby hackInjector(final Jooby app) {
  2. Try.run(() -> {
  3. Field field = Jooby.class.getDeclaredField("injector");
  4. field.setAccessible(true);
  5. Injector injector = proxyInjector(getClass().getClassLoader(), registry);
  6. field.set(app, injector);
  7. registry.put(Key.get(Injector.class), injector);
  8. }).throwException();
  9. return app;
  10. }

代码示例来源:origin: jooby-project/jooby

  1. @Override
  2. public void tryRun() throws Throwable {
  3. if (dep == null) {
  4. return;
  5. }
  6. Try.run(() -> {
  7. Optional<Method> shutdown = Arrays.stream(dep.getClass().getMethods())
  8. .filter(m -> m.getName().startsWith("shutdown")
  9. && m.getParameterCount() == 0
  10. && Modifier.isPublic(m.getModifiers()))
  11. .findFirst();
  12. if (shutdown.isPresent()) {
  13. log.debug("stopping {}", dep);
  14. shutdown.get().invoke(dep);
  15. } else {
  16. log.debug("no shutdown method found for: {}", dep);
  17. }
  18. }).unwrap(InvocationTargetException.class)
  19. .onComplete(() -> this.dep = null)
  20. .throwException();
  21. }

代码示例来源:origin: jooby-project/jooby

  1. private static List<URI> expandPath(final ClassLoader loader, final String pattern) {
  2. List<URI> result = new ArrayList<>();
  3. File file = new File(pattern);
  4. if (file.exists()) {
  5. result.add(file.toURI());
  6. }
  7. Try.run(() -> Collections.list(loader.getResources(pattern))
  8. .stream()
  9. .map(it -> Try.apply(it::toURI).get())
  10. .forEach(result::add));
  11. return result;
  12. }

代码示例来源:origin: jooby-project/jooby

  1. /**
  2. * Release nodejs and v8 resources.
  3. */
  4. public void release() {
  5. Try.run(scope::release);
  6. node.release();
  7. }

代码示例来源:origin: org.jooby/funzy

  1. /**
  2. * Run the given action if and only if this is a failure.
  3. *
  4. * @param action Failure action/listener.
  5. * @return This try.
  6. */
  7. @Override public Value<V> onFailure(final Consumer<? super Throwable> action) {
  8. super.onFailure(action);
  9. return this;
  10. }

代码示例来源:origin: jooby-project/jooby

  1. @Override
  2. public void send(final ByteBuffer buffer) throws Exception {
  3. Try.of(Channels.newChannel(rsp.getOutputStream()))
  4. .run(channel -> channel.write(buffer))
  5. .onSuccess(() -> committed = true);
  6. }

代码示例来源:origin: org.jooby/funzy

  1. /**
  2. * Always run the given action, works like a finally clause.
  3. *
  4. * @param action Finally action.
  5. * @return This try result.
  6. */
  7. @Override public Value<V> onComplete(Throwing.Runnable action) {
  8. return (Value<V>) super.onComplete(action);
  9. }

代码示例来源:origin: org.jooby/funzy

  1. @Override public <X extends Throwable> Value<V> unwrap(Class<? extends X> type) {
  2. return (Value<V>) super.unwrap(type);
  3. }

代码示例来源:origin: org.jooby/funzy

  1. /**
  2. * Run the given action if and only if this is a success.
  3. *
  4. * @param action Success listener.
  5. * @return This try.
  6. */
  7. @Override public Value<V> onSuccess(final Runnable action) {
  8. super.onSuccess(action);
  9. return this;
  10. }

代码示例来源:origin: jooby-project/jooby

  1. buckets.forEach(n -> Try.apply(() -> r.require(n, Bucket.class).close())
  2. .onFailure(x -> log.debug("bucket {} close operation resulted in exception", n, x))
  3. .orElse(false));
  4. Try.run(cluster::disconnect)
  5. .onFailure(x -> log.debug("disconnect operation resulted in exception", x));
  6. Try.run(cenv::shutdown)
  7. .onFailure(x -> log.debug("environment shutdown resulted in exception", x));
  8. });

代码示例来源:origin: jooby-project/jooby

  1. @Override
  2. default void handle(final Request req, final Response rsp, final Chain chain) throws Throwable {
  3. Sse sse = req.require(Sse.class);
  4. String path = req.path();
  5. rsp.send(new Deferred(deferred -> {
  6. try {
  7. sse.handshake(req, () -> {
  8. Try.run(() -> handle(req, sse))
  9. .onSuccess(() -> deferred.resolve(null))
  10. .onFailure(ex -> {
  11. deferred.reject(ex);
  12. Logger log = LoggerFactory.getLogger(Sse.class);
  13. log.error("execution of {} resulted in error", path, ex);
  14. });
  15. });
  16. } catch (Exception ex) {
  17. deferred.reject(ex);
  18. }
  19. }));
  20. }

相关文章