mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-09-23 22:19:13 +00:00
bug5217: unfinished (7) ; implementation of hashFingerprint
This commit is contained in:
+1
-2
@@ -2,7 +2,6 @@ package com.sap.sailing.domain.test.hash;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.junit.Before;
|
||||
|
||||
@@ -11,7 +10,7 @@ import com.sap.sailing.domain.tracking.impl.DynamicTrackedRaceImpl;
|
||||
import com.sap.sailing.domain.tractracadapter.ReceiverType;
|
||||
|
||||
public class CalculatingHashTest extends OnlineTracTracBasedTest {
|
||||
private static final Logger logger = Logger.getLogger(CalculatingHashTest.class.getName());
|
||||
// private static final Logger logger = Logger.getLogger(CalculatingHashTest.class.getName());
|
||||
DynamicTrackedRaceImpl trackedRace1;
|
||||
DynamicTrackedRaceImpl trackedRace2;
|
||||
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.sap.sailing.domain.markpassinghash;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public interface TrackedRaceHashFingerprint {
|
||||
|
||||
JSONObject toJson();
|
||||
|
||||
public int getCompetitor();
|
||||
|
||||
public int getStart();
|
||||
|
||||
public int getEnd();
|
||||
|
||||
public int getWaypoints();
|
||||
|
||||
public int getNumberOfGPSFixes();
|
||||
|
||||
public int getGpsFixes();
|
||||
|
||||
}
|
||||
+2
-1
@@ -1,9 +1,10 @@
|
||||
package com.sap.sailing.domain.markpassinghash;
|
||||
|
||||
import com.sap.sailing.domain.markpassinghash.impl.TrackedRaceHashFingerprintImpl;
|
||||
import com.sap.sailing.domain.markpassinghash.impl.TrackedRaceHashForMarkPassingCalculationImpl;
|
||||
import com.sap.sailing.domain.tracking.impl.TrackedRaceImpl;
|
||||
|
||||
public interface TrackedRaceHashForMaskPassingCalculationFactory {
|
||||
|
||||
TrackedRaceHashForMarkPassingCalculationImpl create(TrackedRaceImpl trackedRace);
|
||||
TrackedRaceHashFingerprintImpl create(TrackedRaceImpl trackedRace);
|
||||
}
|
||||
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.sap.sailing.domain.markpassinghash.impl;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import com.sap.sailing.domain.markpassinghash.TrackedRaceHashFingerprint;
|
||||
|
||||
public class TrackedRaceHashFingerprintImpl implements TrackedRaceHashFingerprint{
|
||||
private final int competitor;
|
||||
private final int start;
|
||||
private final int end;
|
||||
private final int waypoints;
|
||||
private final int numberOfGPSFixes;
|
||||
private final int gpsFixes;
|
||||
|
||||
public TrackedRaceHashFingerprintImpl(Integer competitor, Integer start, Integer end, Integer waypoints,
|
||||
Integer numberOfGPSFixes, Integer gpsFixes) {
|
||||
this.competitor = competitor;
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
this.waypoints = waypoints;
|
||||
this.numberOfGPSFixes = numberOfGPSFixes;
|
||||
this.gpsFixes = gpsFixes;
|
||||
}
|
||||
|
||||
public JSONObject toJson() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getCompetitor() {
|
||||
return competitor;
|
||||
}
|
||||
|
||||
public int getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public int getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
public int getWaypoints() {
|
||||
return waypoints;
|
||||
}
|
||||
|
||||
public int getNumberOfGPSFixes() {
|
||||
return numberOfGPSFixes;
|
||||
}
|
||||
|
||||
public int getGpsFixes() {
|
||||
return gpsFixes;
|
||||
}
|
||||
}
|
||||
+151
-8
@@ -1,17 +1,160 @@
|
||||
package com.sap.sailing.domain.markpassinghash.impl;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.sap.sailing.domain.base.Competitor;
|
||||
import com.sap.sailing.domain.base.Mark;
|
||||
import com.sap.sailing.domain.base.Waypoint;
|
||||
import com.sap.sailing.domain.common.Position;
|
||||
import com.sap.sailing.domain.common.tracking.GPSFix;
|
||||
import com.sap.sailing.domain.markpassinghash.TrackedRaceHashForMaskPassingCalculationFactory;
|
||||
import com.sap.sailing.domain.tracking.impl.TrackedRaceImpl;
|
||||
import com.sap.sse.common.TimePoint;
|
||||
import com.sap.sse.common.Util;
|
||||
|
||||
public class TrackedRaceHashForMarkPassingCalculationFactoryImpl implements TrackedRaceHashForMaskPassingCalculationFactory{
|
||||
|
||||
public TrackedRaceHashForMarkPassingCalculationFactoryImpl() {
|
||||
}
|
||||
public class TrackedRaceHashForMarkPassingCalculationFactoryImpl
|
||||
implements TrackedRaceHashForMaskPassingCalculationFactory {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(TrackedRaceHashForMarkPassingComparatorImpl.class.getName());
|
||||
|
||||
@Override
|
||||
public TrackedRaceHashForMarkPassingCalculationImpl create(TrackedRaceImpl trackedRace) {
|
||||
|
||||
TrackedRaceHashForMarkPassingCalculationImpl hashValues = new TrackedRaceHashForMarkPassingCalculationImpl(trackedRace.getHashValuesForMarkPassingCalculation());
|
||||
return hashValues;
|
||||
public TrackedRaceHashFingerprintImpl create(TrackedRaceImpl trackedRace) {
|
||||
int hashForCompetitors = 0;
|
||||
int hashForWaypoints = calculateHashForWaypoints(trackedRace);
|
||||
for (Competitor c : trackedRace.getRace().getCompetitors()) {
|
||||
hashForCompetitors = hashForCompetitors + calculateHashForCompetitor(c);
|
||||
}
|
||||
int hashForStart = calculateHashForStart(trackedRace);
|
||||
int hashForEnd = calculateHashForEnd(trackedRace);
|
||||
int hashForNumberOfGPSFixes = calculateHashForNumberOfGPSFixes(trackedRace);
|
||||
int hashForGPSFixes = calculateHashForGPSFixes(trackedRace);
|
||||
|
||||
return new TrackedRaceHashFingerprintImpl(hashForWaypoints, hashForCompetitors, hashForStart, hashForEnd,
|
||||
hashForNumberOfGPSFixes, hashForGPSFixes);
|
||||
}
|
||||
|
||||
private int calculateHashForCompetitor(Competitor c) {
|
||||
int res = 0;
|
||||
try {
|
||||
res = res ^ c.getId().hashCode();
|
||||
res = (res << 5) - res;
|
||||
} catch (Exception e) {
|
||||
logger.info("Hash calculation for competitor " + c + " failed: " + e);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private int calculateHashForStart(TrackedRaceImpl trackedRace) {
|
||||
int res = 0;
|
||||
try {
|
||||
res = res ^ calculateHashForTimePoint(trackedRace.getStartOfTracking());
|
||||
res = (res << 5) - res;
|
||||
} catch (Exception e) {
|
||||
logger.info("An error occured when getting the start of tracking: " + e);
|
||||
}
|
||||
try {
|
||||
// Maybe that could be solved by using .getStartOfRace(boolean inferred)
|
||||
if (trackedRace.getTrackedRegatta().getRegatta().useStartTimeInference() == false) {
|
||||
res = res ^ calculateHashForTimePoint(trackedRace.getStartOfRace());
|
||||
res = (res << 5) - res;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.info("An error occured when getting the starttime: " + e);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private int calculateHashForEnd(TrackedRaceImpl trackedRace) {
|
||||
int res = 0;
|
||||
try {
|
||||
res = res ^ calculateHashForTimePoint(trackedRace.getEndOfTracking());
|
||||
res = (res << 5) - res;
|
||||
} catch (Exception e) {
|
||||
logger.info("Hash calculation for end of Tracking failed: " + e);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private int calculateHashForNumberOfGPSFixes(TrackedRaceImpl trackedRace) {
|
||||
Iterable<Waypoint> waypoints = trackedRace.getRace().getCourse().getWaypoints();
|
||||
int count = 0;
|
||||
for (Waypoint w : waypoints) {
|
||||
for (Mark m : w.getMarks()) {
|
||||
Iterable<GPSFix> gpsTrack = null;
|
||||
try {
|
||||
trackedRace.getTrack(m).lockForRead();
|
||||
gpsTrack = trackedRace.getTrack(m).getFixes();
|
||||
// Unused warning since we need the individual Fixes only to count
|
||||
for (int i = 0; i < Util.size(gpsTrack); i++) {
|
||||
count++;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.info("Counting of GPSFixes produced an error: " + e);
|
||||
} finally {
|
||||
trackedRace.getTrack(m).unlockAfterRead();
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private int calculateHashForGPSFixes(TrackedRaceImpl trackedRace) {
|
||||
Iterable<Waypoint> waypoints = trackedRace.getRace().getCourse().getWaypoints();
|
||||
int res = 0;
|
||||
for (Waypoint w : waypoints) {
|
||||
for (Mark m : w.getMarks()) {
|
||||
Iterable<GPSFix> gpsTrack = null;
|
||||
try {
|
||||
trackedRace.getTrack(m).lockForRead();
|
||||
gpsTrack = trackedRace.getTrack(m).getFixes();
|
||||
for (GPSFix gf : gpsTrack) {
|
||||
res = res ^ calculateHashForTimePoint(gf.getTimePoint());
|
||||
res = (res << 5) - res;
|
||||
res = res ^ calculateHashForPosition(gf.getPosition());
|
||||
res = (res << 5) - res;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.info("Hash calculation for GPSFixes of the mark " + m + "failed: " + e);
|
||||
} finally {
|
||||
trackedRace.getTrack(m).unlockAfterRead();
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private int calculateHashForTimePoint(TimePoint tp) {
|
||||
int res = 0;
|
||||
// How problematic is this cast?
|
||||
try {
|
||||
res = res ^ (int) tp.asMillis();
|
||||
res = (res << 5) - res;
|
||||
} catch (Exception e) {
|
||||
logger.info("Ther");
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
private int calculateHashForPosition(Position p) {
|
||||
int res = 0;
|
||||
res = res ^ (int) p.getLatDeg();
|
||||
res = res ^ (int) p.getLngDeg();
|
||||
res = (res << 5) - res;
|
||||
return res;
|
||||
}
|
||||
|
||||
private int calculateHashForWaypoints(TrackedRaceImpl trackedRace) {
|
||||
Iterable<Waypoint> waypoints = trackedRace.getRace().getCourse().getWaypoints();
|
||||
int res = 0;
|
||||
for (Waypoint p : waypoints) {
|
||||
res = res ^ p.getId().hashCode();
|
||||
try {
|
||||
res = res ^ p.getPassingInstructions().hashCode();
|
||||
} catch (Exception e) {
|
||||
logger.info("Hash calculation for Waypoints failed: " + e);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
+11
-31
@@ -1,54 +1,34 @@
|
||||
package com.sap.sailing.domain.markpassinghash.impl;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.sap.sailing.domain.base.Competitor;
|
||||
import com.sap.sailing.domain.markpassinghash.impl.TrackedRaceHashForMarkPassingComparatorImpl.TypeOfHash;
|
||||
import com.sap.sailing.domain.tracking.impl.TrackedRaceImpl;
|
||||
|
||||
public class TrackedRaceHashForMarkPassingCalculationImpl {
|
||||
private final int competitor;
|
||||
private final int start;
|
||||
private final int end;
|
||||
private final int waypoints;
|
||||
private final int numberOfGPSFixes;
|
||||
private final int gpsFixes;
|
||||
|
||||
public TrackedRaceHashForMarkPassingCalculationImpl(Map<TypeOfHash, Integer> hashValues) {
|
||||
this.competitor = hashValues.get(TypeOfHash.COMPETITOR);
|
||||
this.start = hashValues.get(TypeOfHash.START);
|
||||
this.end = hashValues.get(TypeOfHash.END);
|
||||
this.waypoints = hashValues.get(TypeOfHash.WAYPOINTS);
|
||||
this.numberOfGPSFixes = hashValues.get(TypeOfHash.NUMBEROFGPSFIXES);
|
||||
this.gpsFixes = hashValues.get(TypeOfHash.GPSFIXES);
|
||||
private final TrackedRaceHashFingerprintImpl hashFingerprint;
|
||||
|
||||
public TrackedRaceHashForMarkPassingCalculationImpl(TrackedRaceHashFingerprintImpl hashFingerprint) {
|
||||
this.hashFingerprint = hashFingerprint;
|
||||
}
|
||||
|
||||
public boolean equals(TrackedRaceImpl trackedRace) {
|
||||
TrackedRaceHashForMarkPassingComparatorImpl hashCalculator = new TrackedRaceHashForMarkPassingComparatorImpl(
|
||||
trackedRace);
|
||||
int competitorHash = 0;
|
||||
TrackedRaceHashForMarkPassingCalculationFactoryImpl factory = new TrackedRaceHashForMarkPassingCalculationFactoryImpl();
|
||||
TrackedRaceHashFingerprintImpl trackedRaceHashFingerprint = factory.create(trackedRace);
|
||||
|
||||
if (hashCalculator.calculateHashForWaypoints() != waypoints)
|
||||
if (hashFingerprint.getWaypoints() != trackedRaceHashFingerprint.getWaypoints())
|
||||
return false;
|
||||
|
||||
for (Competitor c : trackedRace.getRace().getCompetitors()) {
|
||||
competitorHash = competitorHash + hashCalculator.calculateHashForCompetitor(c);
|
||||
}
|
||||
|
||||
if (competitorHash != competitor)
|
||||
if (hashFingerprint.getCompetitor() != trackedRaceHashFingerprint.getCompetitor())
|
||||
return false;
|
||||
|
||||
if (hashCalculator.calculateHashForStart() != start)
|
||||
if (hashFingerprint.getStart() != trackedRaceHashFingerprint.getStart())
|
||||
return false;
|
||||
|
||||
if (hashCalculator.calculateHashForEnd() != end)
|
||||
if (hashFingerprint.getEnd() != trackedRaceHashFingerprint.getEnd())
|
||||
return false;
|
||||
|
||||
if (hashCalculator.calculateHashForNumberOfGPSFixes() != numberOfGPSFixes)
|
||||
if (hashFingerprint.getNumberOfGPSFixes() != trackedRaceHashFingerprint.getNumberOfGPSFixes())
|
||||
return false;
|
||||
|
||||
if (hashCalculator.calculateHashForGPSFixes() != gpsFixes)
|
||||
if (hashFingerprint.getGpsFixes() != trackedRaceHashFingerprint.getGpsFixes())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user