From 1ec1b3357e34fbf265e6cd555a86fdf73e6d9d3e Mon Sep 17 00:00:00 2001 From: Jonas Pohl Date: Tue, 18 Jun 2024 22:33:57 +0200 Subject: [PATCH] fun with switch --- .../RobbiSimulator/model/Territory.java | 16 +++++----- .../RobbiSimulator/model/TerritoryState.java | 30 +++++++++---------- .../utils/PropertiesLoader.java | 2 +- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/JayPi4c/RobbiSimulator/model/Territory.java b/src/main/java/com/JayPi4c/RobbiSimulator/model/Territory.java index 945f68a..58625ff 100644 --- a/src/main/java/com/JayPi4c/RobbiSimulator/model/Territory.java +++ b/src/main/java/com/JayPi4c/RobbiSimulator/model/Territory.java @@ -247,7 +247,7 @@ public synchronized Item getItem(int x, int y) { * @return the item that has been removed from the tile */ public Item removeItem(int x, int y) { - Item i = null; + Item i; synchronized (this) { Tile t = tiles[normalizeCoordinate(x, numberOfColumns)][normalizeCoordinate(y, numberOfRows)]; i = t.pickItem(); @@ -581,14 +581,12 @@ public boolean fromXML(InputStream stream) { */ private Item getItemFromParser(XMLStreamReader parser) { String type = parser.getAttributeValue(null, "type"); - if (type.equals("Nut")) { - return new Nut(); - } else if (type.equals("Accu")) { - return new Accu(); - } else if (type.equals("Screw")) { - return new Screw(); - } - return null; + return switch (type) { + case "Nut" -> new Nut(); + case "Accu" -> new Accu(); + case "Screw" -> new Screw(); + default -> null; + }; } /** diff --git a/src/main/java/com/JayPi4c/RobbiSimulator/model/TerritoryState.java b/src/main/java/com/JayPi4c/RobbiSimulator/model/TerritoryState.java index 370aa84..556a239 100644 --- a/src/main/java/com/JayPi4c/RobbiSimulator/model/TerritoryState.java +++ b/src/main/java/com/JayPi4c/RobbiSimulator/model/TerritoryState.java @@ -42,22 +42,22 @@ public TerritoryState(int numberOfColumns, int numberOfRows, Tile[][] tiles, Rob this.tiles = new Tile[tiles.length][tiles[0].length]; for (int i = 0; i < tiles.length; i++) { for (int j = 0; j < tiles[i].length; j++) { - Tile t = tiles[i][j]; - Tile newTile; - if (t instanceof Hollow) { - newTile = new Hollow(); - } else if (t instanceof PileOfScrap) { - newTile = new PileOfScrap(); - } else if (t instanceof Stockpile stockpile) { - newTile = new Stockpile(); - for (Item item : stockpile.getAllItems()) - newTile.setItem(item); - } else { - newTile = new Tile(); - newTile.setItem(t.getItem()); - } + Tile newTile = switch (tiles[i][j]) { + case Hollow ignored: + yield new Hollow(); + case PileOfScrap ignored: + yield new PileOfScrap(); + case Stockpile stockpile: + Stockpile s = new Stockpile(); + for (Item item : stockpile.getAllItems()) + s.setItem(item); + yield s; + case Tile t: + Tile t2 = new Tile(); + t2.setItem(t.getItem()); + yield t2; + }; this.tiles[i][j] = newTile; - } } this.robbiState = new RobbiState(robbi.getX(), robbi.getY(), robbi.getFacing(), robbi.getItem()); diff --git a/src/main/java/com/JayPi4c/RobbiSimulator/utils/PropertiesLoader.java b/src/main/java/com/JayPi4c/RobbiSimulator/utils/PropertiesLoader.java index e375acc..15bfc8b 100644 --- a/src/main/java/com/JayPi4c/RobbiSimulator/utils/PropertiesLoader.java +++ b/src/main/java/com/JayPi4c/RobbiSimulator/utils/PropertiesLoader.java @@ -137,7 +137,7 @@ public static Locale getLocale() { * @return true, if the saving was successful, false otherwise */ public static boolean finish() { - properties.put(LANGUAGE_PROPERTY, I18nUtils.getLocale().toString()); + properties.put(LANGUAGE_PROPERTY, I18nUtils.getLocale().toLanguageTag()); properties.put(SOUNDS_PROPERTY, Boolean.toString(SoundManager.getSound())); properties.put(DARKMODE_PROPERTY, Boolean.toString(SceneManager.getDarkmode())); try (FileOutputStream fos = new FileOutputStream(DIR + FILE)) {