fixes some code inspection issues

Mon, 27 Jul 2020 10:56:17 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Jul 2020 10:56:17 +0200
changeset 12
1c62c6009161
parent 11
f7433671fec5
child 13
5e69b1bb707f

fixes some code inspection issues

manifest.mf file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/sudoku/ActionHandler.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/sudoku/ButtonPanel.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/sudoku/DocumentHandler.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/sudoku/Field.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/sudoku/MainMenu.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/sudoku/Solver.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/sudoku/SudokuTextField.java file | annotate | diff | comparison | revisions
--- a/manifest.mf	Sat Jul 25 15:56:48 2020 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-Manifest-Version: 1.0
-X-COMMENT: Main-Class will be added automatically by build
-
--- a/src/main/java/de/uapcore/sudoku/ActionHandler.java	Sat Jul 25 15:56:48 2020 +0200
+++ b/src/main/java/de/uapcore/sudoku/ActionHandler.java	Mon Jul 27 10:56:17 2020 +0200
@@ -47,9 +47,9 @@
     public static final String QUIT = "quit";
     public static final String ABOUT = "about";
 
-    private Field field;
-    private Solver solver;
-    private DocumentHandler doc;
+    private final Field field;
+    private final Solver solver;
+    private final DocumentHandler doc;
 
     /**
      * Constructs a new action handler instance.
--- a/src/main/java/de/uapcore/sudoku/ButtonPanel.java	Sat Jul 25 15:56:48 2020 +0200
+++ b/src/main/java/de/uapcore/sudoku/ButtonPanel.java	Mon Jul 27 10:56:17 2020 +0200
@@ -34,7 +34,7 @@
  */
 public final class ButtonPanel extends JPanel {
     
-    private JButton save, check, solve;
+    private final JButton save, check, solve;
 
     public ButtonPanel(ActionHandler l) {
         setLayout(new GridBagLayout());
--- a/src/main/java/de/uapcore/sudoku/DocumentHandler.java	Sat Jul 25 15:56:48 2020 +0200
+++ b/src/main/java/de/uapcore/sudoku/DocumentHandler.java	Mon Jul 27 10:56:17 2020 +0200
@@ -59,13 +59,13 @@
                 }
                 Matcher m = pat.matcher(line);
                 if (m.matches()) {
-                    String c[] = line.trim().split(" ");
+                    String[] c = line.trim().split(" ");
                     if (c.length != 9) {
                         break;
                     }
                     for (int i = 0 ; i < 9 ; i++) {
                         field.setCellValue(i, row,
-                                c[i].equals("_") ? 0 : Integer.valueOf(c[i]));
+                                c[i].equals("_") ? 0 : Integer.parseInt(c[i]));
                     }
                     row++;
                 } else {
--- a/src/main/java/de/uapcore/sudoku/Field.java	Sat Jul 25 15:56:48 2020 +0200
+++ b/src/main/java/de/uapcore/sudoku/Field.java	Mon Jul 27 10:56:17 2020 +0200
@@ -36,7 +36,7 @@
  * Cells are identified by zero-based indices from top-left to bottom-right.
  */
 public final class Field extends JPanel {
-    private SudokuTextField[][] cells;
+    private final SudokuTextField[][] cells;
 
     /**
      * Constructs a new 9x9 Sudoku grid.
@@ -124,6 +124,7 @@
      * @param x horizontal position
      * @param y vertical position
      * @param v the cells value
+     * @throws IllegalArgumentException if v is not between 0 and 9
      */
     public void setCellValue(int x, int y, int v) {
         cells[x][y].setValue(v);
@@ -198,6 +199,7 @@
      * @param x horizontal position from 0 to 2
      * @param y vertical position from 0 to 2
      * @return a two-dimensional array containing the square cell values
+     * @throws IllegalArgumentException if the coordinates are out of bounds
      */
     public int[][] getSquare(int x, int y) {
         if (x < 0 || x > 2 || y < 0 || y > 2) {
@@ -219,12 +221,13 @@
      *
      * @param y the row position
      * @return an array containing the row values
+     * @throws IllegalArgumentException if y is not between 0 and 8
      */
     public int[] getRow(int y) {
         if (y < 0 || y > 8) {
             throw new IllegalArgumentException("Invalid row number");
         }
-        int row[] = new int[9];
+        int[] row = new int[9];
 
         for (int x = 0; x < 9; x++) {
             row[x] = getCellValue(x, y);
@@ -238,12 +241,13 @@
      *
      * @param x the column position
      * @return an array containing the column values
+     * @throws IllegalArgumentException if x is not between 0 and 8
      */
     public int[] getColumn(int x) {
         if (x < 0 || x > 8) {
             throw new IllegalArgumentException("Invalid column number");
         }
-        int column[] = new int[9];
+        int[] column = new int[9];
 
         for (int y = 0; y < 9; y++) {
             column[y] = getCellValue(x, y);
--- a/src/main/java/de/uapcore/sudoku/MainMenu.java	Sat Jul 25 15:56:48 2020 +0200
+++ b/src/main/java/de/uapcore/sudoku/MainMenu.java	Mon Jul 27 10:56:17 2020 +0200
@@ -33,8 +33,8 @@
  */
 public class MainMenu {
     
-    private ActionHandler handler;
-    private JMenuBar menuBar;
+    private final ActionHandler handler;
+    private final JMenuBar menuBar;
     
     public MainMenu(ActionHandler h) {
         handler = h;
--- a/src/main/java/de/uapcore/sudoku/Solver.java	Sat Jul 25 15:56:48 2020 +0200
+++ b/src/main/java/de/uapcore/sudoku/Solver.java	Mon Jul 27 10:56:17 2020 +0200
@@ -148,7 +148,7 @@
     public boolean solve(Field f) {
         
         // Calculate initial candidates
-        List<Integer> candidates[][] = new List[9][9];
+        List<Integer>[][] candidates = new List[9][9];
         for (int x = 0 ; x < 9 ; x++) {
             for (int y = 0 ; y < 9 ; y++) {
                 candidates[x][y] = new ArrayList<>(9);
@@ -189,7 +189,7 @@
      * @return true, if the check succeeds, false otherwise
      */
     public boolean check(Field f) {
-        int line[];
+        int[] line;
         for (int i = 0 ; i < 9 ; i++) {
             line = f.getRow(i);
             if (!valid(line)) {
@@ -201,7 +201,7 @@
             }
         }
         
-        int square[][];
+        int[][] square;
         for (int x = 0 ; x < 3 ; x++) {
             for (int y = 0 ; y < 3 ; y++) {
                 square = f.getSquare(x, y);
@@ -215,7 +215,7 @@
     }
     
     private boolean valid(int[] line) {
-        int numbers[];
+        int[] numbers;
         numbers = new int[9];
         for (int i = 0 ; i < 9 ; i++) {
             int l = line[i]-1;
--- a/src/main/java/de/uapcore/sudoku/SudokuTextField.java	Sat Jul 25 15:56:48 2020 +0200
+++ b/src/main/java/de/uapcore/sudoku/SudokuTextField.java	Mon Jul 27 10:56:17 2020 +0200
@@ -114,7 +114,7 @@
         if (getText().isEmpty()) {
             return 0;
         } else {
-            return Integer.valueOf(getText());
+            return Integer.parseInt(getText());
         }
     }
 
@@ -122,6 +122,7 @@
      * Sets the field's value.
      *
      * @param v the number from 1 to 9 or zero to clear the field
+     * @throws IllegalArgumentException if v is not between 0 and 9
      */
     public void setValue(int v) {
         if (v == 0) {

mercurial