Skip to content

Commit cf80209

Browse files
committed
Change cloning mechanism, change many fields to final
1 parent 301d76c commit cf80209

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

sudoku/src/main/java/de/sfuhrm/sudoku/CachedGameMatrixImpl.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ class CachedGameMatrixImpl extends GameMatrixImpl {
3535
* A set 2-bit means that the digit 2 is free for use.
3636
* And so on.
3737
*/
38-
private int[] rowFree;
38+
private final int[] rowFree;
3939

4040
/** Buffered free masks per column.
4141
* @see #rowFree
4242
*/
43-
private int[] columnFree;
43+
private final int[] columnFree;
4444

4545
/** Buffered free masks per block.
4646
* @see #rowFree
4747
*/
48-
private int[][] blockFree;
48+
private final int[][] blockFree;
4949

5050
/** The count of non-{@link GameSchema#getUnsetValue() unset} cells.
5151
* @see #getSetCount()
@@ -77,6 +77,18 @@ class CachedGameMatrixImpl extends GameMatrixImpl {
7777
}
7878
}
7979

80+
/**
81+
* Clone constructor.
82+
* @param source the source to initialize with.
83+
*/
84+
CachedGameMatrixImpl(final CachedGameMatrixImpl source) {
85+
super(source);
86+
87+
blockFree = QuadraticArrays.cloneArray(source.blockFree);
88+
columnFree = Arrays.copyOf(source.columnFree, source.columnFree.length);
89+
rowFree = Arrays.copyOf(source.rowFree, source.rowFree.length);
90+
}
91+
8092
@Override
8193
int getBlockFreeMask(final int row, final int column) {
8294
final int blockWidth = getSchema().getBlockWidth();
@@ -149,11 +161,6 @@ public int getSetCount() {
149161

150162
@Override
151163
public CachedGameMatrixImpl clone() {
152-
CachedGameMatrixImpl clone;
153-
clone = (CachedGameMatrixImpl) super.clone();
154-
clone.blockFree = QuadraticArrays.cloneArray(blockFree);
155-
clone.columnFree = Arrays.copyOf(columnFree, columnFree.length);
156-
clone.rowFree = Arrays.copyOf(rowFree, rowFree.length);
157-
return clone;
164+
return new CachedGameMatrixImpl(this);
158165
}
159166
}

sudoku/src/main/java/de/sfuhrm/sudoku/GameMatrixImpl.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class GameMatrixImpl implements Cloneable, GameMatrix {
3939
* The values 1-9 mean the corresponding cell
4040
* value.
4141
*/
42-
private byte[][] data;
42+
private final byte[][] data;
4343

4444
/**
4545
* Creates an empty riddle.
@@ -52,6 +52,16 @@ class GameMatrixImpl implements Cloneable, GameMatrix {
5252
data = new byte[inGameSchema.getWidth()][inGameSchema.getWidth()];
5353
}
5454

55+
/**
56+
* Clone constructor.
57+
* @param source source matrix to init with.
58+
* @see #setAll(byte[][])
59+
*/
60+
GameMatrixImpl(final GameMatrixImpl source) {
61+
this.gameSchema = source.gameSchema;
62+
data = QuadraticArrays.cloneArray(source.data);
63+
}
64+
5565
/** Sets all cells to the given values.
5666
* @param initializationData initialization data with the first dimension
5767
* being the rows and the second dimension being the columns.
@@ -208,15 +218,7 @@ public final boolean equals(final Object obj) {
208218

209219
@Override
210220
public GameMatrixImpl clone() {
211-
GameMatrixImpl clone;
212-
try {
213-
clone = (GameMatrixImpl) super.clone();
214-
clone.data = QuadraticArrays.cloneArray(data);
215-
} catch (CloneNotSupportedException ex) {
216-
throw new IllegalStateException(ex);
217-
}
218-
219-
return clone;
221+
return new GameMatrixImpl(this);
220222
}
221223

222224
/** Finds the duplicate bits.

sudoku/src/main/java/de/sfuhrm/sudoku/RiddleImpl.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class RiddleImpl extends GameMatrixImpl implements Riddle {
3131
* Whether the cell is writable. Pre-defined cells are only readable, use
3232
* settable cells are writable.
3333
*/
34-
private boolean[][] writeable;
34+
private final boolean[][] writeable;
3535

3636
/**
3737
* Creates an empty full-writable riddle.
@@ -49,6 +49,15 @@ class RiddleImpl extends GameMatrixImpl implements Riddle {
4949
}
5050
}
5151

52+
/**
53+
* Clone constructor.
54+
* @param source source data to init with.
55+
*/
56+
RiddleImpl(final RiddleImpl source) {
57+
super(source);
58+
writeable = QuadraticArrays.cloneArray(source.writeable);
59+
}
60+
5261
/**
5362
* Get whether a certain field is writable.
5463
* @param row the row of the cell to get the writability for.
@@ -75,9 +84,6 @@ public final void setWritable(final int row,
7584

7685
@Override
7786
public final RiddleImpl clone() {
78-
RiddleImpl clone;
79-
clone = (RiddleImpl) super.clone();
80-
clone.writeable = QuadraticArrays.cloneArray(writeable);
81-
return clone;
87+
return new RiddleImpl(this);
8288
}
8389
}

0 commit comments

Comments
 (0)