本文整理汇总了Java中com.diffplug.common.base.Box类的典型用法代码示例。如果您正苦于以下问题:Java Box类的具体用法?Java Box怎么用?Java Box使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Box类属于com.diffplug.common.base包,在下文中一共展示了Box类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: runWithCleanStack
import com.diffplug.common.base.Box; //导入依赖的package包/类
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();
}
}
开发者ID:diffplug,项目名称:durian-debug,代码行数:19,代码来源:StackDumperTest.java
示例2: javaExec
import com.diffplug.common.base.Box; //导入依赖的package包/类
/** 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);
}
}
开发者ID:diffplug,项目名称:goomph,代码行数:23,代码来源:JavaExecWinFriendly.java
示例3: fromVolatile
import com.diffplug.common.base.Box; //导入依赖的package包/类
/**
* Creates an `RxGetter` from the given `Observable` and `initialValue`,
* appropriate for observables which emit values on multiple threads.
*
* The value returned by {@link RxGetter#get()} will be the last value emitted by
* the observable, as recorded by a volatile field.
*/
public static <T> RxGetter<T> fromVolatile(Observable<T> observable, T initialValue) {
Box<T> box = Box.ofVolatile(initialValue);
Rx.subscribe(observable, box::set);
return new RxGetter<T>() {
@Override
public Observable<T> asObservable() {
return observable;
}
@Override
public T get() {
return box.get();
}
};
}
开发者ID:diffplug,项目名称:durian-rx,代码行数:23,代码来源:RxGetter.java
示例4: from
import com.diffplug.common.base.Box; //导入依赖的package包/类
/**
* Creates an `RxGetter` from the given `Observable` and `initialValue`,
* appropriate for observables which emit values on a single thread.
*
* The value returned by {@link RxGetter#get()} will be the last value emitted by
* the observable, as recorded by a non-volatile field.
*/
public static <T> RxGetter<T> from(Observable<T> observable, T initialValue) {
Box<T> box = Box.of(initialValue);
Rx.subscribe(observable, box::set);
return new RxGetter<T>() {
@Override
public Observable<T> asObservable() {
return observable;
}
@Override
public T get() {
return box.get();
}
};
}
开发者ID:diffplug,项目名称:durian-rx,代码行数:23,代码来源:RxGetter.java
示例5: testLockingBehavior
import com.diffplug.common.base.Box; //导入依赖的package包/类
static void testLockingBehavior(String message, Function<String, LockBox<String>> constructor, Consumer<LockBox<String>> timed, String expectedResult) {
LockBox<String> box = constructor.apply("1");
Box.Nullable<Double> elapsed = Box.Nullable.of(null);
ThreadHarness harness = new ThreadHarness();
harness.add(() -> {
box.modify(val -> {
Errors.rethrow().run(() -> Thread.sleep(100));
return val + "2";
});
});
harness.add(() -> {
LapTimer timer = LapTimer.createMs();
timed.accept(box);
elapsed.set(timer.lap());
});
harness.run();
// make sure that the action which should have been delayed, was delayed for the proper time
Assert.assertEquals("Wrong delay time for " + message, 0.1, elapsed.get().doubleValue(), 0.05);
// make sure that the final result was what was expected
Assert.assertEquals("Wrong result for " + message, expectedResult, box.get());
}
开发者ID:diffplug,项目名称:durian-rx,代码行数:22,代码来源:LockBoxTest.java
示例6: testDisposableEar
import com.diffplug.common.base.Box; //导入依赖的package包/类
@Test
public void testDisposableEar() {
InteractiveTest.testCoat("Non-interactive, will pass itself", cmp -> {
Shell underTest = new Shell(cmp.getShell(), SWT.NONE);
DisposableEar ear = SwtRx.disposableEar(underTest);
Assert.assertFalse(ear.isDisposed());
Box<Boolean> hasBeenDisposed = Box.of(false);
ear.runWhenDisposed(() -> hasBeenDisposed.set(true));
Assert.assertFalse(hasBeenDisposed.get());
underTest.dispose();
Assert.assertTrue(hasBeenDisposed.get());
Assert.assertTrue(ear.isDisposed());
Box<Boolean> alreadyDisposed = Box.of(false);
ear.runWhenDisposed(() -> alreadyDisposed.set(true));
Assert.assertTrue(alreadyDisposed.get());
InteractiveTest.closeAndPass(cmp);
});
}
开发者ID:diffplug,项目名称:durian-swt,代码行数:23,代码来源:SwtRxTest.java
示例7: scheduleCancelWorks
import com.diffplug.common.base.Box; //导入依赖的package包/类
@Test
public void scheduleCancelWorks() throws InterruptedException, ExecutionException {
testOffUiThread(() -> {
Box<Boolean> hasRun = Box.of(false);
ScheduledFuture<?> future = SwtExec.async().schedule(() -> {
hasRun.set(true);
}, 100, TimeUnit.MILLISECONDS);
Thread.sleep(10);
future.cancel(true);
Thread.sleep(200);
try {
future.get();
Assert.fail();
} catch (CancellationException e) {
// we got the cancellation we expected
}
Assert.assertEquals(true, future.isCancelled());
Assert.assertEquals(true, future.isDone());
Assert.assertEquals(false, hasRun.get());
});
}
开发者ID:diffplug,项目名称:durian-swt,代码行数:22,代码来源:SwtExecSchedulingTest.java
示例8: scheduleFixedRate
import com.diffplug.common.base.Box; //导入依赖的package包/类
@Test
public void scheduleFixedRate() throws InterruptedException, ExecutionException {
testOffUiThread(() -> {
Box.Int count = Box.Int.of(0);
ScheduledFuture<?> future = SwtExec.async().scheduleAtFixedRate(() -> {
count.set(count.getAsInt() + 1);
// block UI thread for 400ms - very bad
Errors.rethrow().run(() -> Thread.sleep(400));
}, 500, 500, TimeUnit.MILLISECONDS);
Thread.sleep(2250);
// increment every 500ms for 2250ms, it should tick 4 times
Assertions.assertThat(count.getAsInt()).isEqualTo(4);
future.cancel(true);
Thread.sleep(1000);
Assertions.assertThat(count.getAsInt()).isEqualTo(4);
});
}
开发者ID:diffplug,项目名称:durian-swt,代码行数:18,代码来源:SwtExecSchedulingTest.java
示例9: scheduleFixedDelay
import com.diffplug.common.base.Box; //导入依赖的package包/类
@Test
public void scheduleFixedDelay() throws InterruptedException, ExecutionException {
testOffUiThread(() -> {
Box.Int count = Box.Int.of(0);
ScheduledFuture<?> future = SwtExec.async().scheduleWithFixedDelay(() -> {
// block UI thread for 400ms - very bad
Errors.rethrow().run(() -> Thread.sleep(400));
count.set(count.getAsInt() + 1);
}, 500, 500, TimeUnit.MILLISECONDS);
Thread.sleep(2250);
// increment every 500ms and burn 400ms for 2250ms, it should tick twice
Assertions.assertThat(count.getAsInt()).isEqualTo(2);
future.cancel(true);
Thread.sleep(1000);
Assertions.assertThat(count.getAsInt()).isEqualTo(2);
});
}
开发者ID:diffplug,项目名称:durian-swt,代码行数:18,代码来源:SwtExecSchedulingTest.java
示例10: Running
import com.diffplug.common.base.Box; //导入依赖的package包/类
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());
}
开发者ID:diffplug,项目名称:goomph,代码行数:14,代码来源:EclipseIniLauncher.java
示例11: testEquals
import com.diffplug.common.base.Box; //导入依赖的package包/类
public void testEquals() {
List<List<Object>> allGroups = new ArrayList<>();
Box<List<Object>> currentGroup = Box.of(new ArrayList<>());
API api = new API() {
@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<>();
});
}
};
try {
setupTest(api);
} catch (Exception e) {
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()'.");
}
EqualsTester tester = new EqualsTester();
for (List<Object> step : allGroups) {
tester.addEqualityGroup(step.toArray());
}
tester.testEquals();
}
开发者ID:diffplug,项目名称:spotless,代码行数:36,代码来源:SerializableEqualityTester.java
示例12: modify
import com.diffplug.common.base.Box; //导入依赖的package包/类
/** 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.ofNull();
delegate.modify(input -> {
R unmappedResult = mutator.apply(converter.convertNonNull(input));
result.set(unmappedResult);
return converter.revertNonNull(unmappedResult);
});
return result.get();
}
开发者ID:diffplug,项目名称:durian-rx,代码行数:12,代码来源:MappedImp.java
示例13: settable
import com.diffplug.common.base.Box; //导入依赖的package包/类
@Test
public void settable() {
DisposableEar.Settable ear = DisposableEar.settable();
Assert.assertFalse(ear.isDisposed());
Box<Boolean> hasBeenDisposed = Box.of(false);
ear.runWhenDisposed(() -> hasBeenDisposed.set(true));
Assert.assertFalse(hasBeenDisposed.get());
ear.dispose();
Assert.assertTrue(hasBeenDisposed.get());
Assert.assertTrue(ear.isDisposed());
assertDisposedBehavior(ear);
}
开发者ID:diffplug,项目名称:durian-rx,代码行数:16,代码来源:DisposableEarTest.java
示例14: setResult
import com.diffplug.common.base.Box; //导入依赖的package包/类
private static void setResult(Control ctl, Optional<Throwable> result) {
SwtExec.async().guardOn(ctl).execute(() -> {
Shell shell = ctl.getShell();
Box<Optional<Throwable>> resultBox = shellToResult.remove(shell);
Objects.requireNonNull(resultBox, "No test shell for control.");
resultBox.set(result);
shell.dispose();
});
}
开发者ID:diffplug,项目名称:durian-swt,代码行数:10,代码来源:InteractiveTest.java
示例15: openInstructions
import com.diffplug.common.base.Box; //导入依赖的package包/类
/** Opens the instructions dialog. */
private static Shell openInstructions(Shell underTest, String instructions, Box<Optional<Throwable>> result) {
Shell instructionsShell = Shells.builder(SWT.TITLE | SWT.BORDER, cmp -> {
Layouts.setGrid(cmp).numColumns(3);
// show the instructions
Text text = new Text(cmp, SWT.WRAP);
Layouts.setGridData(text).horizontalSpan(3).grabAll();
text.setEditable(false);
text.setText(instructions);
// pass / fail buttons
Layouts.newGridPlaceholder(cmp).grabHorizontal();
Consumer<Boolean> buttonCreator = isPass -> {
Button btn = new Button(cmp, SWT.PUSH);
btn.setText(isPass ? "PASS" : "FAIL");
btn.addListener(SWT.Selection, e -> {
result.set(isPass ? Optional.empty() : Optional.of(new FailedByUser(instructions)));
cmp.getShell().dispose();
});
Layouts.setGridData(btn).widthHint(SwtMisc.defaultButtonWidth());
};
buttonCreator.accept(true);
buttonCreator.accept(false);
})
.setTitle("PASS / FAIL")
.setSize(SwtMisc.scaleByFontHeight(18, 0))
.openOn(underTest);
// put the instructions to the right of the dialog under test
Rectangle instructionsBounds = instructionsShell.getBounds();
Rectangle underTestBounds = underTest.getBounds();
instructionsBounds.x = underTestBounds.x + underTestBounds.width + HORIZONTAL_SEP;
instructionsBounds.y = underTestBounds.y;
instructionsShell.setBounds(instructionsBounds);
// return the value
return instructionsShell;
}
开发者ID:diffplug,项目名称:durian-swt,代码行数:41,代码来源:InteractiveTest.java
示例16: loopUntilGet
import com.diffplug.common.base.Box; //导入依赖的package包/类
/** Runs the display loop until the given future has returned. */
public static <T> T loopUntilGet(ListenableFuture<T> future) throws Throwable {
Box.Nullable<T> result = Box.Nullable.ofNull();
Box.Nullable<Throwable> error = Box.Nullable.ofNull();
Rx.subscribe(future, Rx.onValueOnFailure(result::set, error::set));
loopUntil(display -> future.isDone());
if (error.get() != null) {
throw error.get();
} else {
return result.get();
}
}
开发者ID:diffplug,项目名称:durian-swt,代码行数:15,代码来源:SwtMisc.java
示例17: testCopy
import com.diffplug.common.base.Box; //导入依赖的package包/类
@Test
public void testCopy() {
// we'll set this variable to show that it's running as expected
Box.Nullable<String> toSet = Box.Nullable.ofNull();
// create an action
IAction action = Actions.builder()
.setText("Name")
.setTooltip("Tooltip")
.setAccelerator(SWT.SHIFT | 'a')
.setRunnable(() -> toSet.set("WasRun")).build();
// make sure it's doing what we expect
Assert.assertEquals("Name", action.getText());
Assert.assertEquals("Tooltip [Shift A]", action.getToolTipText());
Assert.assertEquals(SWT.SHIFT | 'a', action.getAccelerator());
Assert.assertEquals(null, toSet.get());
action.run();
Assert.assertEquals("WasRun", toSet.get());
// copy that action
IAction copy = Actions.builderCopy(action).setAccelerator(SWT.NONE)
.setRunnable(() -> toSet.set("CopyWasRun")).build();
Assert.assertEquals(SWT.NONE, copy.getAccelerator());
// test that the tooltip was stripped correctly in the copy
Assert.assertEquals("Tooltip", copy.getToolTipText());
// make sure that the runnable took
copy.run();
Assert.assertEquals("CopyWasRun", toSet.get());
// but didn't screw up the other one
action.run();
Assert.assertEquals("WasRun", toSet.get());
}
开发者ID:diffplug,项目名称:durian-swt,代码行数:35,代码来源:ActionsTest.java
示例18: schedulePerformance
import com.diffplug.common.base.Box; //导入依赖的package包/类
@Test
public void schedulePerformance() throws InterruptedException, ExecutionException {
testOffUiThread(() -> {
Box.Int delay = Box.Int.of(-1);
long start = System.currentTimeMillis();
ScheduledFuture<?> future = SwtExec.async().schedule(() -> {
delay.set(Ints.saturatedCast(System.currentTimeMillis() - start));
}, 1000, TimeUnit.MILLISECONDS);
// block until finish
future.get();
Assertions.assertThat(delay.getAsInt()).isBetween(750, 1250);
});
}
开发者ID:diffplug,项目名称:durian-swt,代码行数:14,代码来源:SwtExecSchedulingTest.java
示例19: assertDisposedBehavior
import com.diffplug.common.base.Box; //导入依赖的package包/类
private void assertDisposedBehavior(DisposableEar ear) {
Assert.assertTrue(ear.isDisposed());
Box<Boolean> hasBeenDisposed = Box.of(false);
ear.runWhenDisposed(() -> hasBeenDisposed.set(true));
Assert.assertTrue(hasBeenDisposed.get());
}
开发者ID:diffplug,项目名称:durian-rx,代码行数:7,代码来源:DisposableEarTest.java
示例20: createSetter
import com.diffplug.common.base.Box; //导入依赖的package包/类
/**
* {@link ImageDescriptor} allows an {@link Image} to be shared in a pool using reference counting. In order to not screw-up the reference
* counting, you need to be pretty careful with how you use them.
* <p>
* This creates a {@link com.diffplug.common.base.Box.Nullable Box.Nullable<ImageDescriptor>} which sets and gets images in a way that will keep the reference counting happy.
* <p>
* <b>NO ONE MUST SET THE IMAGE EXCEPT THIS SETTER.</b>
*
* @param lifecycle Any outstanding images will be destroyed with the lifecycle of this Widget.
* @param imageGetter Function which returns the image currently on the Widget (used to ensure that nobody messed with it).
* @param imageSetter Function which sets the image on the Widget.
* @return A `Box.Nullable<ImageDescriptor>` for setting the {@link Image} using an {@link ImageDescriptor}.
*/
public static Box.Nullable<ImageDescriptor> createSetter(Widget lifecycle, Supplier<Image> imageGetter, Consumer<Image> imageSetter) {
return new Box.Nullable<ImageDescriptor>() {
private ImageDescriptor lastDesc;
private Image lastImg;
{
// when the control is disposed, we'll clear the image
lifecycle.addListener(SWT.Dispose, e -> {
if (lastDesc != null) {
lastDesc.destroyResource(lastImg);
}
});
}
@Override
public ImageDescriptor get() {
return lastDesc;
}
@Override
public void set(ImageDescriptor newDesc) {
// make sure nobody else messed with the image
if (!Objects.equals(imageGetter.get(), lastImg)) {
// if someone else did mess with it, we can probably survive, so best to just
// log the failure and continue with setting the image
Errors.log().accept(new IllegalStateException("Setter must have exclusive control over the image field."));
}
// set the new image
Image newImg;
if (newDesc != null) {
newImg = (Image) newDesc.createResource(lifecycle.getDisplay());
} else {
newImg = null;
}
imageSetter.accept(newImg);
// if an image was already set, destroy it
if (lastDesc != null) {
lastDesc.destroyResource(lastImg);
}
// save the fields for the next go-round
lastDesc = newDesc;
lastImg = newImg;
}
};
}
开发者ID:diffplug,项目名称:durian-swt,代码行数:62,代码来源:ImageDescriptors.java
注:本文中的com.diffplug.common.base.Box类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论