Skip to content

Commit 7d3aaa7

Browse files
committed
Refactor JigsawInfo builder to use Optionals and fix piecing-it-together style issues
1 parent 965396c commit 7d3aaa7

File tree

5 files changed

+86
-65
lines changed

5 files changed

+86
-65
lines changed

exercises/practice/piecing-it-together/.meta/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
".meta/src/reference/java/PiecingItTogether.java"
1414
],
1515
"editor": [
16-
".meta/src/reference/java/JigsawInfo.java"
16+
"src/main/java/JigsawInfo.java"
1717
]
1818
},
1919
"blurb": "Fill in missing jigsaw puzzle details from partial data",

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

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,56 +18,56 @@ public class JigsawInfo {
1818
private final Optional<String> format;
1919

2020
private JigsawInfo(Builder builder) {
21-
this.pieces = builder.pieces != null ? OptionalInt.of(builder.pieces) : OptionalInt.empty();
22-
this.border = builder.border != null ? OptionalInt.of(builder.border) : OptionalInt.empty();
23-
this.inside = builder.inside != null ? OptionalInt.of(builder.inside) : OptionalInt.empty();
24-
this.rows = builder.rows != null ? OptionalInt.of(builder.rows) : OptionalInt.empty();
25-
this.columns = builder.columns != null ? OptionalInt.of(builder.columns) : OptionalInt.empty();
26-
this.aspectRatio = builder.aspectRatio != null ? OptionalDouble.of(builder.aspectRatio) : OptionalDouble.empty();
27-
this.format = Optional.ofNullable(builder.format);
21+
this.pieces = builder.pieces;
22+
this.border = builder.border;
23+
this.inside = builder.inside;
24+
this.rows = builder.rows;
25+
this.columns = builder.columns;
26+
this.aspectRatio = builder.aspectRatio;
27+
this.format = builder.format;
2828
}
2929

3030
public static class Builder {
31-
private Integer pieces;
32-
private Integer border;
33-
private Integer inside;
34-
private Integer rows;
35-
private Integer columns;
36-
private Double aspectRatio;
37-
private String format;
38-
39-
public Builder pieces(Integer pieces) {
40-
this.pieces = pieces;
31+
private OptionalInt pieces = OptionalInt.empty();
32+
private OptionalInt border = OptionalInt.empty();
33+
private OptionalInt inside = OptionalInt.empty();
34+
private OptionalInt rows = OptionalInt.empty();
35+
private OptionalInt columns = OptionalInt.empty();
36+
private OptionalDouble aspectRatio = OptionalDouble.empty();
37+
private Optional<String> format = Optional.empty();
38+
39+
public Builder pieces(int pieces) {
40+
this.pieces = OptionalInt.of(pieces);
4141
return this;
4242
}
4343

44-
public Builder border(Integer border) {
45-
this.border = border;
44+
public Builder border(int border) {
45+
this.border = OptionalInt.of(border);
4646
return this;
4747
}
4848

49-
public Builder inside(Integer inside) {
50-
this.inside = inside;
49+
public Builder inside(int inside) {
50+
this.inside = OptionalInt.of(inside);
5151
return this;
5252
}
5353

54-
public Builder rows(Integer rows) {
55-
this.rows = rows;
54+
public Builder rows(int rows) {
55+
this.rows = OptionalInt.of(rows);
5656
return this;
5757
}
5858

59-
public Builder columns(Integer columns) {
60-
this.columns = columns;
59+
public Builder columns(int columns) {
60+
this.columns = OptionalInt.of(columns);
6161
return this;
6262
}
6363

64-
public Builder aspectRatio(Double aspectRatio) {
65-
this.aspectRatio = aspectRatio;
64+
public Builder aspectRatio(double aspectRatio) {
65+
this.aspectRatio = OptionalDouble.of(aspectRatio);
6666
return this;
6767
}
6868

6969
public Builder format(String format) {
70-
this.format = format;
70+
this.format = Optional.of(format);
7171
return this;
7272
}
7373

@@ -106,9 +106,18 @@ public Optional<String> getFormat() {
106106

107107
@Override
108108
public boolean equals(Object o) {
109-
if (o == null || getClass() != o.getClass()) return false;
109+
if (o == null || getClass() != o.getClass()) {
110+
return false;
111+
}
112+
110113
JigsawInfo that = (JigsawInfo) o;
111-
return Objects.equals(pieces, that.pieces) && Objects.equals(border, that.border) && Objects.equals(inside, that.inside) && Objects.equals(rows, that.rows) && Objects.equals(columns, that.columns) && Objects.equals(aspectRatio, that.aspectRatio) && Objects.equals(format, that.format);
114+
return Objects.equals(pieces, that.pieces)
115+
&& Objects.equals(border, that.border)
116+
&& Objects.equals(inside, that.inside)
117+
&& Objects.equals(rows, that.rows)
118+
&& Objects.equals(columns, that.columns)
119+
&& Objects.equals(aspectRatio, that.aspectRatio)
120+
&& Objects.equals(format, that.format);
112121
}
113122

114123
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,4 @@ private static boolean valuesMatch(OptionalDouble a, OptionalDouble b) {
132132

133133
return true;
134134
}
135-
}
135+
}

exercises/practice/piecing-it-together/src/main/java/JigsawInfo.java

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,56 +18,56 @@ public class JigsawInfo {
1818
private final Optional<String> format;
1919

2020
private JigsawInfo(Builder builder) {
21-
this.pieces = builder.pieces != null ? OptionalInt.of(builder.pieces) : OptionalInt.empty();
22-
this.border = builder.border != null ? OptionalInt.of(builder.border) : OptionalInt.empty();
23-
this.inside = builder.inside != null ? OptionalInt.of(builder.inside) : OptionalInt.empty();
24-
this.rows = builder.rows != null ? OptionalInt.of(builder.rows) : OptionalInt.empty();
25-
this.columns = builder.columns != null ? OptionalInt.of(builder.columns) : OptionalInt.empty();
26-
this.aspectRatio = builder.aspectRatio != null ? OptionalDouble.of(builder.aspectRatio) : OptionalDouble.empty();
27-
this.format = Optional.ofNullable(builder.format);
21+
this.pieces = builder.pieces;
22+
this.border = builder.border;
23+
this.inside = builder.inside;
24+
this.rows = builder.rows;
25+
this.columns = builder.columns;
26+
this.aspectRatio = builder.aspectRatio;
27+
this.format = builder.format;
2828
}
2929

3030
public static class Builder {
31-
private Integer pieces;
32-
private Integer border;
33-
private Integer inside;
34-
private Integer rows;
35-
private Integer columns;
36-
private Double aspectRatio;
37-
private String format;
38-
39-
public Builder pieces(Integer pieces) {
40-
this.pieces = pieces;
31+
private OptionalInt pieces = OptionalInt.empty();
32+
private OptionalInt border = OptionalInt.empty();
33+
private OptionalInt inside = OptionalInt.empty();
34+
private OptionalInt rows = OptionalInt.empty();
35+
private OptionalInt columns = OptionalInt.empty();
36+
private OptionalDouble aspectRatio = OptionalDouble.empty();
37+
private Optional<String> format = Optional.empty();
38+
39+
public Builder pieces(int pieces) {
40+
this.pieces = OptionalInt.of(pieces);
4141
return this;
4242
}
4343

44-
public Builder border(Integer border) {
45-
this.border = border;
44+
public Builder border(int border) {
45+
this.border = OptionalInt.of(border);
4646
return this;
4747
}
4848

49-
public Builder inside(Integer inside) {
50-
this.inside = inside;
49+
public Builder inside(int inside) {
50+
this.inside = OptionalInt.of(inside);
5151
return this;
5252
}
5353

54-
public Builder rows(Integer rows) {
55-
this.rows = rows;
54+
public Builder rows(int rows) {
55+
this.rows = OptionalInt.of(rows);
5656
return this;
5757
}
5858

59-
public Builder columns(Integer columns) {
60-
this.columns = columns;
59+
public Builder columns(int columns) {
60+
this.columns = OptionalInt.of(columns);
6161
return this;
6262
}
6363

64-
public Builder aspectRatio(Double aspectRatio) {
65-
this.aspectRatio = aspectRatio;
64+
public Builder aspectRatio(double aspectRatio) {
65+
this.aspectRatio = OptionalDouble.of(aspectRatio);
6666
return this;
6767
}
6868

6969
public Builder format(String format) {
70-
this.format = format;
70+
this.format = Optional.of(format);
7171
return this;
7272
}
7373

@@ -106,9 +106,18 @@ public Optional<String> getFormat() {
106106

107107
@Override
108108
public boolean equals(Object o) {
109-
if (o == null || getClass() != o.getClass()) return false;
109+
if (o == null || getClass() != o.getClass()) {
110+
return false;
111+
}
112+
110113
JigsawInfo that = (JigsawInfo) o;
111-
return Objects.equals(pieces, that.pieces) && Objects.equals(border, that.border) && Objects.equals(inside, that.inside) && Objects.equals(rows, that.rows) && Objects.equals(columns, that.columns) && Objects.equals(aspectRatio, that.aspectRatio) && Objects.equals(format, that.format);
114+
return Objects.equals(pieces, that.pieces)
115+
&& Objects.equals(border, that.border)
116+
&& Objects.equals(inside, that.inside)
117+
&& Objects.equals(rows, that.rows)
118+
&& Objects.equals(columns, that.columns)
119+
&& Objects.equals(aspectRatio, that.aspectRatio)
120+
&& Objects.equals(format, that.format);
112121
}
113122

114123
@Override

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,12 @@ private void assertJigsawInfoEquals(JigsawInfo actual, JigsawInfo expected) {
156156
assertThat(actual.getColumns()).isEqualTo(expected.getColumns());
157157
assertThat(actual.getFormat()).isEqualTo(expected.getFormat());
158158

159-
Double actualAspect = actual.getAspectRatio().orElseThrow(() -> new AssertionError("Missing aspect ratio in actual result"));
160-
Double expectedAspect = expected.getAspectRatio().orElseThrow(() -> new AssertionError("Missing aspect ratio in expected result"));
159+
Double actualAspect = actual.getAspectRatio()
160+
.orElseThrow(() -> new AssertionError("Missing aspect ratio in actual result"));
161+
Double expectedAspect = expected.getAspectRatio()
162+
.orElseThrow(() -> new AssertionError("Missing aspect ratio in expected result"));
161163

162-
assertThat(actualAspect).isCloseTo(expectedAspect, org.assertj.core.api.Assertions.within(DOUBLE_EQUALITY_TOLERANCE));
164+
assertThat(actualAspect).isCloseTo(expectedAspect,
165+
org.assertj.core.api.Assertions.within(DOUBLE_EQUALITY_TOLERANCE));
163166
}
164167
}

0 commit comments

Comments
 (0)