@@ -47,18 +47,79 @@ public final class Slot {
47
47
@ CompileTimeConstant
48
48
public static final Slot NaS = getNaS ();
49
49
50
+ /**
51
+ * Creates a new slot with the specified row and column.
52
+ * @param row the row
53
+ * @param column the col
54
+ * @deprecated Use {@link #of(int, int)}, constructor will be private in 2.1.0
55
+ */
56
+ @ Deprecated
50
57
public Slot (final int row , final int column ) {
51
- this . row = row ;
52
- this . column = column ;
58
+ this ( row , column , false ) ;
59
+ checkRowColumnSafety ( slot , row , column ) ;
53
60
54
61
this .slot = (column + (row - 1 ) * 9 ) - 1 ;
55
62
this .slot = this .slot >= 54 ? -1 : this .slot ;
56
63
}
57
64
65
+ private Slot (final int row , final int column , final int slot ) {
66
+ this (row , column , false );
67
+ this .slot = slot >= 54 ? -1 : slot ;
68
+ checkRowColumnSafety (slot , row , column );
69
+ }
70
+
71
+ /**
72
+ * Creates a new slot with the specified row and column.
73
+ * @param slot the slot
74
+ * @deprecated Use {@link #of(int)}, constructor will be private in 2.1.0
75
+ */
76
+ @ Deprecated
58
77
public Slot (final int slot ) {
78
+ this (slot / 9 + 1 , slot % 9 + 1 , false );
79
+ checkRowColumnSafety (slot , row , column );
80
+
59
81
this .slot = slot >= 54 ? -1 : slot ;
60
- this .row = (slot / 9 ) + 1 ;
61
- this .column = (slot % 9 ) + 1 ;
82
+ }
83
+
84
+ private static void checkRowColumnSafety (int slot , int row , int column ) {
85
+ if (row > 6 ) {
86
+ throw new IllegalArgumentException (
87
+ "Row expected to be between 1 and 6, got " + row +
88
+ "\n Fix: slot = (column + (row - 1) * 9) - 1, slot = " + slot
89
+ );
90
+ }
91
+ if (column > 9 ) {
92
+ throw new IllegalArgumentException (
93
+ "Column expected to be between 1 and 9, got " + column +
94
+ "\n Fix: slot = (column + (row - 1) * 9) - 1, slot = " + slot
95
+ );
96
+ }
97
+ }
98
+
99
+ @ Contract ("_ -> new" )
100
+ public static @ NotNull Slot of (int slot ) {
101
+ return new Slot (slot / 9 + 1 , slot % 9 + 1 , slot );
102
+ }
103
+
104
+ @ Contract ("_, _ -> new" )
105
+ public static @ NotNull Slot of (int row , int column ) {
106
+ return new Slot (row , column , (column + (row - 1 ) * 9 ) - 1 );
107
+ }
108
+
109
+ public static @ NotNull Slot ofUnsafe (int slot ) {
110
+ Slot position = new Slot ((slot / 9 ) + 1 , (slot % 9 ) + 1 , true );
111
+
112
+ int positionSlot = (position .column + (position .row - 1 ) * 9 ) - 1 ;
113
+ position .slot = positionSlot >= 54 ? -1 : positionSlot ;
114
+ return position ;
115
+ }
116
+
117
+ public static Slot ofUnsafe (int row , int column ) {
118
+ if (column > 9 || row > 6 ) return Slot .NaS ;
119
+ Slot position = new Slot (row , column , true );
120
+ int positionSlot = (column + (row - 1 ) * 9 ) - 1 ;
121
+ position .slot = positionSlot >= 54 ? -1 : positionSlot ;
122
+ return position ;
62
123
}
63
124
64
125
// fast copy constructor
@@ -121,9 +182,7 @@ public Slot setSlot(final int slot) {
121
182
*/
122
183
@ NotNull
123
184
public Slot copy () {
124
- final Slot slot = new Slot (row , column , true );
125
- slot .slot = this .slot ;
126
- return slot ;
185
+ return new Slot (row , column , slot );
127
186
}
128
187
129
188
/**
@@ -138,7 +197,7 @@ public boolean isSlot() {
138
197
}
139
198
140
199
/**
141
- * Check if the slot is a valid slot .
200
+ * Check if the slot is valid.
142
201
* @apiNote If the slot is equal to -1, it's invalid; fast check.
143
202
* @return true if the slot does not equal -1.
144
203
*/
@@ -149,17 +208,13 @@ public boolean isValid() {
149
208
// faster NaS (Not A Slot) alternative to creating a Slot like new Slot(8, 1);
150
209
@ NotNull
151
210
private static Slot getNaS () {
152
- Slot slot = new Slot (-1 , -1 , true );
153
- slot .slot = -1 ;
154
- return slot ;
211
+ return new Slot (-1 , -1 , -1 );
155
212
}
156
213
157
214
// faster FIRST slot compared to doing "new Slot(1, 1)"
158
215
@ NotNull
159
216
public static Slot getFirst () {
160
- Slot slot = new Slot (1 , 1 , true );
161
- slot .slot = 0 ;
162
- return slot ;
217
+ return new Slot (1 , 1 , 0 );
163
218
}
164
219
165
220
@ Override
0 commit comments