Skip to content

Commit eec11ac

Browse files
committed
Make getCompleteInformation static and improve naming of helper method and constant in piecing-it-together test file
1 parent fb6d7eb commit eec11ac

File tree

3 files changed

+33
-41
lines changed

3 files changed

+33
-41
lines changed

exercises/practice/piecing-it-together/.meta/src/reference/java/PiecingItTogether.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import java.util.*;
22

33
public 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
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
public class PiecingItTogether {
2-
public JigsawInfo getCompleteInformation(JigsawInfo input) {
2+
public static JigsawInfo getCompleteInformation(JigsawInfo input) {
33
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
44
}
55
}

exercises/practice/piecing-it-together/src/test/java/PiecingItTogetherTest.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
import org.junit.jupiter.api.BeforeEach;
21
import org.junit.jupiter.api.Disabled;
32
import org.junit.jupiter.api.Test;
43

54
import static org.assertj.core.api.Assertions.assertThat;
65
import static org.assertj.core.api.Assertions.assertThatThrownBy;
76

87
public class PiecingItTogetherTest {
9-
private PiecingItTogether solver;
10-
private double epsilon;
11-
12-
@BeforeEach
13-
public void setup() {
14-
solver = new PiecingItTogether();
15-
epsilon = 1e-6;
16-
}
8+
private static final double DOUBLE_EQUALITY_TOLERANCE = 1e-9;
179

1810
@Test
1911
public void test1000PiecesWithAspectRatio() {
@@ -32,8 +24,8 @@ public void test1000PiecesWithAspectRatio() {
3224
.format("landscape")
3325
.build();
3426

35-
JigsawInfo actual = solver.getCompleteInformation(input);
36-
assertJigsawEqualsWithTolerance(actual, expected);
27+
JigsawInfo actual = PiecingItTogether.getCompleteInformation(input);
28+
assertJigsawInfoEquals(actual, expected);
3729
}
3830

3931
@Disabled("Remove to run test")
@@ -54,8 +46,8 @@ public void testSquarePuzzleWith32Rows() {
5446
.format("square")
5547
.build();
5648

57-
JigsawInfo actual = solver.getCompleteInformation(input);
58-
assertJigsawEqualsWithTolerance(actual, expected);
49+
JigsawInfo actual = PiecingItTogether.getCompleteInformation(input);
50+
assertJigsawInfoEquals(actual, expected);
5951
}
6052

6153
@Disabled("Remove to run test")
@@ -76,8 +68,8 @@ public void testInsideAndAspectRatioOnly() {
7668
.format("square")
7769
.build();
7870

79-
JigsawInfo actual = solver.getCompleteInformation(input);
80-
assertJigsawEqualsWithTolerance(actual, expected);
71+
JigsawInfo actual = PiecingItTogether.getCompleteInformation(input);
72+
assertJigsawInfoEquals(actual, expected);
8173
}
8274

8375
@Disabled("Remove to run test")
@@ -98,8 +90,8 @@ public void testLandscape1500WithRowsAndAspect() {
9890
.format("landscape")
9991
.build();
10092

101-
JigsawInfo actual = solver.getCompleteInformation(input);
102-
assertJigsawEqualsWithTolerance(actual, expected);
93+
JigsawInfo actual = PiecingItTogether.getCompleteInformation(input);
94+
assertJigsawInfoEquals(actual, expected);
10395
}
10496

10597
@Disabled("Remove to run test")
@@ -121,8 +113,8 @@ public void test300PiecesPortraitWithBorder() {
121113
.format("portrait")
122114
.build();
123115

124-
JigsawInfo actual = solver.getCompleteInformation(input);
125-
assertJigsawEqualsWithTolerance(actual, expected);
116+
JigsawInfo actual = PiecingItTogether.getCompleteInformation(input);
117+
assertJigsawInfoEquals(actual, expected);
126118
}
127119

128120
@Disabled("Remove to run test")
@@ -133,7 +125,7 @@ public void testInsufficientData() {
133125
.format("landscape")
134126
.build();
135127

136-
assertThatThrownBy(() -> solver.getCompleteInformation(input))
128+
assertThatThrownBy(() -> PiecingItTogether.getCompleteInformation(input))
137129
.isInstanceOf(IllegalArgumentException.class)
138130
.hasMessageContaining("Insufficient data");
139131
}
@@ -147,7 +139,7 @@ public void testContradictoryData() {
147139
.format("square")
148140
.build();
149141

150-
assertThatThrownBy(() -> solver.getCompleteInformation(input))
142+
assertThatThrownBy(() -> PiecingItTogether.getCompleteInformation(input))
151143
.isInstanceOf(IllegalArgumentException.class)
152144
.hasMessageContaining("Contradictory data");
153145
}
@@ -156,7 +148,7 @@ public void testContradictoryData() {
156148
* Helper to compare two JigsawInfo objects,
157149
* allowing a small tolerance when comparing aspect ratio doubles.
158150
*/
159-
private void assertJigsawEqualsWithTolerance(JigsawInfo actual, JigsawInfo expected) {
151+
private void assertJigsawInfoEquals(JigsawInfo actual, JigsawInfo expected) {
160152
assertThat(actual.getPieces()).isEqualTo(expected.getPieces());
161153
assertThat(actual.getBorder()).isEqualTo(expected.getBorder());
162154
assertThat(actual.getInside()).isEqualTo(expected.getInside());
@@ -167,6 +159,6 @@ private void assertJigsawEqualsWithTolerance(JigsawInfo actual, JigsawInfo expec
167159
Double actualAspect = actual.getAspectRatio().orElseThrow(() -> new AssertionError("Missing aspect ratio in actual result"));
168160
Double expectedAspect = expected.getAspectRatio().orElseThrow(() -> new AssertionError("Missing aspect ratio in expected result"));
169161

170-
assertThat(actualAspect).isCloseTo(expectedAspect, org.assertj.core.api.Assertions.within(epsilon));
162+
assertThat(actualAspect).isCloseTo(expectedAspect, org.assertj.core.api.Assertions.within(DOUBLE_EQUALITY_TOLERANCE));
171163
}
172164
}

0 commit comments

Comments
 (0)