mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-04 03:13:49 +00:00
provided a memory-conserving flattened, compact representation of a GPSFixMoving, use it in track
This commit is contained in:
1 parent
c85da2f3fd
commit
3a01b3e769
4 files changed
+150
-6
No files matched your search
+19
-6
@@ -6,7 +6,6 @@ import java.util.Date;
|
||||
import com.sap.sailing.domain.base.TimePoint;
|
||||
|
||||
public abstract class AbstractTimePoint implements TimePoint {
|
||||
private Date date;
|
||||
private static SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
|
||||
|
||||
@Override
|
||||
@@ -15,14 +14,14 @@ public abstract class AbstractTimePoint implements TimePoint {
|
||||
return milliDiff<0 ? -1 : milliDiff == 0 ? 0 : 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Date asDate() {
|
||||
if (date == null) {
|
||||
date = new Date(asMillis());
|
||||
Date result = getDateFromCache();
|
||||
if (result == null) {
|
||||
result = new Date(asMillis());
|
||||
cacheDate(result);
|
||||
}
|
||||
return date;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -39,4 +38,18 @@ public abstract class AbstractTimePoint implements TimePoint {
|
||||
public String toString() {
|
||||
return dateFormatter.format(asDate());
|
||||
}
|
||||
|
||||
/**
|
||||
* A subclass may implement a cache for the {@link Date} representation by overriding this method as well
|
||||
* as {@link #cacheDate(Date)}.
|
||||
*/
|
||||
protected Date getDateFromCache() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A subclass may implement a cache for the {@link Date} representation by overriding this method as well
|
||||
* as {@link #getDate()}.
|
||||
*/
|
||||
protected void cacheDate(Date date) {}
|
||||
}
|
||||
+9
@@ -5,6 +5,7 @@ import java.util.Date;
|
||||
|
||||
public class MillisecondsTimePoint extends AbstractTimePoint {
|
||||
private final long millis;
|
||||
private Date date;
|
||||
|
||||
public static MillisecondsTimePoint now() {
|
||||
return new MillisecondsTimePoint(System.currentTimeMillis());
|
||||
@@ -20,6 +21,14 @@ public class MillisecondsTimePoint extends AbstractTimePoint {
|
||||
this.millis = date.getTime();
|
||||
}
|
||||
|
||||
protected Date getDateFromCache() {
|
||||
return date;
|
||||
}
|
||||
|
||||
protected void cacheDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long asMillis() {
|
||||
return millis;
|
||||
|
||||
Executable
+113
@@ -0,0 +1,113 @@
|
||||
package com.sap.sailing.domain.tracking.impl;
|
||||
|
||||
import com.sap.sailing.domain.base.Bearing;
|
||||
import com.sap.sailing.domain.base.Position;
|
||||
import com.sap.sailing.domain.base.SpeedWithBearing;
|
||||
import com.sap.sailing.domain.base.TimePoint;
|
||||
import com.sap.sailing.domain.base.impl.AbstractBearing;
|
||||
import com.sap.sailing.domain.base.impl.AbstractPosition;
|
||||
import com.sap.sailing.domain.base.impl.AbstractSpeedImpl;
|
||||
import com.sap.sailing.domain.base.impl.AbstractTimePoint;
|
||||
import com.sap.sailing.domain.tracking.GPSFixMoving;
|
||||
|
||||
/**
|
||||
* A memory-conserving representation of a {@link GPSFixMoving} object that produces the fine-grained
|
||||
* objects for {@link Position}, {@link SpeedWithBearing}, {@link Bearing} and {@link TimePoint} dynamically
|
||||
* as thin wrappers around this object which holds all elementary attributes required. This saves several
|
||||
* object references and object headers.
|
||||
*
|
||||
* @author Axel Uhl (d043530)
|
||||
*
|
||||
*/
|
||||
public class CompactGPSFixMovingImpl implements GPSFixMoving {
|
||||
private final double latDeg;
|
||||
private final double lngDeg;
|
||||
private final long timePointAsMillis;
|
||||
private final double knotSpeed;
|
||||
private final double degBearing;
|
||||
|
||||
private class CompactSpeedWithBearing extends AbstractSpeedImpl implements SpeedWithBearing {
|
||||
@Override
|
||||
public double getKnots() {
|
||||
return knotSpeed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bearing getBearing() {
|
||||
return new CompactBearing();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position travelTo(Position pos, TimePoint from, TimePoint to) {
|
||||
return pos.translateGreatCircle(getBearing(), this.travel(from, to));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString()+" to "+getBearing().getDegrees()+"°";
|
||||
}
|
||||
}
|
||||
|
||||
private class CompactBearing extends AbstractBearing {
|
||||
@Override
|
||||
public double getDegrees() {
|
||||
return degBearing;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getRadians() {
|
||||
return degBearing / 180. * Math.PI;
|
||||
}
|
||||
}
|
||||
|
||||
private class CompactPosition extends AbstractPosition {
|
||||
@Override
|
||||
public double getLatDeg() {
|
||||
return latDeg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getLngDeg() {
|
||||
return lngDeg;
|
||||
}
|
||||
}
|
||||
|
||||
private class CompactTimePoint extends AbstractTimePoint implements TimePoint {
|
||||
@Override
|
||||
public long asMillis() {
|
||||
return timePointAsMillis;
|
||||
}
|
||||
}
|
||||
|
||||
public CompactGPSFixMovingImpl(Position position, TimePoint timePoint, SpeedWithBearing speed) {
|
||||
latDeg = position.getLatDeg();
|
||||
lngDeg = position.getLngDeg();
|
||||
timePointAsMillis = timePoint.asMillis();
|
||||
knotSpeed = speed.getKnots();
|
||||
degBearing = speed.getBearing().getDegrees();
|
||||
}
|
||||
|
||||
public CompactGPSFixMovingImpl(GPSFixMoving gpsFixMoving) {
|
||||
this(gpsFixMoving.getPosition(), gpsFixMoving.getTimePoint(), gpsFixMoving.getSpeed());
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpeedWithBearing getSpeed() {
|
||||
return new CompactSpeedWithBearing();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getTimePoint() + ": " + getPosition() + " with " + getSpeed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
return new CompactPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimePoint getTimePoint() {
|
||||
return new CompactTimePoint();
|
||||
}
|
||||
}
|
||||
+9
@@ -20,6 +20,15 @@ public class DynamicGPSFixMovingTrackImpl<ItemType> extends DynamicTrackImpl<Ite
|
||||
super(trackedItem, millisecondsOverWhichToAverage);
|
||||
}
|
||||
|
||||
/**
|
||||
* This redefinition packs the <code>gpsFix</code> into a more compact representation that conserves
|
||||
* memory compared to the original, "naive" implementation. It gets along with a single object.
|
||||
*/
|
||||
@Override
|
||||
public void addGPSFix(GPSFixMoving gpsFix) {
|
||||
super.addGPSFix(new CompactGPSFixMovingImpl(gpsFix));
|
||||
}
|
||||
|
||||
/**
|
||||
* Here we know for sure that the GPS fixes are {@link GPSFixMoving} instances,
|
||||
* so we can use their {@link GPSFixMoving#getSpeed() speed} in averaging. We're still
|
||||
|
||||
Reference in new issue
Block a user