11import java .util .*;
22
33public class PiecingItTogether {
4- private final double epsilon = 1e-6 ;
4+ private static final double DOUBLE_EQUALITY_TOLERANCE = 1e-9 ;
55
6- public JigsawInfo getCompleteInformation (JigsawInfo input ) {
6+ public static JigsawInfo getCompleteInformation (JigsawInfo input ) {
77 Integer rows = input .getRows ().isPresent () ? input .getRows ().getAsInt () : null ;
88 Integer cols = input .getColumns ().isPresent () ? input .getColumns ().getAsInt () : null ;
99 Integer pieces = input .getPieces ().isPresent () ? input .getPieces ().getAsInt () : null ;
@@ -139,10 +139,10 @@ public JigsawInfo getCompleteInformation(JigsawInfo input) {
139139 }
140140 }
141141
142- private String getFormatFromAspect (double aspectRatio ) {
142+ private static String getFormatFromAspect (double aspectRatio ) {
143143 String format ;
144144
145- if (Math .abs (aspectRatio - 1.0 ) < epsilon ) {
145+ if (Math .abs (aspectRatio - 1.0 ) < DOUBLE_EQUALITY_TOLERANCE ) {
146146 format = "square" ;
147147 } else if (aspectRatio < 1.0 ) {
148148 format = "portrait" ;
@@ -153,25 +153,25 @@ private String getFormatFromAspect(double aspectRatio) {
153153 return format ;
154154 }
155155
156- private Integer roundIfClose (double value ) {
156+ private static Integer roundIfClose (double value ) {
157157 double rounded = Math .round (value );
158158
159- if (Math .abs (value - rounded ) < epsilon ) {
159+ if (Math .abs (value - rounded ) < DOUBLE_EQUALITY_TOLERANCE ) {
160160 return (int ) rounded ;
161161 }
162162
163163 throw new IllegalArgumentException ("Contradictory data" );
164164 }
165165
166- private int calculateOtherSideFromPieces (int knownSide , int pieces ) {
166+ private static int calculateOtherSideFromPieces (int knownSide , int pieces ) {
167167 if (pieces <= 0 || pieces % knownSide != 0 ) {
168168 throw new IllegalArgumentException ("Contradictory data" );
169169 }
170170
171171 return pieces / knownSide ;
172172 }
173173
174- private int calculateOtherSideFromBorder (int knownSide , int border ) {
174+ private static int calculateOtherSideFromBorder (int knownSide , int border ) {
175175 if (knownSide == 1 ) {
176176 return border ;
177177 }
@@ -187,15 +187,15 @@ private int calculateOtherSideFromBorder(int knownSide, int border) {
187187 return ((border + 4 ) - 2 * knownSide ) / 2 ;
188188 }
189189
190- private int calculateOtherSideFromInside (int knownSide , int inside ) {
190+ private static int calculateOtherSideFromInside (int knownSide , int inside ) {
191191 if (knownSide <= 2 || inside % (knownSide - 2 ) != 0 ) {
192192 throw new IllegalArgumentException ("Contradictory data" );
193193 }
194194
195195 return (inside / (knownSide - 2 )) + 2 ;
196196 }
197197
198- private Optional <Integer > calculateOtherSide (int knownSide , boolean isRowKnown , Integer pieces , Integer border , Integer inside , Double aspect ) {
198+ private static Optional <Integer > calculateOtherSide (int knownSide , boolean isRowKnown , Integer pieces , Integer border , Integer inside , Double aspect ) {
199199 if (pieces != null ) {
200200 return Optional .of (calculateOtherSideFromPieces (knownSide , pieces ));
201201 } else if (border != null ) {
@@ -217,7 +217,7 @@ private Optional<Integer> calculateOtherSide(int knownSide, boolean isRowKnown,
217217 return Optional .empty ();
218218 }
219219
220- private JigsawInfo fromRowsAndCols (int rows , int cols ) {
220+ private static JigsawInfo fromRowsAndCols (int rows , int cols ) {
221221 int pieces = rows * cols ;
222222 int border = (rows == 1 || cols == 1 ) ? pieces : 2 * (rows + cols - 2 );
223223 int inside = pieces - border ;
@@ -244,7 +244,7 @@ private JigsawInfo fromRowsAndCols(int rows, int cols) {
244244 * @param input the original partial input with possibly known values
245245 * @throws IllegalArgumentException if any known value in the input conflicts with the computed result
246246 */
247- public void checkConsistencyOrThrow (JigsawInfo computed , JigsawInfo input ) {
247+ private static void checkConsistencyOrThrow (JigsawInfo computed , JigsawInfo input ) {
248248 if (!valuesMatch (computed .getPieces (), input .getPieces ()) || !valuesMatch (computed .getBorder (), input .getBorder ()) || !valuesMatch (computed .getInside (), input .getInside ()) || !valuesMatch (computed .getRows (), input .getRows ()) || !valuesMatch (computed .getColumns (), input .getColumns ()) || !valuesMatch (computed .getAspectRatio (), input .getAspectRatio ()) || !valuesMatch (computed .getFormat (), input .getFormat ())) {
249249 throw new IllegalArgumentException ("Contradictory data" );
250250 }
@@ -260,7 +260,7 @@ public void checkConsistencyOrThrow(JigsawInfo computed, JigsawInfo input) {
260260 * @param input the original input to check for consistency
261261 * @return an Optional containing a valid inferred configuration, or empty if the guess is invalid
262262 */
263- private Optional <JigsawInfo > tryGuessWithFixedSide (int fixed , boolean isRowFixed , double aspect , JigsawInfo input ) {
263+ private static Optional <JigsawInfo > tryGuessWithFixedSide (int fixed , boolean isRowFixed , double aspect , JigsawInfo input ) {
264264 try {
265265 int other = isRowFixed ? roundIfClose (fixed * aspect ) : roundIfClose (fixed / aspect );
266266
@@ -284,7 +284,7 @@ private Optional<JigsawInfo> tryGuessWithFixedSide(int fixed, boolean isRowFixed
284284 * @param input the original input to check for consistency
285285 * @return an Optional containing a valid configuration, or empty if inconsistent
286286 */
287- private Optional <JigsawInfo > tryGuess (int rows , int cols , JigsawInfo input ) {
287+ private static Optional <JigsawInfo > tryGuess (int rows , int cols , JigsawInfo input ) {
288288 try {
289289 JigsawInfo guess = fromRowsAndCols (rows , cols );
290290 checkConsistencyOrThrow (guess , input );
@@ -304,23 +304,23 @@ private Optional<JigsawInfo> tryGuess(int rows, int cols, JigsawInfo input) {
304304 * @param <T> the type of the contained values
305305 * @return true if the values are consistent (both missing or equal if present), false otherwise
306306 */
307- private <T > boolean valuesMatch (Optional <T > a , Optional <T > b ) {
307+ private static <T > boolean valuesMatch (Optional <T > a , Optional <T > b ) {
308308 if (a .isPresent () && b .isPresent ()) {
309309 return Objects .equals (a .get (), b .get ());
310310 }
311311
312312 return true ;
313313 }
314314
315- private boolean valuesMatch (OptionalInt a , OptionalInt b ) {
315+ private static boolean valuesMatch (OptionalInt a , OptionalInt b ) {
316316 if (a .isPresent () && b .isPresent ()) {
317317 return a .getAsInt () == b .getAsInt ();
318318 }
319319
320320 return true ;
321321 }
322322
323- private boolean valuesMatch (OptionalDouble a , OptionalDouble b ) {
323+ private static boolean valuesMatch (OptionalDouble a , OptionalDouble b ) {
324324 if (a .isPresent () && b .isPresent ()) {
325325 return Double .compare (a .getAsDouble (), b .getAsDouble ()) == 0 ;
326326 }
0 commit comments