private void taskRanAgainst(String task, String... ranAgainst) throws IOException { pauseForFilesystem(); String console = StringPrinter.buildString(Errors.rethrow().wrap(printer -> { boolean expectFailure = task.equals("spotlessCheck") && !isClean(); if (expectFailure) { gradleRunner().withArguments(task).forwardStdOutput(printer.toWriter()).buildAndFail(); } else { gradleRunner().withArguments(task).forwardStdOutput(printer.toWriter()).build(); } })); SortedSet<String> added = new TreeSet<>(); for (String line : console.split("\n")) { String trimmed = line.trim(); if (trimmed.startsWith("<") && trimmed.endsWith(">")) { added.add(trimmed.substring(1, trimmed.length() - 1)); } } Assert.assertEquals(concat(Arrays.asList(ranAgainst)), concat(added)); }
/** Attempts to call {@code supplier} and rethrows any exceptions as unchecked exceptions. */ public <T> T get(Throwing.Supplier<T> supplier) { return wrap(supplier).get(); }
/** Attempts to call {@code supplier} and rethrows any exceptions as unchecked exceptions. */ public <T> T get(Throwing.Supplier<T> supplier) { return wrap(supplier).get(); }
foodOnPlate.forEach(Errors.rethrow().wrap(this::eat));
.forEach(Errors.rethrow().wrap(file -> { ZipMisc.read(file, MANIFEST_PATH, input -> addManifest(supported, new Manifest(input), file)); })); files.stream().filter(file -> file.isDirectory()).forEach(Errors.rethrow().wrap(folder -> { File manifestFile = new File(folder, MANIFEST_PATH); if (manifestFile.exists()) {
@SuppressWarnings("unused") public void wrapping() { // If your functional interface has 0 or 1 outputs, and 0 or 1 inputs, then Errors can wrap it into its standard Java 8 form Throwing.Runnable marathon = () -> { throw new IAmOnFire(); }; java.lang.Runnable marathonSafe = Errors.log().wrap(marathon); Throwing.Consumer<Food> eat = this::eat; java.util.function.Consumer<Food> eatSafe = Errors.log().wrap(eat); Throwing.Supplier<Food> cookSpatula = () -> cook("spatula"); java.util.function.Supplier<Food> cookSpatulaOrGetCereal = Errors.log().wrapWithDefault(cookSpatula, CEREAL); java.util.function.Supplier<Food> cookSpatulaOrBurn = Errors.rethrow().wrap(cookSpatula); Throwing.Function<String, Food> cookAnything = this::cook; java.util.function.Function<String, Food> cookAnythingOrGetCereal = Errors.log().wrapWithDefault(this::cook, CEREAL); java.util.function.Function<String, Food> cookAnythingOrBurn = Errors.rethrow().wrap(this::cook); // If your function has more than 1 input, you can either // A) Make a wrapper function that calls Errors.get() to return a value (recommended) // B) Make a "wrapper wrapper" (see https://github.com/diffplug/durian/blob/master/test/com/diffplug/common/base/ErrorsMultipleInputs.png) // If your function has more than 1 output, see https://github.com/diffplug/durian/blob/master/test/com/diffplug/common/base/ErrorsMultipleOutputs.png }
/** * Reads the given entry from the zip. * * @param input a zip file * @param toRead a path within that zip file * @return the given path within the zip file decoded as a UTF8 string, with only unix newlines. */ public static String read(File input, String toRead) throws IOException { String raw = StringPrinter.buildString(Errors.rethrow().wrap(printer -> { read(input, toRead, inputStream -> { copy(inputStream, printer.toOutputStream(StandardCharsets.UTF_8)); }); })); return FileMisc.toUnixNewline(raw); }
/** Attempts to call {@code supplier} and rethrows any exceptions as unchecked exceptions. */ public <T> T get(Throwing.Supplier<T> supplier) { return wrap(supplier).get(); }