org.jooby.funzy.Try.apply()方法的使用及代码示例

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

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

Try.apply介绍

[英]Creates a new try from given value provider.
[中]从给定的值提供程序创建新的尝试。

代码示例

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

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

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

/**
 * Decode a path by delegating to {@link URLDecoder#decode(String, String)}.
 *
 * @param path Path to decoded.
 * @return Decode a path by delegating to {@link URLDecoder#decode(String, String)}.
 */
static String decode(String path) {
 return Try.apply(() -> URLDecoder.decode(path, "UTF-8")).get();
}

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

static final Object strToObject(final String value) {
 if (value == null || !value.startsWith(PREFIX)) {
  return value;
 }
 return Try.apply(() -> {
  byte[] bytes = base64().decode(value.substring(PREFIX.length()));
  // TODO: sanitization of arguments
  return new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject();
 }).get();
}

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

@Override
public long getLastModified(final Path handle) {
 return Try.apply(() -> Files.getLastModifiedTime(handle).toMillis()).orElse(-1L);
}

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

private static List<URI> expandPath(final ClassLoader loader, final String pattern) {
 List<URI> result = new ArrayList<>();
 File file = new File(pattern);
 if (file.exists()) {
  result.add(file.toURI());
 }
 Try.run(() -> Collections.list(loader.getResources(pattern))
   .stream()
   .map(it -> Try.apply(it::toURI).get())
   .forEach(result::add));
 return result;
}

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

public static final Object strToObject(final String value) {
 if (value == null || !value.startsWith(PREFIX)) {
  return value;
 }
 return Try.apply(() -> {
  byte[] bytes = BaseEncoding.base64().decode(value.substring(PREFIX.length()));
  // TODO: sanitization of arguments
  return new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject();
 })
   .wrap(x -> new IllegalArgumentException("Can't de-serialize value " + value, x))
   .get();
}

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

static final String objToStr(final Object value) {
  if (value instanceof CharSequence || Primitives.isWrapperType(value.getClass())) {
   return value.toString();
  }
  return PREFIX + Try.apply(() -> {
   ByteArrayOutputStream bytes = new ByteArrayOutputStream();
   ObjectOutputStream stream = new ObjectOutputStream(bytes);
   stream.writeObject(value);
   stream.flush();
   return base64().encode(bytes.toByteArray());
  }).get();
 }
}

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

@Override
public Locale locale(final BiFunction<List<LanguageRange>, List<Locale>, Locale> filter) {
 Supplier<Locale> def = () -> filter.apply(ImmutableList.of(), locales);
 // don't fail on bad Accept-Language header, just fallback to default locale.
 return lang.map(h -> Try.apply(() -> filter.apply(LocaleUtils.range(h), locales)).orElseGet(def))
   .orElseGet(def);
}

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

static Config dbconf(final String db, final Config conf) {
 Function<String, Config> ifconf = path -> {
  if (Try.apply(() -> conf.hasPath(path)).orElse(Boolean.FALSE)) {
   return conf.getConfig(path);
  }
  return ConfigFactory.empty();
 };
 // mongdo.db.* < mongo.*
 return ifconf.apply("mongo." + db)
   .withFallback(ifconf.apply("mongo"));
}

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

private File extra(final List<Artifact> artifacts, final String name) {
 return Try.apply(() -> {
  for (Artifact artifact : artifacts) {
   for (String tail : artifact.getDependencyTrail()) {
    if (tail.contains(name)) {
     return artifact.getFile();
    }
   }
  }
  throw new FileNotFoundException(name);
 }).get();
}

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

@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public Object exec(final List arguments) throws TemplateModelException {
 List<String> args = (List<String>) arguments.stream()
   .map(it -> Try.apply(() -> ((TemplateScalarModel) it).getAsString()).get())
   .collect(Collectors.toList());
 String[] xss = args.subList(1, args.size())
   .toArray(new String[arguments.size() - 1]);
 return env.xss(xss).apply(args.get(0));
}

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

private <T> Optional<T> get(final String name, final Function<String, T> provider) {
 return Try.apply(() -> Optional.of(register(v8, provider.apply(name))))
   .orElse(Optional.empty());
}

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

private static Field field(final Object bean) {
 return Try.apply(() -> CACHE.getUnchecked(bean.getClass()))
   .unwrap(UncheckedExecutionException.class)
   .get();
}

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

/**
 * Prepare and startup a {@link Jooby} application.
 *
 * @param app Application supplier.
 * @param args Application arguments.
 */
public static void run(final Class<? extends Jooby> app, final String... args) {
 run(() -> Try.apply(() -> app.newInstance()).get(), args);
}

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

private Library loadLibrary(final URI lib) {
 return Try.apply(() -> FileSystems.newFileSystem(lib, Maps.newHashMap()))
   .map(it -> Try.apply(() -> new Library(it)).get())
   .recover(x -> Try.apply(() -> new Library(Paths.get(lib))).get())
   .get();
}

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

private Map<String, Object> md(final Executable exec) {
 return Try.apply(() -> cache.getUnchecked(exec.getDeclaringClass()))
   .unwrap(UncheckedExecutionException.class)
   .get();
}

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

private Object value(final RequestParam param, final Request req, final Response rsp,
  final Route.Chain chain)
  throws Throwable {
 return Try.apply(() -> param.value(req, rsp, chain))
   .recover(x -> recoverMissing.apply(x).get())
   .get();
}

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

private CompletableFuture<Optional<Object>> send(final Event event) {
 List<MediaType> produces = event.type().<List<MediaType>>map(ImmutableList::of)
   .orElse(this.produces);
 SseRenderer ctx = new SseRenderer(renderers, produces, StandardCharsets.UTF_8, locale, locals);
 return Try.apply(() -> {
  byte[] bytes = ctx.format(event);
  return send(event.id(), bytes);
 }).recover(x -> {
  CompletableFuture<Optional<Object>> future = new CompletableFuture<>();
  future.completeExceptionally(x);
  return future;
 })
   .get();
}

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

@Override
public void handle(final Request req, final Response rsp, final Chain chain) throws Throwable {
 Object input = supplier.apply(req, rsp, chain);
 Object output = Try
   .apply(() -> mapper.map(input))
   .recover(ClassCastException.class, input)
   .get();
 rsp.send(output);
 chain.next(req, rsp);
}

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

@Override
public void handle(final Request req, final Response rsp, final Err ex) throws Throwable {
 log.error("execution of: {}{} resulted in exception\nRoute:\n{}\n\nStacktrace:",
   req.method(), req.path(), req.route().print(6), ex);
 Config conf = req.require(Config.class);
 boolean stackstrace = Try.apply(() -> conf.getBoolean("err.stacktrace"))
   .orElse(req.require(Env.class).name().equals("dev"));
 rsp.send(
   Results
     .when(MediaType.html, () -> Results.html(VIEW).put("err", ex.toMap(stackstrace)))
     .when(MediaType.all, () -> ex.toMap(stackstrace)));
}

相关文章