/** * 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(); } } }
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()); }
Box.Nullable<String> boxValueNullable = Box.Nullable.ofVolatileNull(); Box.Nullable<String> boxValueNullableFast = Box.Nullable.ofNull(); Box.Nullable<String> boxFromMethodsNullable = Box.Nullable.from(boxValueNullable::get, boxValueNullable::set); expectToString.accept(boxFromMethodsNullable, "Box.Nullable.from[null]"); boxValueNullable.set("contain"); boxValueNullableFast.set("contain"); expectToString.accept(boxValueNullable, "Box.Nullable.ofVolatile[contain]"); expectToString.accept(boxValueNullableFast, "Box.Nullable.of[contain]");
/** 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()); }
/** Sets the given {@link Item} to have the image described by the given descriptor, maintaining proper reference counting. */ public static void set(Item widget, ImageDescriptor image) { globalSetter.forWidget(widget).set(image); }
/** Delegates to set(). */ default void accept(T value) { set(value); }
@Override public void set(R value) { delegate.set(converter.revert(value)); }
@Override public void set(@javax.annotation.Nullable R value) { delegate.set(converter.revert(value)); }
/** Creates a `Box.Nullable` holding null value in a non-`volatile` field. */ public static <T> Nullable<T> ofNull() { return of(null); }
/** Creates a `Box.Nullable` holding null value in a non-`volatile` field. */ public static <T> Nullable<T> ofNull() { return of(null); }
/** Creates a `Box.Nullable` holding a null value in a `volatile` field. */ public static <T> Nullable<T> ofVolatileNull() { return ofVolatile(null); }
/** Sets the given {@link Label} to have the image described by the given descriptor, maintaining proper reference counting. */ public static void set(Label widget, ImageDescriptor image) { globalSetter.forWidget(widget).set(image); }
/** Sets the given {@link Button} to have the image described by the given descriptor, maintaining proper reference counting. */ public static void set(Button widget, ImageDescriptor image) { globalSetter.forWidget(widget).set(image); }
/** Creates a `Box.Nullable` holding the null value in a `volatile` field. */ public static <T> Nullable<T> ofVolatileNull() { return ofVolatile(null); }
@Override public R get() { return converter.convert(delegate.get()); }
/** * Delegates to set(). * * @deprecated Provided to satisfy the {@code Function} interface; use {@link #set} instead. */ @Deprecated default void accept(@javax.annotation.Nullable T value) { set(value); }