fixed other calls to getOrCreateWindTrack by introducing a variant that doesn't require a delay argument

This commit is contained in:
Axel Uhl committed 2012-04-25 23:00:46 +02:00
1 parent d53c576b42
commit 5c29b9c771
7 files changed
+31 -18

No files matched your search

@@ -74,7 +74,7 @@ public class MongoObjectFactoryImpl implements MongoObjectFactory {
@Override @Override
public void addWindTrackDumper(TrackedEvent trackedEvent, TrackedRace trackedRace, WindSource windSource) { public void addWindTrackDumper(TrackedEvent trackedEvent, TrackedRace trackedRace, WindSource windSource) {
WindTrack windTrack = trackedRace.getOrCreateWindTrack(windSource, delayForWindEstimationCacheInvalidation); WindTrack windTrack = trackedRace.getOrCreateWindTrack(windSource);
windTrack.addListener(new MongoWindListener(trackedEvent, trackedRace, windSource, this, database)); windTrack.addListener(new MongoWindListener(trackedEvent, trackedRace, windSource, this, database));
} }
@@ -209,6 +209,11 @@ public interface TrackedRace extends Serializable {
*/ */
Iterable<WindSource> getWindSources(); Iterable<WindSource> getWindSources();
/**
* Same as {@link #getOrCreateWindTrack(WindSource, long) getOrCreateWindTrack(windSource, getMillisecondsOverWhichToAverageWind())}.
*/
WindTrack getOrCreateWindTrack(WindSource windSource);
WindTrack getOrCreateWindTrack(WindSource windSource, long delayForWindEstimationCacheInvalidation); WindTrack getOrCreateWindTrack(WindSource windSource, long delayForWindEstimationCacheInvalidation);
/** /**
@@ -77,7 +77,7 @@ public class DynamicTrackedRaceImpl extends TrackedRaceImpl implements
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject(); ois.defaultReadObject();
for (WindSource windSource : getWindSources()) { for (WindSource windSource : getWindSources()) {
getOrCreateWindTrack(windSource, getMillisecondsOverWhichToAverageWind()/2).addListener(this); getOrCreateWindTrack(windSource).addListener(this);
} }
} }
@@ -118,8 +118,7 @@ public class DynamicTrackedRaceImpl extends TrackedRaceImpl implements
public void setMillisecondsOverWhichToAverageWind(long millisecondsOverWhichToAverageWind) { public void setMillisecondsOverWhichToAverageWind(long millisecondsOverWhichToAverageWind) {
this.millisecondsOverWhichToAverageWind = millisecondsOverWhichToAverageWind; this.millisecondsOverWhichToAverageWind = millisecondsOverWhichToAverageWind;
for (WindSource windSource : getWindSources()) { for (WindSource windSource : getWindSources()) {
getOrCreateWindTrack(windSource, millisecondsOverWhichToAverageWind/2). getOrCreateWindTrack(windSource).setMillisecondsOverWhichToAverage(millisecondsOverWhichToAverageWind);
setMillisecondsOverWhichToAverage(millisecondsOverWhichToAverageWind);
} }
updated(MillisecondsTimePoint.now()); updated(MillisecondsTimePoint.now());
} }
@@ -379,13 +378,13 @@ public class DynamicTrackedRaceImpl extends TrackedRaceImpl implements
@Override @Override
public synchronized void recordWind(Wind wind, WindSource windSource) { public synchronized void recordWind(Wind wind, WindSource windSource) {
getOrCreateWindTrack(windSource, getMillisecondsOverWhichToAverageWind()/2).add(wind); getOrCreateWindTrack(windSource).add(wind);
updated(null); // wind events shouldn't advance race time updated(null); // wind events shouldn't advance race time
} }
@Override @Override
public synchronized void removeWind(Wind wind, WindSource windSource) { public synchronized void removeWind(Wind wind, WindSource windSource) {
getOrCreateWindTrack(windSource, getMillisecondsOverWhichToAverageWind()/2).remove(wind); getOrCreateWindTrack(windSource).remove(wind);
updated(wind.getTimePoint()); updated(wind.getTimePoint());
} }
@@ -436,7 +435,7 @@ public class DynamicTrackedRaceImpl extends TrackedRaceImpl implements
public long getMillisecondsOverWhichToAverageWind() { public long getMillisecondsOverWhichToAverageWind() {
long result = 0; // default in case there is no competitor long result = 0; // default in case there is no competitor
for (WindSource windSource : getWindSources()) { for (WindSource windSource : getWindSources()) {
WindTrack someTrack = getOrCreateWindTrack(windSource, 0); WindTrack someTrack = getOrCreateWindTrack(windSource);
result = someTrack.getMillisecondsOverWhichToAverageWind(); result = someTrack.getMillisecondsOverWhichToAverageWind();
} }
return result; return result;
@@ -754,6 +754,9 @@ public abstract class TrackedRaceImpl implements TrackedRace, CourseListener {
* {@link #windTracks} for the respective source. If found, it's returned; otherwise the wind track is created * {@link #windTracks} for the respective source. If found, it's returned; otherwise the wind track is created
* through the {@link #windStore} using {@link #createWindTrack(WindSource, long)} and added to {@link #windTracks} before * through the {@link #windStore} using {@link #createWindTrack(WindSource, long)} and added to {@link #windTracks} before
* being returned. * being returned.
*
* @param delayForWindEstimationCacheInvalidation if <code>-1</code> and the parameter is accessed, it will be
* replaced by {@link #getMillisecondsOverWhichToAverageWind()}/2
*/ */
@Override @Override
public WindTrack getOrCreateWindTrack(WindSource windSource, long delayForWindEstimationCacheInvalidation) { public WindTrack getOrCreateWindTrack(WindSource windSource, long delayForWindEstimationCacheInvalidation) {
@@ -764,7 +767,8 @@ public abstract class TrackedRaceImpl implements TrackedRace, CourseListener {
synchronized (windTracks) { synchronized (windTracks) {
result = windTracks.get(windSource); result = windTracks.get(windSource);
if (result == null) { if (result == null) {
result = createWindTrack(windSource, delayForWindEstimationCacheInvalidation); result = createWindTrack(windSource, delayForWindEstimationCacheInvalidation == -1 ?
getMillisecondsOverWhichToAverageWind()/2 : delayForWindEstimationCacheInvalidation);
windTracks.put(windSource, result); windTracks.put(windSource, result);
} }
} }
@@ -772,6 +776,11 @@ public abstract class TrackedRaceImpl implements TrackedRace, CourseListener {
return result; return result;
} }
@Override
public WindTrack getOrCreateWindTrack(WindSource windSource) {
return getOrCreateWindTrack(windSource, -1);
}
/** /**
* Creates a wind track for the <code>windSource</code> specified and stores it in {@link #windTracks}. The * Creates a wind track for the <code>windSource</code> specified and stores it in {@link #windTracks}. The
* averaging interval is set according to the averaging interval set for all other wind sources, or the default if * averaging interval is set according to the averaging interval set for all other wind sources, or the default if
@@ -824,7 +833,7 @@ public abstract class TrackedRaceImpl implements TrackedRace, CourseListener {
for (WindSource windSource : getWindSources()) { for (WindSource windSource : getWindSources()) {
// TODO consider parallelizing // TODO consider parallelizing
if (!Util.contains(windSourcesToExclude, windSource)) { if (!Util.contains(windSourcesToExclude, windSource)) {
WindTrack track = getOrCreateWindTrack(windSource, getMillisecondsOverWhichToAverageWind()/2); WindTrack track = getOrCreateWindTrack(windSource);
WindWithConfidence<Pair<Position, TimePoint>> windWithConfidence = track.getAveragedWindWithConfidence( WindWithConfidence<Pair<Position, TimePoint>> windWithConfidence = track.getAveragedWindWithConfidence(
p, at); p, at);
if (windWithConfidence != null) { if (windWithConfidence != null) {
@@ -610,7 +610,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
for (WindSource windSource : windSourcesToDeliver) { for (WindSource windSource : windSourcesToDeliver) {
WindTrackInfoDTO windTrackInfoDTO = new WindTrackInfoDTO(); WindTrackInfoDTO windTrackInfoDTO = new WindTrackInfoDTO();
windTrackInfoDTO.windFixes = new ArrayList<WindDTO>(); windTrackInfoDTO.windFixes = new ArrayList<WindDTO>();
WindTrack windTrack = trackedRace.getOrCreateWindTrack(windSource, delayForWindEstimationCacheInvalidation); WindTrack windTrack = trackedRace.getOrCreateWindTrack(windSource);
windTrackInfoDTO.dampeningIntervalInMilliseconds = windTrack.getMillisecondsOverWhichToAverageWind(); windTrackInfoDTO.dampeningIntervalInMilliseconds = windTrack.getMillisecondsOverWhichToAverageWind();
Iterator<Wind> windIter = windTrack.getFixesIterator(from, /* inclusive */true); Iterator<Wind> windIter = windTrack.getFixesIterator(from, /* inclusive */true);
while (windIter.hasNext()) { while (windIter.hasNext()) {
@@ -705,7 +705,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
TimePoint fromTimePoint = new MillisecondsTimePoint(from); TimePoint fromTimePoint = new MillisecondsTimePoint(from);
WindTrackInfoDTO windTrackInfoDTO = new WindTrackInfoDTO(); WindTrackInfoDTO windTrackInfoDTO = new WindTrackInfoDTO();
windTrackInfoDTO.windFixes = new ArrayList<WindDTO>(); windTrackInfoDTO.windFixes = new ArrayList<WindDTO>();
WindTrack windTrack = trackedRace.getOrCreateWindTrack(windSource, delayForWindEstimationCacheInvalidation); WindTrack windTrack = trackedRace.getOrCreateWindTrack(windSource);
windTrackInfoDTOs.put(windSource, windTrackInfoDTO); windTrackInfoDTOs.put(windSource, windTrackInfoDTO);
windTrackInfoDTO.dampeningIntervalInMilliseconds = windTrack windTrackInfoDTO.dampeningIntervalInMilliseconds = windTrack
.getMillisecondsOverWhichToAverageWind(); .getMillisecondsOverWhichToAverageWind();
@@ -757,7 +757,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
if (windSourceTypeNames == null || windSourceTypeNames.contains(windSource.getType().name())) { if (windSourceTypeNames == null || windSourceTypeNames.contains(windSource.getType().name())) {
WindTrackInfoDTO windTrackInfoDTO = new WindTrackInfoDTO(); WindTrackInfoDTO windTrackInfoDTO = new WindTrackInfoDTO();
windTrackInfoDTO.windFixes = new ArrayList<WindDTO>(); windTrackInfoDTO.windFixes = new ArrayList<WindDTO>();
WindTrack windTrack = trackedRace.getOrCreateWindTrack(windSource, delayForWindEstimationCacheInvalidation); WindTrack windTrack = trackedRace.getOrCreateWindTrack(windSource);
windTrackInfoDTOs.put(windSource, windTrackInfoDTO); windTrackInfoDTOs.put(windSource, windTrackInfoDTO);
windTrackInfoDTO.dampeningIntervalInMilliseconds = windTrack windTrackInfoDTO.dampeningIntervalInMilliseconds = windTrack
.getMillisecondsOverWhichToAverageWind(); .getMillisecondsOverWhichToAverageWind();
@@ -895,7 +895,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
trackedLegOfCompetitor.getLeg()).getLegType(fix.getTimePoint()); trackedLegOfCompetitor.getLeg()).getLegType(fix.getTimePoint());
GPSFixDTO fixDTO = createGPSFixDTO(fix, fix.getSpeed(), createWindDTOFromAlreadyAveraged(trackedRace.getWind(fix.getPosition(), GPSFixDTO fixDTO = createGPSFixDTO(fix, fix.getSpeed(), createWindDTOFromAlreadyAveraged(trackedRace.getWind(fix.getPosition(),
toTimePointExcluding), trackedRace toTimePointExcluding), trackedRace
.getOrCreateWindTrack(windSource, delayForWindEstimationCacheInvalidation)), tack, legType, /* extrapolate */ .getOrCreateWindTrack(windSource)), tack, legType, /* extrapolate */
false); false);
fixesForCompetitor.add(fixDTO); fixesForCompetitor.add(fixDTO);
if (fixIter.hasNext()) { if (fixIter.hasNext()) {
@@ -915,7 +915,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
createSpeedWithBearingDTO(speedWithBearing), createSpeedWithBearingDTO(speedWithBearing),
createWindDTOFromAlreadyAveraged(trackedRace.getWind(position, createWindDTOFromAlreadyAveraged(trackedRace.getWind(position,
toTimePointExcluding), trackedRace toTimePointExcluding), trackedRace
.getOrCreateWindTrack(windSource, delayForWindEstimationCacheInvalidation)), /* extrapolated */ .getOrCreateWindTrack(windSource)), /* extrapolated */
tack2, legType2, true); tack2, legType2, true);
fixesForCompetitor.add(extrapolated); fixesForCompetitor.add(extrapolated);
} }
@@ -1588,7 +1588,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
trackedLegOfCompetitor.getLeg()).getLegType(fix.getTimePoint()); trackedLegOfCompetitor.getLeg()).getLegType(fix.getTimePoint());
GPSFixDTO fixDTO = createGPSFixDTO(fix, speedWithBearing, createWindDTOFromAlreadyAveraged(trackedRace.getWind( GPSFixDTO fixDTO = createGPSFixDTO(fix, speedWithBearing, createWindDTOFromAlreadyAveraged(trackedRace.getWind(
fix.getPosition(), fix.getTimePoint()), fix.getPosition(), fix.getTimePoint()),
trackedRace.getOrCreateWindTrack(windSource, delayForWindEstimationCacheInvalidation)), tack, legType, /* extrapolated */false); trackedRace.getOrCreateWindTrack(windSource)), tack, legType, /* extrapolated */false);
gpsFixDouglasList.add(fixDTO); gpsFixDouglasList.add(fixDTO);
} }
result.put(competitorDTO, gpsFixDouglasList); result.put(competitorDTO, gpsFixDouglasList);
@@ -91,7 +91,7 @@ public class TestStoringAndRetrievingWindTracksTest extends AbstractTracTracLive
Mongo myFirstMongo = newMongo(); Mongo myFirstMongo = newMongo();
DB firstDatabase = myFirstMongo.getDB(dbConfiguration.getDatabaseName()); DB firstDatabase = myFirstMongo.getDB(dbConfiguration.getDatabaseName());
new MongoObjectFactoryImpl(firstDatabase).addWindTrackDumper(trackedEvent, trackedRace, windSource); new MongoObjectFactoryImpl(firstDatabase).addWindTrackDumper(trackedEvent, trackedRace, windSource);
WindTrack windTrack = trackedRace.getOrCreateWindTrack(windSource, delayForWindEstimationCacheInvalidation); WindTrack windTrack = trackedRace.getOrCreateWindTrack(windSource);
Position pos = new DegreePosition(54, 9); Position pos = new DegreePosition(54, 9);
TimePoint timePoint = MillisecondsTimePoint.now(); TimePoint timePoint = MillisecondsTimePoint.now();
for (double bearingDeg = 123.4; bearingDeg<140; bearingDeg += 1.1) { for (double bearingDeg = 123.4; bearingDeg<140; bearingDeg += 1.1) {
@@ -262,7 +262,7 @@ public class AdminApp extends Servlet {
JSONObject jsonWindTracks = new JSONObject(); JSONObject jsonWindTracks = new JSONObject();
for (WindSource windSource : trackedRace.getWindSources()) { for (WindSource windSource : trackedRace.getWindSources()) {
JSONArray jsonWindArray = new JSONArray(); JSONArray jsonWindArray = new JSONArray();
WindTrack windTrack = trackedRace.getOrCreateWindTrack(windSource, delayForWindEstimationCacheInvalidation); WindTrack windTrack = trackedRace.getOrCreateWindTrack(windSource);
synchronized (windTrack) { synchronized (windTrack) {
Iterator<Wind> windIter = windTrack.getFixesIterator(from, /* inclusive */true); Iterator<Wind> windIter = windTrack.getFixesIterator(from, /* inclusive */true);
while (windIter.hasNext()) { while (windIter.hasNext()) {
@@ -370,7 +370,7 @@ public class AdminApp extends Servlet {
try { try {
final DynamicTrackedRace trackedRace = getService().getOrCreateTrackedEvent(event) final DynamicTrackedRace trackedRace = getService().getOrCreateTrackedEvent(event)
.getTrackedRace(race); .getTrackedRace(race);
WindTrack windTrack = trackedRace.getOrCreateWindTrack(trackedRace.getWindSources(windSourceType).iterator().next(), delayForWindEstimationCacheInvalidation); WindTrack windTrack = trackedRace.getOrCreateWindTrack(trackedRace.getWindSources(windSourceType).iterator().next());
TimePoint timePoint = getTimePoint(req, PARAM_NAME_TIME, PARAM_NAME_TIME_MILLIS, TimePoint timePoint = getTimePoint(req, PARAM_NAME_TIME, PARAM_NAME_TIME_MILLIS,
MillisecondsTimePoint.now()); MillisecondsTimePoint.now());
Wind wind = windTrack.getLastFixAtOrBefore(timePoint); Wind wind = windTrack.getLastFixAtOrBefore(timePoint);