/** Maps one {@code Box.Nullable} to another {@code Box.Nullable}. */ default <R> Box.Nullable<R> map(Function<? super T, ? extends R> getMapper, Function<? super R, ? extends T> setMapper) { return Box.Nullable.from(() -> getMapper.apply(get()), toSet -> set(setMapper.apply(toSet))); }
/** * Performs a blocking get in the UI thread. * * @param supplier will be executed in the UI thread. * @return the value which was returned by supplier. */ public <T> T get(Supplier<T> supplier) { if (Thread.currentThread() == swtThread) { return supplier.get(); } else { Nullable<T> holder = Nullable.ofVolatileNull(); display.syncExec(() -> holder.set(supplier.get())); return holder.get(); } } }
/** Shortcut for doing a set() on the result of a get(). */ default T modify(Function<? super T, ? extends T> mutator) { T modified = mutator.apply(get()); set(modified); return modified; }
/** Calls javaExec() in a way which is friendly with windows classpath limitations. */ public static ExecResult javaExec(Project project, Action<JavaExecSpec> spec) throws IOException { if (OS.getNative().isWindows()) { Box.Nullable<File> classpathJarBox = Box.Nullable.ofNull(); ExecResult execResult = project.javaexec(execSpec -> { // handle the user spec.execute(execSpec); // create a jar which embeds the classpath File classpathJar = toJarWithClasspath(execSpec.getClasspath()); classpathJar.deleteOnExit(); // set the classpath to be just that one jar execSpec.setClasspath(project.files(classpathJar)); // save the jar so it can be deleted later classpathJarBox.set(classpathJar); }); // delete the jar after the task has finished Errors.suppress().run(() -> FileMisc.forceDelete(classpathJarBox.get())); return execResult; } else { return project.javaexec(spec); } }
@Test public void testLeft() { Either<TimeUnit, String> left = Either.createLeft(TimeUnit.DAYS); Assert.assertTrue(left.isLeft()); Assert.assertFalse(left.isRight()); Assert.assertEquals(TimeUnit.DAYS, left.getLeft()); Assert.assertEquals(Optional.of(TimeUnit.DAYS), left.asOptionalLeft()); Assert.assertEquals(Optional.empty(), left.asOptionalRight()); try { left.getRight(); Assert.fail(); } catch (Unhandled e) {} Assert.assertEquals("DAYS", left.fold(TimeUnit::toString, Function.identity())); Box.Nullable<TimeUnit> leftSide = Box.Nullable.ofNull(); Box.Nullable<String> rightSide = Box.Nullable.ofNull(); left.acceptBoth(leftSide, rightSide, TimeUnit.HOURS, "wahoo"); Assert.assertEquals(TimeUnit.DAYS, leftSide.get()); Assert.assertEquals("wahoo", rightSide.get()); }
private void runWithCleanStack(Throwing.Runnable runnable) throws Throwable { Box.Nullable<Throwable> testError = Box.Nullable.ofNull(); Thread thread = new Thread() { @Override public void run() { try { runnable.run(); } catch (Throwable e) { testError.set(e); } } }; thread.start(); thread.join(); if (testError.get() != null) { throw testError.get(); } }
@Test public void testRight() { Either<TimeUnit, String> right = Either.createRight("word"); Assert.assertTrue(right.isRight()); Assert.assertFalse(right.isLeft()); Assert.assertEquals("word", right.getRight()); Assert.assertEquals(Optional.of("word"), right.asOptionalRight()); Assert.assertEquals(Optional.empty(), right.asOptionalLeft()); try { right.getLeft(); Assert.fail(); } catch (Unhandled e) {} Assert.assertEquals("word", right.fold(TimeUnit::toString, Function.identity())); Box.Nullable<TimeUnit> leftSide = Box.Nullable.ofNull(); Box.Nullable<String> rightSide = Box.Nullable.ofNull(); right.acceptBoth(leftSide, rightSide, TimeUnit.HOURS, "wahoo"); Assert.assertEquals(TimeUnit.HOURS, leftSide.get()); Assert.assertEquals("word", rightSide.get()); }
private Running() throws Exception { Box.Nullable<BundleContext> context = Box.Nullable.ofNull(); Main main = new Main() { @SuppressFBWarnings(value = "DMI_THREAD_PASSED_WHERE_RUNNABLE_EXPECTED", justification = "splashHandler is a thread rather than a runnable. Almost definitely a small bug, " + "but there's a lot of small bugs in the copy-pasted launcher code. It's battle-tested, FWIW.") @Override protected void invokeFramework(String[] passThruArgs, URL[] bootPath) throws Exception { context.set(EclipseStarter.startup(passThruArgs, splashHandler)); } }; main.basicRun(eclipseIni.getLinesAsArray()); this.bundleContext = Objects.requireNonNull(context.get()); }
/** Runs the display loop until the given future has returned. */ public static <T> T loopUntilGet(CompletionStage<T> future) { Box.Nullable<T> result = Box.Nullable.ofNull(); Box.Nullable<Throwable> error = Box.Nullable.ofNull(); Rx.subscribe(future, Rx.onValueOnFailure(result::set, error::set)); CompletableFuture<?> actualFuture = future.toCompletableFuture(); loopUntil(display -> actualFuture.isDone()); if (error.get() != null) { throw Errors.asRuntime(error.get()); } else { return result.get(); } }
private static Map<String, String> parseFromFile(File artifactsJar) throws IOException { Box.Nullable<Map<String, String>> value = Box.Nullable.ofNull(); ZipMisc.read(artifactsJar, "artifacts.xml", input -> { value.set(Errors.rethrow().get(() -> parse(input))); }); return Objects.requireNonNull(value.get()); } }
@Override public R get() { return converter.convert(delegate.get()); }
/** Shortcut for doing a set() on the result of a get(). */ default T modify(Function<? super T, ? extends T> mutator) { T modified = mutator.apply(get()); set(modified); return modified; }
@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(); }
/** Shortcut for doing a set() on the result of a get(). */ default T modify(Function<? super T, ? extends T> mutator) { T modified = mutator.apply(get()); set(modified); return modified; }
@Override @javax.annotation.Nullable public R get() { return converter.convert(delegate.get()); }
/** Shortcut for doing a set() on the result of a get(). */ @Override public R modify(Function<? super R, ? extends R> mutator) { Box.Nullable<R> result = Box.Nullable.of(null); delegate.modify(input -> { R unmappedResult = mutator.apply(converter.convert(input)); result.set(unmappedResult); return converter.revert(unmappedResult); }); return result.get(); }
/** Shortcut for doing a set() on the result of a get(). */ @Override public R modify(Function<? super R, ? extends R> 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(); }
/** Shortcut for doing a set() on the result of a get(). */ @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.convert(input)); result.set(unmappedResult); return converter.revert(unmappedResult); }); return result.get(); }