From 62e8d0683d378159271b639f317329e183a705c5 Mon Sep 17 00:00:00 2001 From: Timur Saglam Date: Thu, 3 Oct 2024 19:53:57 +0200 Subject: [PATCH] Resolve various Sonar issues to reduce technical debt. --- src/main/java/carcassonne/model/Player.java | 4 ++-- .../model/ai/AbstractCarcassonneMove.java | 15 ++++++++++++++- .../java/carcassonne/model/grid/GridSpot.java | 8 ++++---- .../carcassonne/view/main/MeepleDepiction.java | 2 +- .../carcassonne/view/secondary/SecondaryView.java | 2 +- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/main/java/carcassonne/model/Player.java b/src/main/java/carcassonne/model/Player.java index 4a46cb3..833bf45 100644 --- a/src/main/java/carcassonne/model/Player.java +++ b/src/main/java/carcassonne/model/Player.java @@ -2,7 +2,7 @@ import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; +import java.util.EnumMap; import java.util.List; import java.util.Map; @@ -196,7 +196,7 @@ public String toString() { private void initializeScores() { overallScore = 0; - terrainSpecificScores = new HashMap<>(); + terrainSpecificScores = new EnumMap<>(TerrainType.class); for (int i = 0; i < TerrainType.values().length - 1; i++) { terrainSpecificScores.put(TerrainType.values()[i], 0); // initial scores are zero } diff --git a/src/main/java/carcassonne/model/ai/AbstractCarcassonneMove.java b/src/main/java/carcassonne/model/ai/AbstractCarcassonneMove.java index 197c060..9ba87a8 100644 --- a/src/main/java/carcassonne/model/ai/AbstractCarcassonneMove.java +++ b/src/main/java/carcassonne/model/ai/AbstractCarcassonneMove.java @@ -51,7 +51,20 @@ public AbstractCarcassonneMove(TemporaryTile tile, GridDirection meeplePosition, @Override public int compareTo(AbstractCarcassonneMove other) { - return Double.valueOf(getValue()).compareTo(other.getValue()); + return Double.compare(getValue(), other.getValue()); + } + + @Override + public boolean equals(Object other) { + if (other instanceof AbstractCarcassonneMove otherMove) { + return compareTo(otherMove) == 0; + } + return false; + } + + @Override + public int hashCode() { + return Double.hashCode(getValue()); } /** diff --git a/src/main/java/carcassonne/model/grid/GridSpot.java b/src/main/java/carcassonne/model/grid/GridSpot.java index cc4428b..e4219e1 100644 --- a/src/main/java/carcassonne/model/grid/GridSpot.java +++ b/src/main/java/carcassonne/model/grid/GridSpot.java @@ -3,7 +3,7 @@ import static carcassonne.model.grid.GridDirection.CENTER; import java.util.Collection; -import java.util.HashMap; +import java.util.EnumMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; @@ -35,7 +35,7 @@ public GridSpot(Grid grid, int x, int y) { this.grid = grid; this.x = x; this.y = y; - tagMap = new HashMap<>(); + tagMap = new EnumMap<>(GridDirection.class); for (GridDirection direction : GridDirection.values()) { tagMap.put(direction, new HashSet<>()); } @@ -117,7 +117,7 @@ public int getY() { * @param tilePosition is the specific position. * @return true if not directly or indirectly tagged. */ - public Boolean isIndirectlyTagged(GridDirection tilePosition) { + public boolean isIndirectlyTagged(GridDirection tilePosition) { for (GridDirection otherPosition : GridDirection.values()) { if (isTagged(otherPosition) && tile.hasConnection(tilePosition, otherPosition)) { return true; @@ -133,7 +133,7 @@ public Boolean isIndirectlyTagged(GridDirection tilePosition) { * @param tagger is the specific grid pattern. * @return true if not directly or indirectly tagged by the grid pattern. */ - public Boolean isIndirectlyTaggedBy(GridDirection tilePosition, GridPattern tagger) { + public boolean isIndirectlyTaggedBy(GridDirection tilePosition, GridPattern tagger) { for (GridDirection otherPosition : GridDirection.values()) { if (tile.hasConnection(tilePosition, otherPosition) && tagMap.get(otherPosition).contains(tagger)) { return true; diff --git a/src/main/java/carcassonne/view/main/MeepleDepiction.java b/src/main/java/carcassonne/view/main/MeepleDepiction.java index 9f2d935..db8367e 100644 --- a/src/main/java/carcassonne/view/main/MeepleDepiction.java +++ b/src/main/java/carcassonne/view/main/MeepleDepiction.java @@ -36,7 +36,7 @@ public class MeepleDepiction { public MeepleDepiction(int scalingFactor, ControllerFacade controller, GridDirection direction) { updateMeepleSize(scalingFactor); terrain = TerrainType.OTHER; - label = new JLabel(PaintShop.getPreviewMeeple(terrain, meepleSize)); // new RigidLabel(meepleSize, meepleSize); + label = new JLabel(PaintShop.getPreviewMeeple(terrain, meepleSize)); label.setHorizontalAlignment(SwingConstants.CENTER); preview = false; mouseAdapter = new MouseAdapter() { diff --git a/src/main/java/carcassonne/view/secondary/SecondaryView.java b/src/main/java/carcassonne/view/secondary/SecondaryView.java index 4bd3828..a939978 100644 --- a/src/main/java/carcassonne/view/secondary/SecondaryView.java +++ b/src/main/java/carcassonne/view/secondary/SecondaryView.java @@ -33,7 +33,7 @@ public abstract class SecondaryView extends JDialog implements NotifiableView { * @param controller sets the {@link MainController}. * @param ui is the main graphical user interface. */ - public SecondaryView(ControllerFacade controller, MainView ui) { + protected SecondaryView(ControllerFacade controller, MainView ui) { super(ui); dialogPanel = new JPanel(new GridBagLayout()); this.controller = controller;