use more precise SecondsDurationImpl for Duration.NULL; use switch/case; renamed variables to be more precise

Change-Id: I9928d3bcf3f0b274be30da08d8a5eaff23ab2a95
This commit is contained in:
Axel Uhl committed 2019-09-10 16:26:28 +02:00
1 parent 9996563672
commit f8625b5dee
7 files changed
+29 -22

No files matched your search

@@ -5,7 +5,7 @@ import com.sap.sse.common.Distance;
import com.sap.sse.common.Duration;
import com.sap.sse.common.Speed;
import com.sap.sse.common.TimePoint;
import com.sap.sse.common.impl.MillisecondsDurationImpl;
import com.sap.sse.common.impl.SecondsDurationImpl;
public abstract class AbstractSpeedImpl implements Speed {
@@ -28,7 +28,7 @@ public abstract class AbstractSpeedImpl implements Speed {
@Override
public Duration getDuration(Distance distance) {
return distance == null ? null : new MillisecondsDurationImpl((long) (1000 * distance.getMeters() / getMetersPerSecond()));
return distance == null ? null : new SecondsDurationImpl(distance.getMeters() / getMetersPerSecond());
}
@Override
@@ -13,6 +13,7 @@ import com.sap.sse.common.Distance;
import com.sap.sse.common.Duration;
import com.sap.sse.common.Speed;
import com.sap.sse.common.impl.DegreeBearingImpl;
import com.sap.sse.common.impl.SecondsDurationImpl;
//TODO Finish this comment.
/**
@@ -220,9 +221,10 @@ public class ORCCertificateImpl implements ORCCertificate {
final Map<Speed, Speed> result = new HashMap<>();
for (final Speed tws : ALLOWANCES_TRUE_WIND_SPEEDS) {
// gets the Allowance for Beat&Run at the given TWS divided by 2
Duration allowance = beatVMGPredictionPerTrueWindSpeed.get(tws).getDuration(NAUTICAL_MILE)
final Duration allowancePerNauticalMile = beatVMGPredictionPerTrueWindSpeed.get(tws).getDuration(NAUTICAL_MILE)
.plus(runVMGPredictionPerTrueWindSpeed.get(tws).getDuration(NAUTICAL_MILE)).divide(2);
result.put(tws, NAUTICAL_MILE.inTime(allowance));
final Duration allowancePerNauticalMileRoundedToOneDecimal = new SecondsDurationImpl(((double) Math.round(allowancePerNauticalMile.asSeconds()*10.0))/10.0);
result.put(tws, NAUTICAL_MILE.inTime(allowancePerNauticalMileRoundedToOneDecimal));
}
return result;
}
@@ -236,20 +238,21 @@ public class ORCCertificateImpl implements ORCCertificate {
public Map<Speed, Speed> getLongDistanceSpeedPredictions() {
final Map<Speed, Speed> result = new HashMap<>();
for (final Entry<Speed, Map<Bearing, Double>> twsEntry : perCentOfAllowancesForLongDistancePC.entrySet()) {
Duration allowance = Duration.NULL;
Duration allowancePerNauticalMile = Duration.NULL;
for (Entry<Bearing, Double> twaEntry : twsEntry.getValue().entrySet()) {
if (twaEntry.getKey().equals(UPWIND_TWA)) {
allowance = allowance.plus(beatVMGPredictionPerTrueWindSpeed.get(twsEntry.getKey())
allowancePerNauticalMile = allowancePerNauticalMile.plus(beatVMGPredictionPerTrueWindSpeed.get(twsEntry.getKey())
.getDuration(NAUTICAL_MILE).times(twaEntry.getValue()));
} else if (twaEntry.getKey().equals(DOWNWIND_TWA)) {
allowance = allowance.plus(runVMGPredictionPerTrueWindSpeed.get(twsEntry.getKey())
allowancePerNauticalMile = allowancePerNauticalMile.plus(runVMGPredictionPerTrueWindSpeed.get(twsEntry.getKey())
.getDuration(NAUTICAL_MILE).times(twaEntry.getValue()));
} else {
allowance = allowance.plus(velocityPredictionPerTrueWindSpeedAndAngle.get(twsEntry.getKey())
allowancePerNauticalMile = allowancePerNauticalMile.plus(velocityPredictionPerTrueWindSpeedAndAngle.get(twsEntry.getKey())
.get(twaEntry.getKey()).getDuration(NAUTICAL_MILE).times(twaEntry.getValue()));
}
}
result.put(twsEntry.getKey(), NAUTICAL_MILE.inTime(allowance));
final Duration allowancePerNauticalMileRoundedToOneDecimal = new SecondsDurationImpl(((double) Math.round(allowancePerNauticalMile.asSeconds()*10.0))/10.0);
result.put(twsEntry.getKey(), NAUTICAL_MILE.inTime(allowancePerNauticalMileRoundedToOneDecimal));
}
return result;
}
@@ -34,7 +34,7 @@ public class ORCPerformanceCurveLegImpl implements ORCPerformanceCurveLeg {
@Override
public String toString() {
return "[length=" + length.getNauticalMiles() + "NM, twa=" + twa.getDegrees() + "°]";
return "[length=" + length.getNauticalMiles() + "NM, "+(twa==null?type.name():"TWA=" + twa.getDegrees() + "°")+"]";
}
@Override
@@ -190,7 +190,7 @@ public class TestORCPerformanceCurve {
// Test for corner case and if the algorithm reacts to the boundaries of 6 and 20 kts.
assertEquals( 6.0 , performanceCurveMoana.getImpliedWind(Duration.ONE_HOUR.times(24)).getKnots(), accuracy);
assertEquals(20.0 , performanceCurveMoana.getImpliedWind(Duration.ONE_HOUR.divide(24)).getKnots(), accuracy);
assertEquals(performanceCurveMilan.getAllowancePerCourse(new KnotSpeedImpl(12.80881)).asSeconds(), Duration.ONE_HOUR.asSeconds(), accuracy);
assertEquals(1.0, performanceCurveMilan.getAllowancePerCourse(new KnotSpeedImpl(12.80881)).asHours(), accuracy);
// scratch sheets and implied wind as calculated by Altura for course1 and 1:00:00 / 1:30:00 time sailed, respectively:
// 6kts 8kts 10kts 12kts 14kts 16kts 20kts implied wind Altura ORC Scorer ORC PCS Test SAP
// Milan: 675.2 539.5 473.1 437.6 412.7 388.8 350.8 12.8091135 12.80881 12.80881 12.809089
@@ -19,7 +19,6 @@ import java.util.regex.Pattern;
import org.apache.commons.io.ByteOrderMark;
import org.apache.commons.io.input.BOMInputStream;
import com.sap.sse.common.impl.DegreeBearingImpl;
import com.sap.sailing.domain.common.impl.MeterDistance;
import com.sap.sailing.domain.common.orc.ORCCertificate;
import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl;
@@ -28,6 +27,7 @@ import com.sap.sse.common.Bearing;
import com.sap.sse.common.Distance;
import com.sap.sse.common.Duration;
import com.sap.sse.common.Speed;
import com.sap.sse.common.impl.DegreeBearingImpl;
import com.sap.sse.common.impl.SecondsDurationImpl;
/**
@@ -25,7 +25,6 @@ import com.sap.sailing.domain.common.impl.KnotSpeedImpl;
import com.sap.sailing.domain.common.orc.ORCCertificate;
import com.sap.sailing.domain.common.orc.ORCPerformanceCurveCourse;
import com.sap.sailing.domain.common.orc.ORCPerformanceCurveLeg;
import com.sap.sailing.domain.common.orc.ORCPerformanceCurveLegTypes;
import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl;
import com.sap.sailing.domain.orc.ORCPerformanceCurve;
import com.sap.sse.common.Bearing;
@@ -138,8 +137,6 @@ public class ORCPerformanceCurveImpl implements Serializable, ORCPerformanceCurv
* order is guaranteed to deliver the true wind speed keys in the order of ascending wind speeds.
*/
public LinkedHashMap<Speed, Duration> createAllowancesPerCourse(ORCCertificate certificate) throws FunctionEvaluationException {
final LinkedHashMap<Speed, Duration> result = new LinkedHashMap<>();
final Map<ORCPerformanceCurveLeg, Map<Speed, Duration>> allowancesPerLeg = new HashMap<>();
for (final ORCPerformanceCurveLeg leg : course.getLegs()) {
@@ -169,7 +166,8 @@ public class ORCPerformanceCurveImpl implements Serializable, ORCPerformanceCurv
Map<Speed, Duration> runAllowancePerTrueWindSpeed = certificate.getRunAllowances();
final Map<Speed, Duration> result = new HashMap<>();
if(leg.getType().equals(ORCPerformanceCurveLegTypes.TWA)) {
switch (leg.getType()) {
case TWA:
for (final Speed tws : ORCCertificateImpl.ALLOWANCES_TRUE_WIND_SPEEDS) {
// Case switching on TWA (0. TWA == 0; 1. TWA < Beat; 2. Beat < TWA < Gybe; 3. Gybe < TWA; 4. TWA == 180)
if (leg.getTwa().compareTo(beatAngles.get(tws)) <= 0) {
@@ -186,22 +184,27 @@ public class ORCPerformanceCurveImpl implements Serializable, ORCPerformanceCurv
leg.getTwa()).getDuration(leg.getLength()));
}
}
} else if (leg.getType().equals(ORCPerformanceCurveLegTypes.CIRCULAR_RANDOM)) {
break;
case CIRCULAR_RANDOM:
for (final Speed tws : ORCCertificateImpl.ALLOWANCES_TRUE_WIND_SPEEDS) {
result.put(tws, certificate.getCircularRandomSpeedPredictions().get(tws).getDuration(leg.getLength()));
}
} else if (leg.getType().equals(ORCPerformanceCurveLegTypes.LONG_DISTANCE)) {
break;
case LONG_DISTANCE:
for (final Speed tws : ORCCertificateImpl.ALLOWANCES_TRUE_WIND_SPEEDS) {
result.put(tws, certificate.getLongDistanceSpeedPredictions().get(tws).getDuration(leg.getLength()));
}
} else if (leg.getType().equals(ORCPerformanceCurveLegTypes.WINDWARD_LEEWARD)) {
break;
case WINDWARD_LEEWARD:
for (final Speed tws : ORCCertificateImpl.ALLOWANCES_TRUE_WIND_SPEEDS) {
result.put(tws, certificate.getWindwardLeewardSpeedPrediction().get(tws).getDuration(leg.getLength()));
}
} else if (leg.getType().equals(ORCPerformanceCurveLegTypes.NON_SPINNAKER)) {
break;
case NON_SPINNAKER:
for (final Speed tws : ORCCertificateImpl.ALLOWANCES_TRUE_WIND_SPEEDS) {
result.put(tws, certificate.getNonSpinnakerSpeedPredictions().get(tws).getDuration(leg.getLength()));
}
break;
}
return result;
}
@@ -3,6 +3,7 @@ package com.sap.sse.common;
import java.io.Serializable;
import com.sap.sse.common.impl.MillisecondsDurationImpl;
import com.sap.sse.common.impl.SecondsDurationImpl;
/**
* A time duration that can be converted to various time units and that interoperates with {@link TimePoint}.
@@ -12,9 +13,9 @@ import com.sap.sse.common.impl.MillisecondsDurationImpl;
*/
public interface Duration extends Serializable, Comparable<Duration> {
static final Duration NULL = new MillisecondsDurationImpl(0);
static final Duration NULL = new SecondsDurationImpl(0);
static final Duration ONE_MILLISECOND = new MillisecondsDurationImpl(1);
static final Duration ONE_SECOND = new MillisecondsDurationImpl(1000);
static final Duration ONE_SECOND = new SecondsDurationImpl(1);
static final Duration ONE_MINUTE = ONE_SECOND.times(60);
static final Duration ONE_HOUR = ONE_MINUTE.times(60);
static final Duration ONE_DAY = ONE_HOUR.times(24);