mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-16 09:11:02 +00:00
added method for revoking events to racelog
This commit is contained in:
1 parent
13a1776cc0
commit
3524639080
5 files changed
+95
-21
No files matched your search
+9
@@ -6,6 +6,7 @@ import java.util.NavigableSet;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.sap.sailing.domain.common.WithID;
|
||||
import com.sap.sailing.domain.common.racelog.tracking.NotRevokableException;
|
||||
import com.sap.sailing.domain.racelog.impl.RaceLogEventComparator;
|
||||
import com.sap.sailing.domain.tracking.Track;
|
||||
|
||||
@@ -117,6 +118,14 @@ public interface RaceLog extends Track<RaceLogEvent>, WithID {
|
||||
* @return
|
||||
*/
|
||||
NavigableSet<RaceLogEvent> getUnrevokedEventsDescending();
|
||||
|
||||
/**
|
||||
* Inserts a {@link RevokeEvent} for {@code toRevoke}, if latter exists in the racelog
|
||||
* and has not yet been revoked.
|
||||
*
|
||||
* @param author The author for the {@code RevokeEvent}.
|
||||
*/
|
||||
void revokeEvent(RaceLogEventAuthor author, Revokable toRevoke) throws NotRevokableException;
|
||||
|
||||
/**
|
||||
* Merges all events from the <code>other</code> race log into this.
|
||||
|
||||
+1
-1
@@ -13,6 +13,6 @@ package com.sap.sailing.domain.racelog;
|
||||
* @author Fredrik Teschke
|
||||
*
|
||||
*/
|
||||
public interface Revokable {
|
||||
public interface Revokable extends RaceLogEvent {
|
||||
|
||||
}
|
||||
+8
@@ -11,9 +11,12 @@ import java.util.logging.Logger;
|
||||
|
||||
import com.sap.sailing.domain.common.Duration;
|
||||
import com.sap.sailing.domain.common.TimePoint;
|
||||
import com.sap.sailing.domain.common.racelog.tracking.NotRevokableException;
|
||||
import com.sap.sailing.domain.racelog.RaceLog;
|
||||
import com.sap.sailing.domain.racelog.RaceLogEvent;
|
||||
import com.sap.sailing.domain.racelog.RaceLogEventAuthor;
|
||||
import com.sap.sailing.domain.racelog.RaceLogEventVisitor;
|
||||
import com.sap.sailing.domain.racelog.Revokable;
|
||||
|
||||
/**
|
||||
* Wrapper for a {@link RaceLog} which will ignore all calls trying to add an {@link RaceLogEvent}. All other
|
||||
@@ -230,4 +233,9 @@ public class NoAddingRaceLogWrapper implements RaceLog {
|
||||
public void merge(RaceLog other) {
|
||||
innerRaceLog.merge(other);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void revokeEvent(RaceLogEventAuthor author, Revokable toRevoke) throws NotRevokableException {
|
||||
innerRaceLog.revokeEvent(author, toRevoke);
|
||||
}
|
||||
}
|
||||
+44
-20
@@ -15,8 +15,12 @@ import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.sap.sailing.domain.base.Timed;
|
||||
import com.sap.sailing.domain.common.impl.MillisecondsTimePoint;
|
||||
import com.sap.sailing.domain.common.racelog.tracking.NotRevokableException;
|
||||
import com.sap.sailing.domain.racelog.RaceLog;
|
||||
import com.sap.sailing.domain.racelog.RaceLogEvent;
|
||||
import com.sap.sailing.domain.racelog.RaceLogEventAuthor;
|
||||
import com.sap.sailing.domain.racelog.RaceLogEventFactory;
|
||||
import com.sap.sailing.domain.racelog.RaceLogEventVisitor;
|
||||
import com.sap.sailing.domain.racelog.Revokable;
|
||||
import com.sap.sailing.domain.racelog.RevokeEvent;
|
||||
@@ -147,28 +151,37 @@ public class RaceLogImpl extends TrackImpl<RaceLogEvent> implements RaceLog {
|
||||
private void revokeIfNecessary(RaceLogEvent newEvent) {
|
||||
if (newEvent instanceof RevokeEvent) {
|
||||
RevokeEvent revokeEvent = (RevokeEvent) newEvent;
|
||||
lockForRead();
|
||||
RaceLogEvent revokedEvent = getEventById(revokeEvent.getRevokedEventId());
|
||||
unlockAfterRead();
|
||||
try {
|
||||
checkIfSuccessfullyRevokes(revokeEvent);
|
||||
|
||||
if (revokedEvent == null) {
|
||||
logger.log(Level.FINE, "RevokeEvent added, that refers to non-existent event to be revoked");
|
||||
return;
|
||||
lockForWrite();
|
||||
revokedEventIds.add(revokeEvent.getRevokedEventId());
|
||||
unlockAfterWrite();
|
||||
} catch (NotRevokableException e) {
|
||||
logger.log(Level.WARNING, e.getMessage());
|
||||
}
|
||||
|
||||
if (! (revokedEvent instanceof Revokable)) {
|
||||
logger.log(Level.FINE, "RevokeEvent trying to revoke non-revokable event");
|
||||
return;
|
||||
}
|
||||
|
||||
if (getInternalRawFixes().comparator().compare(revokeEvent, revokedEvent) <= 0) {
|
||||
logger.log(Level.FINE, "RevokeEvent does not have sufficient priority");
|
||||
return;
|
||||
}
|
||||
|
||||
lockForWrite();
|
||||
revokedEventIds.add(revokeEvent.getRevokedEventId());
|
||||
unlockAfterWrite();
|
||||
}
|
||||
}
|
||||
|
||||
private void checkIfSuccessfullyRevokes(RevokeEvent revokeEvent) throws NotRevokableException {
|
||||
lockForRead();
|
||||
RaceLogEvent revokedEvent = getEventById(revokeEvent.getRevokedEventId());
|
||||
unlockAfterRead();
|
||||
|
||||
if (revokedEvent == null) {
|
||||
throw new NotRevokableException("RevokeEvent added, that refers to non-existent event to be revoked");
|
||||
}
|
||||
|
||||
if (revokedEventIds.contains(revokedEvent.getId())) {
|
||||
throw new NotRevokableException("Event has already been revoked");
|
||||
}
|
||||
|
||||
if (! (revokedEvent instanceof Revokable)) {
|
||||
throw new NotRevokableException("RevokeEvent trying to revoke non-revokable event");
|
||||
}
|
||||
|
||||
if (getInternalRawFixes().comparator().compare(revokeEvent, revokedEvent) <= 0) {
|
||||
throw new NotRevokableException("RevokeEvent does not have sufficient priority");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -387,4 +400,15 @@ public class RaceLogImpl extends TrackImpl<RaceLogEvent> implements RaceLog {
|
||||
unlockAfterWrite();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void revokeEvent(RaceLogEventAuthor author, Revokable toRevoke) throws NotRevokableException {
|
||||
if (toRevoke == null) {
|
||||
throw new NotRevokableException("Received null as event to revoke");
|
||||
}
|
||||
RevokeEvent revokeEvent = RaceLogEventFactory.INSTANCE.createRevokeEvent(MillisecondsTimePoint.now(), author,
|
||||
getCurrentPassId(), toRevoke.getId());
|
||||
checkIfSuccessfullyRevokes(revokeEvent);
|
||||
add(revokeEvent);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.sap.sailing.domain.racelog.tracking.analyzing.impl;
|
||||
|
||||
import com.sap.sailing.domain.racelog.RaceLog;
|
||||
import com.sap.sailing.domain.racelog.RaceLogEvent;
|
||||
import com.sap.sailing.domain.racelog.analyzing.impl.RaceLogAnalyzer;
|
||||
|
||||
/**
|
||||
* Finds the most recent unrevoked event in the racelog which is an {@code instanceof} {@link #ofType}.
|
||||
* @author Fredrik Teschke
|
||||
*
|
||||
*/
|
||||
public class LastEventOfTypeFinder extends RaceLogAnalyzer<RaceLogEvent> {
|
||||
private final Class<?> ofType;
|
||||
private final boolean onlyUnrevoked;
|
||||
|
||||
public LastEventOfTypeFinder(RaceLog raceLog, boolean onlyUnrevoked, Class<?> ofType) {
|
||||
super(raceLog);
|
||||
this.ofType = ofType;
|
||||
this.onlyUnrevoked = onlyUnrevoked;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RaceLogEvent performAnalysis() {
|
||||
Iterable<RaceLogEvent> set = onlyUnrevoked ? raceLog.getUnrevokedEventsDescending() : getAllEventsDescending();
|
||||
for (RaceLogEvent event : set) {
|
||||
if (ofType.isAssignableFrom(event.getClass())) {
|
||||
return event;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user