Skip to content

Commit c15cc86

Browse files
committed
More strict builder
1 parent 515e245 commit c15cc86

3 files changed

Lines changed: 30 additions & 31 deletions

File tree

hello/src/main/java/com/ak/builder/Anthropomorphic.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ sealed interface Step2 permits AnthropomorphicBuilder {
1818
}
1919

2020
final class AnthropomorphicBuilder implements Anthropomorphic.Step1, Anthropomorphic.Step2, Builder<Anthropomorphic> {
21+
private record AnthropomorphicRecord(int height, int weight) implements Anthropomorphic {
22+
private AnthropomorphicRecord(int height, int weight) {
23+
this.height = Math.clamp(height, 140, 220);
24+
this.weight = Math.clamp(weight, 40, 130);
25+
}
26+
}
27+
2128
private int height;
2229
private int weight;
2330

@@ -38,14 +45,7 @@ public Builder<Anthropomorphic> weight(int weight) {
3845

3946
@Override
4047
public Anthropomorphic build() {
41-
return new Anthropomorphic.AnthropomorphicRecord(height, weight);
42-
}
43-
}
44-
45-
record AnthropomorphicRecord(int height, int weight) implements Anthropomorphic {
46-
public AnthropomorphicRecord(int height, int weight) {
47-
this.height = Math.clamp(height, 140, 220);
48-
this.weight = Math.clamp(weight, 40, 130);
48+
return new AnthropomorphicRecord(height, weight);
4949
}
5050
}
5151
}

hello/src/main/java/com/ak/builder/BloodPressure.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ sealed interface Step2 permits BloodPressureBuilder {
2020
}
2121

2222
final class BloodPressureBuilder implements Step1, Step2, Builder<BloodPressure> {
23+
private record BloodPressureRecord(int systolic, int diastolic) implements BloodPressure {
24+
private BloodPressureRecord(int systolic, int diastolic) {
25+
IntUnaryOperator pressureLimits = bp -> Math.clamp(bp, 10, 220);
26+
int s = pressureLimits.applyAsInt(systolic);
27+
int d = pressureLimits.applyAsInt(diastolic);
28+
this.systolic = Math.clamp(Math.max(s, d), 50, 220);
29+
this.diastolic = Math.clamp(Math.min(s, d), 10, 130);
30+
}
31+
}
2332
private int systolic;
2433
private int diastolic;
2534

@@ -43,16 +52,6 @@ public BloodPressure build() {
4352
return new BloodPressureRecord(systolic, diastolic);
4453
}
4554
}
46-
47-
record BloodPressureRecord(int systolic, int diastolic) implements BloodPressure {
48-
public BloodPressureRecord(int systolic, int diastolic) {
49-
IntUnaryOperator pressureLimits = bp -> Math.clamp(bp, 10, 220);
50-
int s = pressureLimits.applyAsInt(systolic);
51-
int d = pressureLimits.applyAsInt(diastolic);
52-
this.systolic = Math.clamp(Math.max(s, d), 50, 220);
53-
this.diastolic = Math.clamp(Math.min(s, d), 10, 130);
54-
}
55-
}
5655
}
5756

5857

hello/src/main/java/com/ak/builder/Rates.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ sealed interface Step2 permits RatesBuilder {
1818
}
1919

2020
final class RatesBuilder implements Step1, Step2, Builder<Rates> {
21+
private record RatesRecord(int heart, int respiratory) implements Rates {
22+
private RatesRecord(int heart, int respiratory) {
23+
this.heart = Math.clamp(heart, 40, 140);
24+
int rr = Math.clamp(respiratory, 4, 900);
25+
if (rr > 99) {
26+
this.respiratory = Math.clamp(respiratory, 100, 900);
27+
}
28+
else {
29+
this.respiratory = Math.clamp(respiratory, 4, 28);
30+
}
31+
}
32+
}
33+
2134
private int heart;
2235
private int respiratory;
2336

@@ -41,19 +54,6 @@ public Rates build() {
4154
return new RatesRecord(heart, respiratory);
4255
}
4356
}
44-
45-
record RatesRecord(int heart, int respiratory) implements Rates {
46-
public RatesRecord(int heart, int respiratory) {
47-
this.heart = Math.clamp(heart, 40, 140);
48-
int rr = Math.clamp(respiratory, 4, 900);
49-
if (rr > 99) {
50-
this.respiratory = Math.clamp(respiratory, 100, 900);
51-
}
52-
else {
53-
this.respiratory = Math.clamp(respiratory, 4, 28);
54-
}
55-
}
56-
}
5757
}
5858

5959

0 commit comments

Comments
 (0)