Cleanup: use Java17' Records.

Fixes #1526.
This commit is contained in:
Dennis Guse
2023-06-07 23:02:56 +02:00
parent 81b090893b
commit d7022a07a2
34 changed files with 88 additions and 500 deletions
@@ -2,7 +2,7 @@ package de.dennisguse.opentracks.data.models;
import java.util.Objects;
public class AtmosphericPressure {
public record AtmosphericPressure(float value) {
public static AtmosphericPressure ofPA(float value_Pa) {
return new AtmosphericPressure(value_Pa * 100);
@@ -12,12 +12,6 @@ public class AtmosphericPressure {
return new AtmosphericPressure(value_hPa);
}
private final float value;
private AtmosphericPressure(float value) {
this.value = value;
}
public float getPA() {
return value * 100;
}
@@ -25,24 +19,4 @@ public class AtmosphericPressure {
public float getHPA() {
return value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AtmosphericPressure that = (AtmosphericPressure) o;
return Float.compare(that.value, value) == 0;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
return "AtmosphericPressure{" +
"value=" + value +
'}';
}
}
@@ -1,18 +1,8 @@
package de.dennisguse.opentracks.data.models;
public class BatteryLevel {
public record BatteryLevel(int percentage) {
public static BatteryLevel of(int percentage) {
return new BatteryLevel(percentage);
}
private final int percentage;
private BatteryLevel(int percentage) {
this.percentage = percentage;
}
public int getPercentage() {
return percentage;
}
}
@@ -5,7 +5,7 @@ import androidx.annotation.NonNull;
import java.time.Duration;
import java.util.Objects;
public class Cadence {
public record Cadence(float value_rpm) {
public static Cadence of(float value, Duration duration) {
if (duration.isZero()) {
@@ -23,34 +23,7 @@ public class Cadence {
return of(0.0f);
}
private final float value_rpm;
private Cadence(float value) {
this.value_rpm = value;
}
public float getRPM() {
return value_rpm;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Cadence cadence = (Cadence) o;
return Float.compare(cadence.value_rpm, value_rpm) == 0;
}
@Override
public int hashCode() {
return Objects.hash(value_rpm);
}
@NonNull
@Override
public String toString() {
return "Cadence{" +
"value=" + value_rpm + " rpm" +
'}';
}
}
@@ -7,12 +7,11 @@ import java.util.Objects;
import de.dennisguse.opentracks.settings.UnitSystem;
public class Distance {
public record Distance(double distance_m) {
public static Distance of(double distance_m) {
return new Distance(distance_m);
}
public static Distance of(Double distance_m) {
if (distance_m == null) {
return Distance.of(Double.NaN);
@@ -20,11 +19,9 @@ public class Distance {
return Distance.of((double) distance_m);
}
}
public static Distance of(String distance_m) {
return of(Float.parseFloat(distance_m));
}
@Nullable
public static Distance ofOrNull(Double distance_m) {
if (distance_m == null) {
@@ -65,12 +62,6 @@ public class Distance {
};
}
private final double distance_m;
private Distance(double distance_m) {
this.distance_m = distance_m;
}
public Distance plus(@NonNull Distance distance) {
return new Distance(distance_m + distance.distance_m);
}
@@ -142,27 +133,6 @@ public class Distance {
};
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Distance distance = (Distance) o;
return Double.compare(distance.distance_m, distance_m) == 0;
}
@Override
public int hashCode() {
return Objects.hash(distance_m);
}
@NonNull
@Override
public String toString() {
return "Distance{" +
"distance_m=" + distance_m +
'}';
}
// multiplication factors for conversion
private static final double KM_TO_M = 1000.0;
private static final double M_TO_KM = 1 / KM_TO_M;
@@ -4,40 +4,13 @@ import androidx.annotation.NonNull;
import java.util.Objects;
public class HeartRate {
public record HeartRate(float value) {
public static HeartRate of(float value) {
return new HeartRate(value);
}
private final float value;
private HeartRate(float value) {
this.value = value;
}
public float getBPM() {
return value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HeartRate heartRate = (HeartRate) o;
return Float.compare(heartRate.value, value) == 0;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@NonNull
@Override
public String toString() {
return "HeartRate{" +
"value=" + value + " bpm" +
'}';
}
}
@@ -26,7 +26,6 @@ import androidx.annotation.Nullable;
import java.time.Duration;
import java.time.Instant;
import java.util.Objects;
import de.dennisguse.opentracks.stats.TrackStatistics;
@@ -267,31 +266,7 @@ public final class Marker {
return photoUrl != null && !"".equals(photoUrl);
}
public static class Id implements Parcelable {
private final long id;
public Id(long id) {
this.id = id;
}
//TOOD Limit visibility to TrackRecordingService / ContentProvider
public long getId() {
return id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Id id1 = (Id) o;
return id == id1.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
public record Id(long id) implements Parcelable {
@NonNull
@Override
@@ -309,7 +284,7 @@ public final class Marker {
parcel.writeLong(id);
}
public static final Parcelable.Creator<Id> CREATOR = new Parcelable.Creator<>() {
public static final Creator<Id> CREATOR = new Creator<>() {
public Id createFromParcel(Parcel in) {
return new Id(in.readLong());
}
@@ -4,40 +4,13 @@ import androidx.annotation.NonNull;
import java.util.Objects;
public class Power {
public record Power(float value) {
public static Power of(float value) {
return new Power(value);
}
private final float value;
private Power(float value) {
this.value = value;
}
public float getW() {
return value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Power power = (Power) o;
return Float.compare(power.value, value) == 0;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@NonNull
@Override
public String toString() {
return "Power{" +
"value=" + value + " W" +
'}';
}
}
@@ -8,7 +8,7 @@ import java.util.Objects;
import de.dennisguse.opentracks.settings.PreferencesUtils;
import de.dennisguse.opentracks.settings.UnitSystem;
public class Speed {
public record Speed(double speed_mps) {
public static Speed of(Distance distance, Duration duration) {
if (duration.isZero()) {
@@ -47,12 +47,6 @@ public class Speed {
return Speed.of(Math.abs(speed1.speed_mps - speed2.speed_mps));
}
private final double speed_mps;
private Speed(double speed_mps) {
this.speed_mps = speed_mps;
}
public Speed mul(double factor) {
return new Speed(factor * speed_mps);
}
@@ -121,25 +115,4 @@ public class Speed {
case NAUTICAL_IMPERIAL -> toKnots();
};
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Speed speed = (Speed) o;
return Double.compare(speed.speed_mps, speed_mps) == 0;
}
@Override
public int hashCode() {
return Objects.hash(speed_mps);
}
@NonNull
@Override
public String toString() {
return "Speed{" +
"speed_mps=" + speed_mps +
'}';
}
}