From 4ddaaaf7580ed0d430e3cfe48b804fe58d0483de Mon Sep 17 00:00:00 2001 From: TreeSnowFence <139384610+TreeSnowFence@users.noreply.github.com> Date: Fri, 26 Jul 2024 14:53:56 -0400 Subject: [PATCH] Added Jetbrains notation to StarBattle --- .../java/edu/rpi/legup/puzzle/nurikabe/Nurikabe.java | 9 +++++++-- .../edu/rpi/legup/puzzle/nurikabe/NurikabeCell.java | 5 +++-- .../edu/rpi/legup/puzzle/starbattle/StarBattle.java | 7 ++++++- .../rpi/legup/puzzle/starbattle/StarBattleCell.java | 8 ++++++-- .../legup/puzzle/starbattle/StarBattleCellFactory.java | 9 ++++++--- .../legup/puzzle/starbattle/StarBattleController.java | 6 +++++- .../legup/puzzle/starbattle/StarBattleElementView.java | 9 ++++++--- .../legup/puzzle/starbattle/StarBattleExporter.java | 7 +++++-- .../legup/puzzle/starbattle/StarBattleImporter.java | 10 ++++++++-- .../rpi/legup/puzzle/starbattle/StarBattleView.java | 3 ++- 10 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/main/java/edu/rpi/legup/puzzle/nurikabe/Nurikabe.java b/src/main/java/edu/rpi/legup/puzzle/nurikabe/Nurikabe.java index eed8dc087..0e25e1879 100644 --- a/src/main/java/edu/rpi/legup/puzzle/nurikabe/Nurikabe.java +++ b/src/main/java/edu/rpi/legup/puzzle/nurikabe/Nurikabe.java @@ -5,6 +5,7 @@ import edu.rpi.legup.model.gameboard.PuzzleElement; import edu.rpi.legup.model.rules.ContradictionRule; import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; public class Nurikabe extends Puzzle { public Nurikabe() { @@ -20,6 +21,7 @@ public Nurikabe() { /** Initializes the game board. Called by the invoker of the class */ @Override + @Contract(pure = false) public void initializeView() { boardView = new NurikabeView((NurikabeBoard) currentBoard); boardView.setBoard(currentBoard); @@ -33,6 +35,7 @@ public void initializeView() { * @return board of the random edu.rpi.legup.puzzle */ @Override + @Contract("_ -> null") public Board generatePuzzle(int difficulty) { return null; } @@ -57,7 +60,8 @@ public boolean isValidDimensions(int rows, int columns) { * @return true if board is valid, false otherwise */ @Override - public boolean isBoardComplete(Board board) { + @Contract(pure = true) + public boolean isBoardComplete(@NotNull Board board) { NurikabeBoard nurikabeBoard = (NurikabeBoard) board; for (ContradictionRule rule : contradictionRules) { @@ -80,5 +84,6 @@ public boolean isBoardComplete(Board board) { * @param board the board that has changed */ @Override - public void onBoardChange(Board board) {} + @Contract(pure = true) + public void onBoardChange(@NotNull Board board) {} } diff --git a/src/main/java/edu/rpi/legup/puzzle/nurikabe/NurikabeCell.java b/src/main/java/edu/rpi/legup/puzzle/nurikabe/NurikabeCell.java index b89015333..7f5869784 100644 --- a/src/main/java/edu/rpi/legup/puzzle/nurikabe/NurikabeCell.java +++ b/src/main/java/edu/rpi/legup/puzzle/nurikabe/NurikabeCell.java @@ -5,6 +5,7 @@ import java.awt.*; import java.awt.event.MouseEvent; import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; public class NurikabeCell extends GridCell { @@ -14,7 +15,7 @@ public class NurikabeCell extends GridCell { * @param value value of the NurikabeCell * @param location position of the NurikabeCell */ - public NurikabeCell(int value, Point location) { + public NurikabeCell(int value, @NotNull Point location) { super(value, location); } @@ -45,7 +46,7 @@ public NurikabeType getType() { * @param e element to set the type of this nurikabe cell to */ @Override - public void setType(Element e, MouseEvent m) { + public void setType(@NotNull Element e, @NotNull MouseEvent m) { switch (e.getElementID()) { case "NURI-PLAC-0001": this.data = -1; diff --git a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattle.java b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattle.java index 39092bbc6..9d378be3c 100644 --- a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattle.java +++ b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattle.java @@ -2,6 +2,8 @@ import edu.rpi.legup.model.Puzzle; import edu.rpi.legup.model.gameboard.Board; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; public class StarBattle extends Puzzle { public StarBattle() { @@ -15,12 +17,14 @@ public StarBattle() { } @Override + @Contract(pure = false) public void initializeView() { boardView = new StarBattleView((StarBattleBoard) currentBoard); addBoardListener(boardView); } @Override + @Contract("_ -> null") public Board generatePuzzle(int difficulty) { return null; } @@ -31,5 +35,6 @@ public boolean isBoardComplete(Board board) { } @Override - public void onBoardChange(Board board) {} + @Contract(pure = true) + public void onBoardChange(@NotNull Board board) {} } diff --git a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleCell.java b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleCell.java index ddae8f882..818866f86 100644 --- a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleCell.java +++ b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleCell.java @@ -2,6 +2,9 @@ import edu.rpi.legup.model.elements.Element; import edu.rpi.legup.model.gameboard.GridCell; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + import java.awt.*; import java.awt.event.MouseEvent; @@ -17,7 +20,7 @@ public class StarBattleCell extends GridCell { * @param groupIndex indicates what group # the cell is in. * @param size size of the star battle cell */ - public StarBattleCell(int value, Point location, int groupIndex, int size) { + public StarBattleCell(int value, @NotNull Point location, int groupIndex, int size) { super(value, location); this.groupIndex = groupIndex; this.max = size; @@ -28,7 +31,7 @@ public int getGroupIndex() { } @Override - public void setType(Element e, MouseEvent m) { + public void setType(@NotNull Element e, @NotNull MouseEvent m) { switch (e.getElementID()) { case "STBL-PLAC-0001": this.data = -3; @@ -77,6 +80,7 @@ public StarBattleCellType getType() { return null; } + @Contract(pure = true) public StarBattleCell copy() { StarBattleCell copy = new StarBattleCell(data, (Point) location.clone(), groupIndex, max); copy.setIndex(index); diff --git a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleCellFactory.java b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleCellFactory.java index eb2a830f6..038e425eb 100644 --- a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleCellFactory.java +++ b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleCellFactory.java @@ -8,10 +8,13 @@ import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; public class StarBattleCellFactory extends ElementFactory { @Override - public StarBattleCell importCell(Node node, Board board) throws InvalidFileFormatException { + @Contract(pure = false) + public StarBattleCell importCell(@NotNull Node node, @NotNull Board board) throws InvalidFileFormatException { try { if (!node.getNodeName().equalsIgnoreCase("cell")) { throw new InvalidFileFormatException( @@ -50,8 +53,8 @@ public StarBattleCell importCell(Node node, Board board) throws InvalidFileForma throw new InvalidFileFormatException("starbattle Factory: could not find attribute(s)"); } } - - public org.w3c.dom.Element exportCell(Document document, PuzzleElement puzzleElement) { + @Contract(pure = false) + public org.w3c.dom.Element exportCell(@NotNull Document document, @NotNull PuzzleElement puzzleElement) { org.w3c.dom.Element cellElement = document.createElement("cell"); StarBattleCell cell = (StarBattleCell) puzzleElement; diff --git a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleController.java b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleController.java index ba19361c9..5117a6faf 100644 --- a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleController.java +++ b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleController.java @@ -2,11 +2,15 @@ import edu.rpi.legup.controller.ElementController; import edu.rpi.legup.model.gameboard.PuzzleElement; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + import java.awt.event.MouseEvent; public class StarBattleController extends ElementController { @Override - public void changeCell(MouseEvent e, PuzzleElement data) { + @Contract(pure = false) + public void changeCell(@NotNull MouseEvent e, @NotNull PuzzleElement data) { StarBattleCell cell = (StarBattleCell) data; if (e.getButton() == MouseEvent.BUTTON1) { if (e.isControlDown()) { diff --git a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleElementView.java b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleElementView.java index 66d59d364..e1f6aa772 100644 --- a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleElementView.java +++ b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleElementView.java @@ -2,20 +2,23 @@ import edu.rpi.legup.ui.boardview.GridElementView; import java.awt.*; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; public class StarBattleElementView extends GridElementView { - public StarBattleElementView(StarBattleCell cell) { + public StarBattleElementView(@NotNull StarBattleCell cell) { super(cell); } @Override - public StarBattleCell getPuzzleElement() { + public @NotNull StarBattleCell getPuzzleElement() { return (StarBattleCell) super.getPuzzleElement(); } @Override - public void drawElement(Graphics2D graphics2D) { + @Contract(pure = true) + public void drawElement(@NotNull Graphics2D graphics2D) { StarBattleCell cell = (StarBattleCell) puzzleElement; StarBattleCellType type = cell.getType(); if (type == StarBattleCellType.STAR) { diff --git a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleExporter.java b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleExporter.java index f2d2c4d80..33fd5d135 100644 --- a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleExporter.java +++ b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleExporter.java @@ -2,14 +2,17 @@ import edu.rpi.legup.model.PuzzleExporter; import org.w3c.dom.Document; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; public class StarBattleExporter extends PuzzleExporter { - public StarBattleExporter(StarBattle starbattle) { + public StarBattleExporter(@NotNull StarBattle starbattle) { super(starbattle); } @Override - protected org.w3c.dom.Element createBoardElement(Document newDocument) { + @Contract(pure = true) + protected @NotNull org.w3c.dom.Element createBoardElement(@NotNull Document newDocument) { StarBattleBoard board = (StarBattleBoard) puzzle.getTree().getRootNode().getBoard(); org.w3c.dom.Element boardElement = newDocument.createElement("board"); boardElement.setAttribute("size", String.valueOf(board.getSize())); diff --git a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleImporter.java b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleImporter.java index 2a608c893..8c02494d7 100644 --- a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleImporter.java +++ b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleImporter.java @@ -6,21 +6,25 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; public class StarBattleImporter extends PuzzleImporter { - public StarBattleImporter(StarBattle starbattle) { + public StarBattleImporter(@NotNull StarBattle starbattle) { super(starbattle); } /** Puzzle setting to support row and column inputs */ @Override + @Contract(pure = true, value = "-> true") public boolean acceptsRowsAndColumnsInput() { return true; } /** Puzzle setting to disable support for text input */ @Override + @Contract(pure = true, value = "-> false") public boolean acceptsTextInput() { return false; } @@ -44,6 +48,7 @@ public void initializeBoard(int rows, int columns) { * @throws InvalidFileFormatException if file is invalid */ @Override + @Contract(pure = false) public void initializeBoard(Node node) throws InvalidFileFormatException { Element puzzleElement = (Element) node; int puzzle_num = Integer.parseInt(puzzleElement.getAttribute("puzzle_num")); @@ -92,7 +97,8 @@ public void initializeBoard(Node node) throws InvalidFileFormatException { * @throws UnsupportedOperationException since StarBattle does not support text input */ @Override - public void initializeBoard(String[] statements) throws UnsupportedOperationException { + @Contract(value = "_ -> fail", pure = false) + public void initializeBoard(@NotNull String[] statements) throws UnsupportedOperationException { throw new UnsupportedOperationException("Star Battle does not accept text input"); } } diff --git a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleView.java b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleView.java index 550b5495d..8fc400420 100644 --- a/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleView.java +++ b/src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleView.java @@ -6,6 +6,7 @@ import java.awt.*; import java.io.IOException; import javax.imageio.ImageIO; +import org.jetbrains.annotations.NotNull; public class StarBattleView extends GridBoardView { static Image STAR; @@ -21,7 +22,7 @@ public class StarBattleView extends GridBoardView { } } - public StarBattleView(StarBattleBoard board) { + public StarBattleView(@NotNull StarBattleBoard board) { super(new BoardController(), new StarBattleController(), board.getDimension()); for (PuzzleElement puzzleElement : board.getPuzzleElements()) {