From d37134204ecd5897b452883b6ad1988d3eb969ef Mon Sep 17 00:00:00 2001 From: Hans Bering <9629717+hansi-b@users.noreply.github.com> Date: Sun, 8 Sep 2024 20:55:39 +0200 Subject: [PATCH] use Stream for FxProject children * adapt tests --- .../hansib/simplertimes/fx/TrayIconMenu.java | 15 +---------- .../simplertimes/fx/data/FxProject.java | 4 +-- .../simplertimes/fx/tree/TreeItemNode.java | 3 ++- .../simplertimes/fx/data/FxProjectSpec.groovy | 27 ++++++++++--------- 4 files changed, 20 insertions(+), 29 deletions(-) diff --git a/simplerTimesFx/src/main/java/org/hansib/simplertimes/fx/TrayIconMenu.java b/simplerTimesFx/src/main/java/org/hansib/simplertimes/fx/TrayIconMenu.java index 0968508..f877853 100755 --- a/simplerTimesFx/src/main/java/org/hansib/simplertimes/fx/TrayIconMenu.java +++ b/simplerTimesFx/src/main/java/org/hansib/simplertimes/fx/TrayIconMenu.java @@ -20,8 +20,6 @@ import java.awt.GraphicsEnvironment; import java.awt.SystemTray; -import java.util.ArrayList; -import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -64,8 +62,7 @@ static TrayIconMenu create(Stage primaryStage, ObservableData data) { .addTitleItem(true) // .separator(); - for (MenuItem p : createProjectMenus(data)) - menuBuilder.menuItem(p); + data.fxProjectTree().children().map(project -> createMenu(project)).forEach(menuBuilder::menuItem); tim.iconWithMenu = menuBuilder // .separator().addExitMenuItem(MenuItems.Exit.fmt()) // @@ -73,16 +70,6 @@ static TrayIconMenu create(Stage primaryStage, ObservableData data) { return tim; } - private static List createProjectMenus(ObservableData data) { - FxProject projectTree = data.fxProjectTree(); - - List menus = new ArrayList<>(); - for (FxProject project : projectTree.children()) { - menus.add(createMenu(project)); - } - return menus; - } - /* * Create menu items of all project leaves in this project. */ diff --git a/simplerTimesFx/src/main/java/org/hansib/simplertimes/fx/data/FxProject.java b/simplerTimesFx/src/main/java/org/hansib/simplertimes/fx/data/FxProject.java index c216cd8..7cc2e54 100644 --- a/simplerTimesFx/src/main/java/org/hansib/simplertimes/fx/data/FxProject.java +++ b/simplerTimesFx/src/main/java/org/hansib/simplertimes/fx/data/FxProject.java @@ -106,8 +106,8 @@ public boolean hasChildren() { } @Override - public Iterable children() { - return project.children().stream().map(fxByProject::get).toList(); + public Stream children() { + return project.children().stream().map(fxByProject::get); } /** diff --git a/simplerTimesFx/src/main/java/org/hansib/simplertimes/fx/tree/TreeItemNode.java b/simplerTimesFx/src/main/java/org/hansib/simplertimes/fx/tree/TreeItemNode.java index 021c35d..0622471 100644 --- a/simplerTimesFx/src/main/java/org/hansib/simplertimes/fx/tree/TreeItemNode.java +++ b/simplerTimesFx/src/main/java/org/hansib/simplertimes/fx/tree/TreeItemNode.java @@ -19,6 +19,7 @@ package org.hansib.simplertimes.fx.tree; import java.util.Comparator; +import java.util.stream.Stream; import javafx.scene.control.TreeItem; @@ -45,7 +46,7 @@ interface PreRemovalCallback { T addChild(String childText); - Iterable children(); + Stream children(); /** * Sorts the children of this node by their texts according to the argument diff --git a/simplerTimesFx/src/test/groovy/org/hansib/simplertimes/fx/data/FxProjectSpec.groovy b/simplerTimesFx/src/test/groovy/org/hansib/simplertimes/fx/data/FxProjectSpec.groovy index a654f42..6c740a2 100644 --- a/simplerTimesFx/src/test/groovy/org/hansib/simplertimes/fx/data/FxProjectSpec.groovy +++ b/simplerTimesFx/src/test/groovy/org/hansib/simplertimes/fx/data/FxProjectSpec.groovy @@ -59,8 +59,8 @@ public class FxProjectSpec extends Specification { when: def fx = FxProject.root(root) - def pFx = fx.children()[0] - def s2Fx = pFx.children()[0].children()[0] + def pFx = children(fx)[0] + def s2Fx = children(children(pFx)[0])[0] then: s2Fx.formatName(pFx, " - ") == "hello - world" @@ -77,11 +77,11 @@ public class FxProjectSpec extends Specification { def fx = FxProject.root(root) then: - def children = fx.children() + def c = children(fx) fx.flatList() == [ - children[0], - children[0].children()[0], - children[1] + c[0], + children(c[0])[0], + c[1] ] } @@ -90,16 +90,16 @@ public class FxProjectSpec extends Specification { given: def c1 = root.add('1') c1.add('1b') - root.add('2') + root.add('2x') when: def fx = FxProject.root(root) then: - def children = fx.children() + def c = children(fx) fx.leafChildren().toList() == [ - children[0].children()[0], - children[1] + children(c[0])[0], // 1b + c[1] // 2x ] } @@ -112,7 +112,10 @@ public class FxProjectSpec extends Specification { def fx = FxProject.root(root) then: - def children = fx.children() - children[0].leafChildren().toList() == [] + children(fx)[0].leafChildren().toList() == [] + } + + static List children(FxProject p) { + p.children().toList() } }