Box<Boolean> wasChanged = Box.of(false); ImmutableSet<File> result = cached.computeIfAbsent(ImmutableSet.copyOf(mavenCoords), coords -> { wasChanged.set(true); return ImmutableSet.copyOf(input.get().provisionWithTransitives(withTransitives, coords)); }); if (wasChanged.get()) { try (ObjectOutputStream outputStream = new ObjectOutputStream(Files.asByteSink(cacheFile).openBufferedStream())) { outputStream.writeObject(cached);
@Override public void areDifferentThan() { currentGroup.modify(current -> { // create two instances, and add them to the group current.add(create()); current.add(create()); // create two instances using a serialization roundtrip, and add them to the group current.add(reserialize(create())); current.add(reserialize(create())); // add this group to the list of all groups allGroups.add(current); // and return a new blank group for the next call return new ArrayList<>(); }); } };
public void testEquals() { List<List<Object>> allGroups = new ArrayList<>(); Box<List<Object>> currentGroup = Box.of(new ArrayList<>()); API api = new API() { @Override throw new AssertionError("Error during setupTest", e); List<Object> lastGroup = currentGroup.get(); if (!lastGroup.isEmpty()) { throw new IllegalArgumentException("Looks like you forgot to make a final call to 'areDifferentThan()'.");
/** Delegates to set(). */ default void accept(T value) { set(value); }
Box<Optional<Throwable>> result = Box.ofVolatile(Optional.of(new FailedByUser(instructions))); result.set(Optional.empty()); }); }); if (result.get().isPresent()) { Throwable e = result.get().get(); if (e instanceof Error) { throw (Error) e;
@Override public R get() { return converter.convertNonNull(delegate.get()); }
Box<String> boxValue = Box.ofVolatile("contain"); Box<String> boxValueFast = Box.of("contain"); Box<String> boxFromMethods = Box.from(boxValue::get, boxValue::set);
/** * Delegates to {@link #set}. * * @deprecated Provided to satisfy the {@link Consumer} interface; use {@link #set} instead. */ @Deprecated default void accept(T value) { set(value); }
@Override public R get() { return converter.convertNonNull(delegate.get()); }
/** * Given a consumer of lines, creates a stateful consumer of strings * which will combine its input until it finds a newline, and * split its input when it contains multiple newlines. Examples * make this more clear: * <pre> * "some", "\n", "simple ", "lines", "\n" -> "some", "simple lines" * "some\nsimple lines\n" -> "some", "simple lines" * "no newline\nno output" -> "no newline" * </pre> * @param perLine a Consumer<String> which will receive strings which were terminated by newlines (but aren't anymore). * @return a Consumer<String> which accepts any strings, and will feed them to perLine. */ public static Consumer<String> stringsToLines(Consumer<String> perLine) { Box<String> leftover = Box.of(""); return rawString -> { rawString = leftover.get() + rawString.replace("\r", ""); int lastIdx = 0; int idx = 0; while ((idx = rawString.indexOf('\n', lastIdx)) > -1) { perLine.accept(rawString.substring(lastIdx, idx)); lastIdx = idx + 1; } leftover.set(rawString.substring(lastIdx)); }; }
/** * Delegates to set(). * * @deprecated Provided to satisfy the {@link Consumer} interface; use {@link #set} instead. */ @Deprecated default void accept(T value) { set(value); }
@Override public R modify(Function<? super R, ? extends R> mutator) { Objects.requireNonNull(mutator); Box.Nullable<R> result = Box.Nullable.of(null); delegate.modify(input -> { R unmappedResult = mutator.apply(converter.convertNonNull(input)); result.set(unmappedResult); return converter.revertNonNull(unmappedResult); }); return result.get(); }
/** * Given a consumer of lines, creates a stateful consumer of strings * which will combine its input until it finds a newline, and * split its input when it contains multiple newlines. Examples * make this more clear: * <pre> * "some", "\n", "simple ", "lines", "\n" -> "some", "simple lines" * "some\nsimple lines\n" -> "some", "simple lines" * "no newline\nno output" -> "no newline" * </pre> * @param perLine a Consumer<String> which will receive strings which were terminated by newlines (but aren't anymore). * @return a Consumer<String> which accepts any strings, and will feed them to perLine. */ public static Consumer<String> stringsToLines(Consumer<String> perLine) { Box<String> leftover = Box.of(""); return rawString -> { rawString = leftover.get() + rawString.replace("\r", ""); int lastIdx = 0; int idx = 0; while ((idx = rawString.indexOf('\n', lastIdx)) > -1) { perLine.accept(rawString.substring(lastIdx, idx)); lastIdx = idx + 1; } leftover.set(rawString.substring(lastIdx)); }; }
public <T> CompletionStage<T> filter(CompletionStage<T> obj) { box.set(obj.toCompletableFuture()); return obj; } }