本文整理汇总了Java中org.reactfx.value.Var.newSimpleVar方法的典型用法代码示例。如果您正苦于以下问题:Java Var.newSimpleVar方法的具体用法?Java Var.newSimpleVar怎么用?Java Var.newSimpleVar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.reactfx.value.Var
的用法示例。
在下文中一共展示了Var.newSimpleVar方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testUndoInvertsTheChange
import org.reactfx.value.Var; //导入方法依赖的package包/类
@Test
public void testUndoInvertsTheChange() {
EventSource<Integer> changes = new EventSource<>();
Var<Integer> lastAction = Var.newSimpleVar(null);
UndoManager<?> um = UndoManagerFactory.unlimitedHistoryUndoManager(
changes, i -> -i, i -> { lastAction.setValue(i); changes.push(i); });
changes.push(3);
changes.push(7);
assertNull(lastAction.getValue());
um.undo();
assertEquals(-7, lastAction.getValue().intValue());
um.undo();
assertEquals(-3, lastAction.getValue().intValue());
um.redo();
assertEquals(3, lastAction.getValue().intValue());
um.redo();
assertEquals(7, lastAction.getValue().intValue());
}
示例2: ParagraphBox
import org.reactfx.value.Var; //导入方法依赖的package包/类
ParagraphBox(Paragraph<PS, SEG, S> par, BiConsumer<TextFlow, PS> applyParagraphStyle,
Function<StyledSegment<SEG, S>, Node> nodeFactory) {
this.getStyleClass().add("paragraph-box");
this.text = new ParagraphText<>(par, nodeFactory);
applyParagraphStyle.accept(this.text, par.getParagraphStyle());
this.index = Var.newSimpleVar(0);
getChildren().add(text);
graphic = Val.combine(
graphicFactory,
this.index,
(f, i) -> f != null ? f.apply(i) : null);
graphic.addListener((obs, oldG, newG) -> {
if(oldG != null) {
getChildren().remove(oldG);
}
if(newG != null) {
getChildren().add(newG);
}
});
graphicOffset.addListener(obs -> requestLayout());
}
示例3: testDynamicMap
import org.reactfx.value.Var; //导入方法依赖的package包/类
@Test
public void testDynamicMap() {
LiveList<String> strings = new LiveArrayList<>("1", "22", "333");
Var<Function<String, Integer>> fn = Var.newSimpleVar(String::length);
SuspendableList<Integer> ints = strings.mapDynamic(fn).suspendable();
assertEquals(2, ints.get(1).intValue());
ints.observeChanges(ch -> {
for(ListModification<?> mod: ch) {
assertEquals(Arrays.asList(1, 2, 3), mod.getRemoved());
assertEquals(Arrays.asList(1, 16, 9), mod.getAddedSubList());
}
});
ints.suspendWhile(() -> {
strings.set(1, "4444");
fn.setValue(s -> s.length() * s.length());
});
}
示例4: testNullToValChange
import org.reactfx.value.Var; //导入方法依赖的package包/类
@Test
public void testNullToValChange() {
Var<String> src = Var.newSimpleVar(null);
LiveList<String> list = src.asList();
assertEquals(0, list.size());
List<ListModification<? extends String>> mods = new ArrayList<>();
list.observeModifications(mods::add);
src.setValue("foo");
assertEquals(1, mods.size());
ListModification<? extends String> mod = mods.get(0);
assertEquals(0, mod.getRemovedSize());
assertEquals(Collections.singletonList("foo"), mod.getAddedSubList());
assertEquals(1, list.size());
}
示例5: testValToNullChange
import org.reactfx.value.Var; //导入方法依赖的package包/类
@Test
public void testValToNullChange() {
Var<String> src = Var.newSimpleVar("foo");
LiveList<String> list = src.asList();
assertEquals(1, list.size());
List<ListModification<? extends String>> mods = new ArrayList<>();
list.observeModifications(mods::add);
src.setValue(null);
assertEquals(1, mods.size());
ListModification<? extends String> mod = mods.get(0);
assertEquals(Collections.singletonList("foo"), mod.getRemoved());
assertEquals(0, mod.getAddedSize());
assertEquals(0, list.size());
}
示例6: testValToValChange
import org.reactfx.value.Var; //导入方法依赖的package包/类
@Test
public void testValToValChange() {
Var<String> src = Var.newSimpleVar("foo");
LiveList<String> list = src.asList();
assertEquals(1, list.size());
List<ListModification<? extends String>> mods = new ArrayList<>();
list.observeModifications(mods::add);
src.setValue("bar");
assertEquals(1, mods.size());
ListModification<? extends String> mod = mods.get(0);
assertEquals(Collections.singletonList("foo"), mod.getRemoved());
assertEquals(Collections.singletonList("bar"), mod.getAddedSubList());
assertEquals(1, list.size());
}
示例7: test
import org.reactfx.value.Var; //导入方法依赖的package包/类
@Test
public void test() {
LiveList<Integer> list = new LiveArrayList<>(1, 2, 4);
Var<IndexRange> range = Var.newSimpleVar(new IndexRange(0, 0));
Val<Integer> rangeSum = list.reduceRange(range, (a, b) -> a + b);
assertNull(rangeSum.getValue());
List<Integer> observed = new ArrayList<>();
rangeSum.values().subscribe(sum -> {
observed.add(sum);
if(sum == null) {
range.setValue(new IndexRange(0, 2));
} else if(sum == 3) {
list.addAll(1, Arrays.asList(8, 16));
} else if(sum == 9) {
range.setValue(new IndexRange(2, 4));
}
});
assertEquals(Arrays.asList(null, 3, 9, 18), observed);
}
示例8: testWhenBound
import org.reactfx.value.Var; //导入方法依赖的package包/类
@Test
public void testWhenBound() {
ObservableList<Integer> list = FXCollections.observableArrayList(1, 1, 1, 1, 1);
Val<Integer> sum = LiveList.reduce(list, (a, b) -> a + b);
Var<Integer> lastObserved = Var.newSimpleVar(sum.getValue());
assertEquals(5, lastObserved.getValue().intValue());
sum.addListener((obs, oldVal, newVal) -> {
assertEquals(lastObserved.getValue(), oldVal);
lastObserved.setValue(newVal);
});
list.addAll(2, Arrays.asList(2, 2));
assertEquals(9, lastObserved.getValue().intValue());
list.subList(3, 6).clear();
assertEquals(5, lastObserved.getValue().intValue());
}
示例9: VirtualWebView
import org.reactfx.value.Var; //导入方法依赖的package包/类
VirtualWebView(WebView webView) {
this.webView = webView;
getChildren().add(webView);
totalWidth = Val.create(() -> Double.parseDouble(String.valueOf(webView.getEngine().executeScript("document.body.scrollWidth"))));
totalHeight = Val.create(() -> Double.parseDouble(String.valueOf(webView.getEngine().executeScript("document.body.scrollHeight"))));
estimateScrollX = Var.newSimpleVar(Double.parseDouble(String.valueOf(webView.getEngine().executeScript("window.pageXOffset || document.documentElement.scrollLeft"))));
estimateScrollY = Var.newSimpleVar(Double.parseDouble(String.valueOf(webView.getEngine().executeScript("window.pageYOffset || document.documentElement.scrollTop"))));
}
示例10: testMultipleModificationsWhenBound
import org.reactfx.value.Var; //导入方法依赖的package包/类
@Test
public void testMultipleModificationsWhenBound() {
SuspendableList<Integer> list = new LiveArrayList<>(1, 1, 1, 1, 1).suspendable();
Val<Integer> sum = list.reduce((a, b) -> a + b);
Var<Integer> lastObserved = Var.newSimpleVar(null);
sum.observeChanges((obs, oldVal, newVal) -> lastObserved.setValue(newVal));
list.suspendWhile(() -> {
list.addAll(0, Arrays.asList(3, 2));
list.remove(4, 6);
list.addAll(4, Arrays.asList(8, 15));
});
assertEquals(31, lastObserved.getValue().intValue());
}
示例11: testRecursion
import org.reactfx.value.Var; //导入方法依赖的package包/类
@Test
public void testRecursion() {
SuspendableList<Integer> list = new LiveArrayList<>(1, 1, 1, 1, 1).suspendable();
Val<Integer> sum = list.reduce((a, b) -> a + b);
Var<Integer> lastObserved = Var.newSimpleVar(null);
Random random = new Random(0xcafebabe);
sum.addListener(obs -> {
Integer newVal = sum.getValue();
if(newVal < 1000) {
list.suspendWhile(() -> {
// remove 4 items
int i = random.nextInt(list.size() - 3);
list.subList(i, i + 3).clear();
list.remove(random.nextInt(list.size()));
// insert 5 items
list.addAll(random.nextInt(list.size()), Arrays.asList(
random.nextInt(12), random.nextInt(12), random.nextInt(12)));
list.add(random.nextInt(list.size()), random.nextInt(12));
list.add(random.nextInt(list.size()), random.nextInt(12));
});
}
});
sum.observeChanges((obs, oldVal, newVal) -> lastObserved.setValue(newVal));
list.set(2, 0);
assertThat(lastObserved.getValue().intValue(), greaterThanOrEqualTo(1000));
}
示例12: start
import org.reactfx.value.Var; //导入方法依赖的package包/类
@Override
public void start(Stage primaryStage) {
Circle circle = new Circle(30.0);
Pane canvas = new Pane(circle);
// animate circle position
Var<Point2D> center = Var.newSimpleVar(new Point2D(W/2, H/2));
Val<Point2D> animCenter = center.animate(
(p1, p2) -> Duration.ofMillis((long) p1.distance(p2)),
(p1, p2, frac) -> p1.multiply(1.0-frac).add(p2.multiply(frac)));
circle.centerXProperty().bind(animCenter.map(Point2D::getX));
circle.centerYProperty().bind(animCenter.map(Point2D::getY));
// animate circle color
Var<Color> color = Var.newSimpleVar(Color.BLUE);
Val<Color> animColor = Val.animate(color, Duration.ofMillis(500));
circle.fillProperty().bind(animColor);
// on click, move to random position and transition to random color
Random random = new Random();
circle.setOnMouseClicked(click -> {
center.setValue(new Point2D(
random.nextInt(W),
random.nextInt(H)));
color.setValue(Color.rgb(
random.nextInt(240),
random.nextInt(240),
random.nextInt(240)));
});
primaryStage.setScene(new Scene(canvas, W, H));
primaryStage.show();
}
示例13: testAutoEmittingStream
import org.reactfx.value.Var; //导入方法依赖的package包/类
@Test
public void testAutoEmittingStream() {
List<Integer> emitted = new ArrayList<>();
Var<Integer> source = Var.newSimpleVar(1);
EventStream<Integer> stream = source.values().withDefaultEvent(0);
stream.subscribe(emitted::add);
assertEquals(Arrays.asList(1), emitted);
source.setValue(2);
assertEquals(Arrays.asList(1, 2), emitted);
}