From fc01a8736af29778e19bf4fd068633f54ea34ba3 Mon Sep 17 00:00:00 2001 From: JustCris Date: Tue, 7 Jan 2025 15:03:11 +0100 Subject: [PATCH] general fixes Ratio.java and Distance.java --- .../vehiclerental/model/RentalVehicleFuel.java | 12 +----------- .../transit/model/basic/Distance.java | 12 ++---------- .../opentripplanner/transit/model/basic/Ratio.java | 10 +++++++--- 3 files changed, 10 insertions(+), 24 deletions(-) diff --git a/application/src/main/java/org/opentripplanner/service/vehiclerental/model/RentalVehicleFuel.java b/application/src/main/java/org/opentripplanner/service/vehiclerental/model/RentalVehicleFuel.java index 2e24613f49d..3b0349be0b8 100644 --- a/application/src/main/java/org/opentripplanner/service/vehiclerental/model/RentalVehicleFuel.java +++ b/application/src/main/java/org/opentripplanner/service/vehiclerental/model/RentalVehicleFuel.java @@ -11,19 +11,9 @@ */ public class RentalVehicleFuel { - /** - * Current fuel percentage, expressed from 0 to 1. - *

- * May be {@code null}. - */ @Nullable public final Ratio percent; - /** - * Distance that the vehicle can travel with the current charge or fuel. - *

- * May be {@code null}. - */ @Nullable public final Distance range; @@ -34,7 +24,7 @@ public RentalVehicleFuel(@Nullable Ratio fuelPercent, @Nullable Distance range) @Nullable public Double getPercent() { - return this.percent != null ? this.percent.ratio : null; + return this.percent != null ? this.percent.asDouble() : null; } @Nullable diff --git a/application/src/main/java/org/opentripplanner/transit/model/basic/Distance.java b/application/src/main/java/org/opentripplanner/transit/model/basic/Distance.java index fef694ec023..9188699abfb 100644 --- a/application/src/main/java/org/opentripplanner/transit/model/basic/Distance.java +++ b/application/src/main/java/org/opentripplanner/transit/model/basic/Distance.java @@ -9,7 +9,7 @@ public class Distance { private final double meters; /** Returns a Distance object representing the given number of meters */ - public Distance(double distanceInMeters) { + private Distance(double distanceInMeters) { if (distanceInMeters < 0) { throw new IllegalArgumentException("Distance cannot be negative"); } @@ -19,19 +19,11 @@ public Distance(double distanceInMeters) { /** Returns a Distance object representing the given number of meters */ public static Distance ofMeters(double value) throws IllegalArgumentException { - if (value < 0) { - throw new IllegalArgumentException("Distance cannot be negative"); - } - return new Distance(value); } /** Returns a Distance object representing the given number of kilometers */ public static Distance ofKilometers(double value) { - if (value < 0) { - throw new IllegalArgumentException("Distance cannot be negative"); - } - return new Distance(value * METERS_PER_KM); } @@ -51,7 +43,7 @@ public boolean equals(Object other) { @Override public int hashCode() { - return Objects.hash(meters, "Distance"); + return Objects.hash(meters); } @Override diff --git a/application/src/main/java/org/opentripplanner/transit/model/basic/Ratio.java b/application/src/main/java/org/opentripplanner/transit/model/basic/Ratio.java index 0c980be6eee..95c4260b078 100644 --- a/application/src/main/java/org/opentripplanner/transit/model/basic/Ratio.java +++ b/application/src/main/java/org/opentripplanner/transit/model/basic/Ratio.java @@ -4,9 +4,9 @@ public class Ratio { - public final Double ratio; + private final Double ratio; - public Ratio(Double ratio) throws IllegalArgumentException { + public Ratio(Double ratio) { if (ratio < 0d || ratio > 1d) { throw new IllegalArgumentException("Ratio must be in range [0,1]"); } @@ -25,11 +25,15 @@ public boolean equals(Object other) { @Override public int hashCode() { - return Objects.hash(this.ratio, "Ratio"); + return Objects.hash(this.ratio); } @Override public String toString() { return this.ratio.toString(); } + + public Double asDouble() { + return ratio; + } }