From a483359adce946777d5c0b0ddce64fee3c2f6168 Mon Sep 17 00:00:00 2001 From: Alexander Kobelev Date: Sat, 25 Jan 2025 14:17:45 +0300 Subject: [PATCH] https://github.com/kousen/dataorientedprogramming remove cycles --- .../java/com/ak/builder/BloodPressure.java | 18 +++++++-------- .../src/main/java/com/ak/builder/Patient.java | 19 ++++++++------- hello/src/main/java/com/ak/builder/Rates.java | 23 +++++++++---------- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/hello/src/main/java/com/ak/builder/BloodPressure.java b/hello/src/main/java/com/ak/builder/BloodPressure.java index e2e37db..3a2460e 100644 --- a/hello/src/main/java/com/ak/builder/BloodPressure.java +++ b/hello/src/main/java/com/ak/builder/BloodPressure.java @@ -2,7 +2,7 @@ import java.util.function.IntUnaryOperator; -public sealed interface BloodPressure permits BloodPressureRecord { +public sealed interface BloodPressure { int systolic(); int diastolic(); @@ -43,15 +43,15 @@ public BloodPressure build() { return new BloodPressureRecord(systolic, diastolic); } } -} -record BloodPressureRecord(int systolic, int diastolic) implements BloodPressure { - BloodPressureRecord(int systolic, int diastolic) { - IntUnaryOperator pressureLimits = bp -> Math.clamp(bp, 10, 220); - int s = pressureLimits.applyAsInt(systolic); - int d = pressureLimits.applyAsInt(diastolic); - this.systolic = Math.clamp(Math.max(s, d), 50, 220); - this.diastolic = Math.clamp(Math.min(s, d), 10, 130); + record BloodPressureRecord(int systolic, int diastolic) implements BloodPressure { + public BloodPressureRecord(int systolic, int diastolic) { + IntUnaryOperator pressureLimits = bp -> Math.clamp(bp, 10, 220); + int s = pressureLimits.applyAsInt(systolic); + int d = pressureLimits.applyAsInt(diastolic); + this.systolic = Math.clamp(Math.max(s, d), 50, 220); + this.diastolic = Math.clamp(Math.min(s, d), 10, 130); + } } } diff --git a/hello/src/main/java/com/ak/builder/Patient.java b/hello/src/main/java/com/ak/builder/Patient.java index 1afb85f..848b16b 100644 --- a/hello/src/main/java/com/ak/builder/Patient.java +++ b/hello/src/main/java/com/ak/builder/Patient.java @@ -5,7 +5,7 @@ import java.util.Objects; import java.util.function.Function; -public sealed interface Patient permits PatientRecord { +public sealed interface Patient { int age(); Anthropomorphic anthropomorphic(); @@ -74,16 +74,15 @@ public Patient build() { ); } } -} -record PatientRecord(int age, Anthropomorphic anthropomorphic, BloodPressure bloodPressure, - Rates rates) implements Patient { - PatientRecord(int age, Anthropomorphic anthropomorphic, BloodPressure bloodPressure, Rates rates) { - this.age = Math.clamp(age, 12, 100); - this.anthropomorphic = anthropomorphic; - this.bloodPressure = bloodPressure; - this.rates = rates; + record PatientRecord(int age, Anthropomorphic anthropomorphic, BloodPressure bloodPressure, + Rates rates) implements Patient { + public PatientRecord(int age, Anthropomorphic anthropomorphic, BloodPressure bloodPressure, Rates rates) { + this.age = Math.clamp(age, 12, 100); + this.anthropomorphic = anthropomorphic; + this.bloodPressure = bloodPressure; + this.rates = rates; + } } } - diff --git a/hello/src/main/java/com/ak/builder/Rates.java b/hello/src/main/java/com/ak/builder/Rates.java index 43f2a21..383f8df 100644 --- a/hello/src/main/java/com/ak/builder/Rates.java +++ b/hello/src/main/java/com/ak/builder/Rates.java @@ -1,6 +1,6 @@ package com.ak.builder; -public sealed interface Rates permits RatesRecord { +public sealed interface Rates { int heart(); int respiratory(); @@ -41,20 +41,19 @@ public Rates build() { return new RatesRecord(heart, respiratory); } } -} -record RatesRecord(int heart, int respiratory) implements Rates { - RatesRecord(int heart, int respiratory) { - this.heart = Math.clamp(heart, 40, 140); - int rr = Math.clamp(respiratory, 4, 900); - if (rr > 99) { - this.respiratory = Math.clamp(respiratory, 100, 900); - } - else { - this.respiratory = Math.clamp(respiratory, 4, 28); + record RatesRecord(int heart, int respiratory) implements Rates { + public RatesRecord(int heart, int respiratory) { + this.heart = Math.clamp(heart, 40, 140); + int rr = Math.clamp(respiratory, 4, 900); + if (rr > 99) { + this.respiratory = Math.clamp(respiratory, 100, 900); + } + else { + this.respiratory = Math.clamp(respiratory, 4, 28); + } } } } -