Moved the recalculation of the wind chart points to a separate class, to

be able to write tests.
Wrote tests for the recalculation and refactored the structure of the
algorithm a bit.
This commit is contained in:
Lennart Hensler
2013-01-28 10:44:00 +01:00
parent 2c680f8984
commit a29fff0268
3 changed files with 70 additions and 25 deletions
@@ -0,0 +1,34 @@
package com.sap.sailing.gwt.ui.test;
import static org.junit.Assert.*;
import static org.hamcrest.core.IsEqual.*;
import org.junit.Test;
import org.moxieapps.gwt.highcharts.client.Point;
import com.sap.sailing.gwt.ui.shared.charts.WindChartPointRecalculator;
public class TestWindChartPointRecalculation {
@Test
public void testRecalculaion() {
Point notToBeRecalculated = new Point(10, 100);
assertThat(WindChartPointRecalculator.recalculateDirectionPoint(90.0, 110.0, notToBeRecalculated).getY(), equalTo(notToBeRecalculated.getY()));
assertThat(WindChartPointRecalculator.recalculateDirectionPoint(null, 110.0, notToBeRecalculated).getY(), equalTo(notToBeRecalculated.getY()));
assertThat(WindChartPointRecalculator.recalculateDirectionPoint(90.0, null, notToBeRecalculated).getY(), equalTo(notToBeRecalculated.getY()));
assertThat(WindChartPointRecalculator.recalculateDirectionPoint(100.27458, 110.0, notToBeRecalculated).getY(), equalTo(notToBeRecalculated.getY()));
assertThat(WindChartPointRecalculator.recalculateDirectionPoint(90.0, 100.75639, notToBeRecalculated).getY(), equalTo(notToBeRecalculated.getY()));
//Test for special case, which caused a wrong recalculation, because of the wrong use of Math.abs()
notToBeRecalculated = new Point(10, 179.8537);
assertThat(WindChartPointRecalculator.recalculateDirectionPoint(180.12356, 190.0, notToBeRecalculated).getY(), equalTo(notToBeRecalculated.getY()));
assertThat(WindChartPointRecalculator.recalculateDirectionPoint(170.0, 179.5372, notToBeRecalculated).getY(), equalTo(notToBeRecalculated.getY()));
Point toBeMovedDown = new Point(10, 356);
assertThat(WindChartPointRecalculator.recalculateDirectionPoint(4.0, 23.0, toBeMovedDown).getY(), equalTo(new Point(10, 356.0 - 360.0).getY()));
Point toBeMovedUp = new Point(10, 5);
assertThat(WindChartPointRecalculator.recalculateDirectionPoint(356.0, 359.83364, toBeMovedUp).getY(), equalTo(new Point(10, 5.0 + 360.0).getY()));
}
}
@@ -346,7 +346,7 @@ public class WindChart extends RaceChart implements Component<WindChartSettings>
newDirectionPoint.setName(name);
}
newDirectionPoint = recalculateDirectionPoint(directionMin, directionMax, newDirectionPoint);
newDirectionPoint = WindChartPointRecalculator.recalculateDirectionPoint(directionMin, directionMax, newDirectionPoint);
directionPoints[currentPointIndex] = newDirectionPoint;
double direction = newDirectionPoint.getY().doubleValue();
@@ -393,30 +393,6 @@ public class WindChart extends RaceChart implements Component<WindChartSettings>
timeOfLatestRequestInMillis = newMaxTimepoint;
}
private Point recalculateDirectionPoint(Double yMin, Double yMax, Point directionPoint) {
double y = directionPoint.getY().doubleValue();
boolean recalculated = false;
if (yMax != null && yMin != null && (y < yMin || y > yMax)) {
double deltaMin = Math.abs(yMin - y);
double deltaMax = Math.abs(yMax - y);
double yDown = y - 360;
double deltaMinDown = Math.abs(yMin - yDown);
double yUp = y + 360;
double deltaMaxUp = Math.abs(yMax - yUp);
if (!(deltaMin <= deltaMinDown && deltaMin <= deltaMaxUp)
&& !(deltaMax <= deltaMinDown && deltaMax <= deltaMaxUp)) {
y = deltaMaxUp <= deltaMinDown ? yUp : yDown;
recalculated = true;
}
}
return recalculated ? new Point(directionPoint.getX(), y) : directionPoint;
}
@Override
public boolean hasSettings() {
return true;
@@ -0,0 +1,35 @@
package com.sap.sailing.gwt.ui.shared.charts;
import org.moxieapps.gwt.highcharts.client.Point;
public final class WindChartPointRecalculator {
private WindChartPointRecalculator() { }
public static Point recalculateDirectionPoint(Double yMin, Double yMax, Point directionPoint) {
double y = directionPoint.getY().doubleValue();
if (yMax != null && yMin != null && (y < yMin || y > yMax)) {
double deltaMin = Math.abs(yMin - y);
double deltaMax = Math.abs(yMax - y);
double yDown = y - 360;
double deltaMinDown = Math.abs(yMin - yDown);
double yUp = y + 360;
double deltaMaxUp = Math.abs(yMax - yUp);
if (isRecalculationNeeded(deltaMin, deltaMax, deltaMinDown, deltaMaxUp)) {
y = deltaMaxUp <= deltaMinDown ? yUp : yDown;
return new Point(directionPoint.getX(), y);
}
}
return directionPoint;
}
private static boolean isRecalculationNeeded(double deltaMin, double deltaMax, double deltaMinDown, double deltaMaxUp) {
return (deltaMinDown < deltaMin || deltaMinDown < deltaMax) || (deltaMaxUp < deltaMin || deltaMaxUp < deltaMax);
}
}