From 988b5c0c372112525c5a9063136bfde8934afe52 Mon Sep 17 00:00:00 2001 From: akmsw Date: Tue, 2 Jan 2024 23:49:14 -0300 Subject: [PATCH] enhance text area update --- .../controllers/AnchoragesController.java | 57 +++++++++---------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/src/main/java/armameeldoparti/controllers/AnchoragesController.java b/src/main/java/armameeldoparti/controllers/AnchoragesController.java index 5afb5b8..4fdfbae 100644 --- a/src/main/java/armameeldoparti/controllers/AnchoragesController.java +++ b/src/main/java/armameeldoparti/controllers/AnchoragesController.java @@ -8,7 +8,9 @@ import armameeldoparti.utils.common.Constants; import armameeldoparti.views.AnchoragesView; import java.awt.Component; +import java.util.Comparator; import java.util.List; +import java.util.stream.IntStream; import javax.swing.JCheckBox; import javax.swing.JOptionPane; @@ -260,36 +262,31 @@ private void updateTextArea() { view.getTextArea() .setText(""); - var wrapper = new Object() { - private int anchorageNumber; - private int counter = 1; - }; - - for (wrapper.anchorageNumber = 1; wrapper.anchorageNumber <= anchoragesAmount; wrapper.anchorageNumber++) { - view.getTextArea() - .append("ANCLAJE " + wrapper.anchorageNumber + System.lineSeparator()); - - CommonFields.getPlayersSets() - .forEach( - (key, value) -> value.stream() - .filter(player -> player.getAnchorageNumber() == wrapper.anchorageNumber) - .forEach( - player -> { - view.getTextArea() - .append(wrapper.counter + ". " + player.getName() + System.lineSeparator()); - - wrapper.counter++; - } - ) - ); - - if (wrapper.anchorageNumber != anchoragesAmount) { - view.getTextArea() - .append(System.lineSeparator()); - } - - wrapper.counter = 1; - } + IntStream.range(0, anchoragesAmount) + .forEach(i -> { + view.getTextArea() + .append("ANCLAJE " + (i + 1) + System.lineSeparator()); + + List anchoredPlayers = CommonFields.getPlayersSets() + .entrySet() + .stream() + .flatMap(playersSet -> playersSet.getValue() + .stream() + .filter(player -> player.getAnchorageNumber() == (i + 1))) + .sorted(Comparator.comparing(player -> player.getPosition() + .ordinal())) + .toList(); + + for (Player player : anchoredPlayers) { + view.getTextArea() + .append((anchoredPlayers.indexOf(player) + 1) + ". " + player.getName() + System.lineSeparator()); + } + + if ((i + 1) != anchoragesAmount) { + view.getTextArea() + .append(System.lineSeparator()); + } + }); } /**