/** Returns an optional of the proper autoclose delay. */ private static Optional<Integer> autoCloseMs() { String value = System.getProperty(AUTOCLOSE_KEY); if (value == null) { return Optional.empty(); } else { return Errors.log().getWithDefault(() -> { int intValue = Integer.parseInt(value); Preconditions.checkArgument(intValue > 0, "%s should be positive or non-existent, this was %s", AUTOCLOSE_KEY, intValue); return Optional.of(intValue); }, Optional.<Integer> empty()); } }
/** An instance of {@code TreeDef.Parented} for {@link Path}. */ public static TreeDef.Parented<Path> forPath(Consumer<Throwable> errorPolicy) { Errors.Handling errors = Errors.createHandling(errorPolicy); return TreeDef.Parented.of( path -> errors.<List<Path>> getWithDefault(() -> { if (Files.isDirectory(path)) { return Files.list(path).collect(Collectors.toList()); } else { return Collections.emptyList(); } }, Collections.emptyList()), path -> errors.<Path> getWithDefault(() -> { return path.getParent(); }, null)); } }
/** An instance of {@code TreeDef.Parented} for {@link Path}. */ public static TreeDef.Parented<Path> forPath(Consumer<Throwable> errorPolicy) { Errors.Handling errors = Errors.createHandling(errorPolicy); return TreeDef.Parented.of( path -> errors.<List<Path>> getWithDefault(() -> { if (Files.isDirectory(path)) { return Files.list(path).collect(Collectors.toList()); } else { return Collections.emptyList(); } }, Collections.emptyList()), path -> errors.<Path> getWithDefault(() -> { return path.getParent(); }, null)); } }
Food handled = handling.getWithDefault(() -> cook("spaghetti"), CEREAL); // gotta have that default
/** An instance of {@code TreeDef.Parented} for {@link Path}. */ public static TreeDef.Parented<Path> forPath(Consumer<Throwable> errorPolicy) { Errors.Handling errors = Errors.createHandling(errorPolicy); return TreeDef.Parented.of( path -> errors.<List<Path>> getWithDefault(() -> { if (Files.isDirectory(path)) { return Files.list(path).collect(Collectors.toList()); } else { return Collections.emptyList(); } }, Collections.emptyList()), path -> errors.<Path> getWithDefault(() -> { return path.getParent(); }, null)); } }
/** An instance of {@code TreeDef.Parented} for {@link File}. */ public static TreeDef.Parented<File> forFile(Consumer<Throwable> errorPolicy) { Errors.Handling errors = Errors.createHandling(errorPolicy); return TreeDef.Parented.of( file -> errors.<List<File>> getWithDefault(() -> { if (file.isDirectory()) { return Arrays.asList(file.listFiles()); } else { return Collections.emptyList(); } }, Collections.emptyList()), file -> errors.<File> getWithDefault(() -> { return file.getParentFile(); }, null)); }
@Override public void finalize() { // If we're taking the "default value" route, Errors has you covered Food logged = Errors.log().getWithDefault(() -> cook("spaghetti"), CEREAL); // log to twitter Food suppressed = Errors.suppress().getWithDefault(() -> cook("spaghetti"), CEREAL); // suppress to insecurity buffer // If we're taking the "rethrow RuntimeException" route, then specifying a // default value would be nonsensical, so we don't do it Food rethrow = Errors.rethrow().get(() -> cook("spaghetti")); // I'm stressed, don't judge me Errors.suppress().run(() -> { eat(logged); eat(suppressed); eat(rethrow); }); }
/** An instance of {@code TreeDef.Parented} for {@link File}. */ public static TreeDef.Parented<File> forFile(Consumer<Throwable> errorPolicy) { Errors.Handling errors = Errors.createHandling(errorPolicy); return TreeDef.Parented.of( file -> errors.<List<File>> getWithDefault(() -> { if (file.isDirectory()) { return Arrays.asList(file.listFiles()); } else { return Collections.emptyList(); } }, Collections.emptyList()), file -> errors.<File> getWithDefault(() -> { return file.getParentFile(); }, null)); }
/** An instance of {@code TreeDef.Parented} for {@link File}. */ public static TreeDef.Parented<File> forFile(Consumer<Throwable> errorPolicy) { Errors.Handling errors = Errors.createHandling(errorPolicy); return TreeDef.Parented.of( file -> errors.<List<File>> getWithDefault(() -> { if (file.isDirectory()) { return Arrays.asList(file.listFiles()); } else { return Collections.emptyList(); } }, Collections.emptyList()), file -> errors.<File> getWithDefault(() -> { return file.getParentFile(); }, null)); }