splitting RaceDTO into BasicRaceDTO and RaceDTO where BasicRaceDTO has all it takes for isLive check, making MetaLeaderboardRaceColumnDTO smaller

This commit is contained in:
Axel Uhl committed 2015-12-04 16:14:42 +01:00
1 parent bc78f2773b
commit 250b912328
3 files changed
+124 -75

No files matched your search

@@ -0,0 +1,120 @@
package com.sap.sailing.domain.common.dto;
import java.util.Date;
import com.sap.sailing.domain.common.RegattaAndRaceIdentifier;
import com.sap.sailing.domain.common.TimingConstants;
/**
* Master data about a single race that is to be transferred to the client.<p>
*
* @author Axel Uhl (d043530)
*
*/
public class BasicRaceDTO extends NamedDTO {
private static final long serialVersionUID = 2613189982608149975L;
/**
* Tells if this race is currently being tracked, meaning that a {@link RaceTracker} is
* listening for incoming GPS fixes, mark passings etc., to update a {@link TrackedRace} object
* accordingly.
*/
public boolean isTracked;
public Date startOfRace;
public Date endOfRace;
public TrackedRaceDTO trackedRace;
public BasicRaceDTO() {} // for GWT serialization only
public BasicRaceDTO(RegattaAndRaceIdentifier raceIdentifier, TrackedRaceDTO trackedRace) {
super(raceIdentifier.getRaceName());
this.trackedRace = trackedRace;
}
/**
* @see {@link TrackedRace#isLive} for further explanation.
* @param serverTimePointAsMillis
* the time point (in server clock time) at which to determine whether the race is/was live
* @return <code>true</code> if <code>serverTimePointAsMillis</code> is between (inclusively) the start and end time
* point of the "live" interval as defined above.
*/
public boolean isLive(long serverTimePointAsMillis) {
final Date startOfLivePeriod;
final Date endOfLivePeriod;
if (trackedRace == null || !trackedRace.hasGPSData || !trackedRace.hasWindData) {
startOfLivePeriod = null;
endOfLivePeriod = null;
} else {
if (startOfRace == null) {
startOfLivePeriod = trackedRace.startOfTracking;
} else {
startOfLivePeriod = new Date(startOfRace.getTime() - TimingConstants.PRE_START_PHASE_DURATION_IN_MILLIS);
}
if (endOfRace == null) {
if (trackedRace.timePointOfNewestEvent != null) {
endOfLivePeriod = new Date(trackedRace.timePointOfNewestEvent.getTime()
+ TimingConstants.IS_LIVE_GRACE_PERIOD_IN_MILLIS);
} else {
endOfLivePeriod = null;
}
} else {
endOfLivePeriod = new Date(endOfRace.getTime() + TimingConstants.IS_LIVE_GRACE_PERIOD_IN_MILLIS);
}
}
// if an empty timepoint is given then take the start of the race
if (serverTimePointAsMillis == 0) {
serverTimePointAsMillis = startOfLivePeriod.getTime()+1;
}
// whenLastTrackedRaceWasLive is null if there is no tracked race for fleet, or the tracked race hasn't started yet at the server time
// when this DTO was assembled, or there were no GPS or wind data
final boolean result =
startOfLivePeriod != null &&
endOfLivePeriod != null &&
startOfLivePeriod.getTime() <= serverTimePointAsMillis &&
serverTimePointAsMillis <= endOfLivePeriod.getTime();
return result;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((endOfRace == null) ? 0 : endOfRace.hashCode());
result = prime * result + (isTracked ? 1231 : 1237);
result = prime * result + ((startOfRace == null) ? 0 : startOfRace.hashCode());
result = prime * result + ((trackedRace == null) ? 0 : trackedRace.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
BasicRaceDTO other = (BasicRaceDTO) obj;
if (endOfRace == null) {
if (other.endOfRace != null)
return false;
} else if (!endOfRace.equals(other.endOfRace))
return false;
if (isTracked != other.isTracked)
return false;
if (startOfRace == null) {
if (other.startOfRace != null)
return false;
} else if (!startOfRace.equals(other.startOfRace))
return false;
if (trackedRace == null) {
if (other.trackedRace != null)
return false;
} else if (!trackedRace.equals(other.trackedRace))
return false;
return true;
}
}
@@ -6,10 +6,10 @@ public class MetaLeaderboardRaceColumnDTO extends RaceColumnDTO {
private static final long serialVersionUID = 6152752963316150432L;
private ArrayList<RaceDTO> raceList = new ArrayList<RaceDTO>();
private ArrayList<BasicRaceDTO> raceList = new ArrayList<BasicRaceDTO>();
public boolean isLive(FleetDTO fleet, long serverTimePointAsMillis) {
for (RaceDTO race : raceList) {
for (BasicRaceDTO race : raceList) {
if (race.isLive(serverTimePointAsMillis)) {
return true;
}
@@ -1,10 +1,7 @@
package com.sap.sailing.domain.common.dto;
import java.util.Date;
import com.sap.sailing.domain.common.RegattaAndRaceIdentifier;
import com.sap.sailing.domain.common.RegattaNameAndRaceName;
import com.sap.sailing.domain.common.TimingConstants;
/**
* Master data about a single race that is to be transferred to the client.<p>
@@ -12,7 +9,7 @@ import com.sap.sailing.domain.common.TimingConstants;
* @author Axel Uhl (d043530)
*
*/
public class RaceDTO extends NamedDTO {
public class RaceDTO extends BasicRaceDTO {
private static final long serialVersionUID = 2613189982608149975L;
public enum RaceLiveState { NOT_TRACKED, TRACKED, TRACKED_AND_LIVE, TRACKED_BUT_NOT_SCHEDULED };
@@ -24,13 +21,10 @@ public class RaceDTO extends NamedDTO {
*/
public boolean isTracked;
public Date startOfRace;
public Date endOfRace;
public RaceStatusDTO status;
public PlacemarkOrderDTO places;
public TrackedRaceDTO trackedRace;
public TrackedRaceStatisticsDTO trackedRaceStatistics;
private String regattaName;
@@ -43,9 +37,8 @@ public class RaceDTO extends NamedDTO {
}
public RaceDTO(RegattaAndRaceIdentifier raceIdentifier, TrackedRaceDTO trackedRace, boolean isCurrentlyTracked) {
super(raceIdentifier.getRaceName());
super(raceIdentifier, trackedRace);
this.regattaName = raceIdentifier.getRegattaName();
this.trackedRace = trackedRace;
this.isTracked = isCurrentlyTracked;
}
@@ -71,64 +64,15 @@ public class RaceDTO extends NamedDTO {
return regattaName;
}
/**
* @see {@link TrackedRace#isLive} for further explanation.
* @param serverTimePointAsMillis
* the time point (in server clock time) at which to determine whether the race is/was live
* @return <code>true</code> if <code>serverTimePointAsMillis</code> is between (inclusively) the start and end time
* point of the "live" interval as defined above.
*/
public boolean isLive(long serverTimePointAsMillis) {
final Date startOfLivePeriod;
final Date endOfLivePeriod;
if (trackedRace == null || !trackedRace.hasGPSData || !trackedRace.hasWindData) {
startOfLivePeriod = null;
endOfLivePeriod = null;
} else {
if (startOfRace == null) {
startOfLivePeriod = trackedRace.startOfTracking;
} else {
startOfLivePeriod = new Date(startOfRace.getTime() - TimingConstants.PRE_START_PHASE_DURATION_IN_MILLIS);
}
if (endOfRace == null) {
if (trackedRace.timePointOfNewestEvent != null) {
endOfLivePeriod = new Date(trackedRace.timePointOfNewestEvent.getTime()
+ TimingConstants.IS_LIVE_GRACE_PERIOD_IN_MILLIS);
} else {
endOfLivePeriod = null;
}
} else {
endOfLivePeriod = new Date(endOfRace.getTime() + TimingConstants.IS_LIVE_GRACE_PERIOD_IN_MILLIS);
}
}
// if an empty timepoint is given then take the start of the race
if (serverTimePointAsMillis == 0) {
serverTimePointAsMillis = startOfLivePeriod.getTime()+1;
}
// whenLastTrackedRaceWasLive is null if there is no tracked race for fleet, or the tracked race hasn't started yet at the server time
// when this DTO was assembled, or there were no GPS or wind data
final boolean result =
startOfLivePeriod != null &&
endOfLivePeriod != null &&
startOfLivePeriod.getTime() <= serverTimePointAsMillis &&
serverTimePointAsMillis <= endOfLivePeriod.getTime();
return result;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((boatClass == null) ? 0 : boatClass.hashCode());
result = prime * result + ((endOfRace == null) ? 0 : endOfRace.hashCode());
result = prime * result + (isTracked ? 1231 : 1237);
result = prime * result + ((places == null) ? 0 : places.hashCode());
result = prime * result + ((regattaName == null) ? 0 : regattaName.hashCode());
result = prime * result + ((startOfRace == null) ? 0 : startOfRace.hashCode());
result = prime * result + ((status == null) ? 0 : status.hashCode());
result = prime * result + ((trackedRace == null) ? 0 : trackedRace.hashCode());
return result;
}
@@ -146,11 +90,6 @@ public class RaceDTO extends NamedDTO {
return false;
} else if (!boatClass.equals(other.boatClass))
return false;
if (endOfRace == null) {
if (other.endOfRace != null)
return false;
} else if (!endOfRace.equals(other.endOfRace))
return false;
if (isTracked != other.isTracked)
return false;
if (places == null) {
@@ -163,21 +102,11 @@ public class RaceDTO extends NamedDTO {
return false;
} else if (!regattaName.equals(other.regattaName))
return false;
if (startOfRace == null) {
if (other.startOfRace != null)
return false;
} else if (!startOfRace.equals(other.startOfRace))
return false;
if (status == null) {
if (other.status != null)
return false;
} else if (!status.equals(other.status))
return false;
if (trackedRace == null) {
if (other.trackedRace != null)
return false;
} else if (!trackedRace.equals(other.trackedRace))
return false;
return true;
}
}