Skip to content

Commit

Permalink
updated race example
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-higgins committed Mar 8, 2024
1 parent 9aa26b7 commit 03fe89b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,77 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

public class RaceCalculator {

public record RunnerStarted(long runnerId, Instant startTime) {
RunnerStarted(long runnerId, String time) {
this(runnerId, Instant.parse(time));
}
}

public record RunnerFinished(long runnerId, Instant finishTime) {
RunnerFinished(long runnerId, String time) {
this(runnerId, Instant.parse(time));
}
}

public interface ResultsPublisher {
void publishAllResults();
}

private record RunningRecord(Instant startTime, Instant finishTime) {
RunningRecord(Instant startTime) {
this(startTime, startTime);
}

public String runDuration() {
Duration duration = Duration.between(startTime, finishTime);
return duration.toHoursPart() + ":" + duration.toMinutesPart() + ":" + duration.toSecondsPart();
}
}

@Getter
public static class RaceTimeTracker {

private final transient Map<Long, Long> runnerRaceTimeMap = new HashMap<>();
private final transient Map<Long, RunningRecord> raceTimeMap = new HashMap<>();

@OnEventHandler(propagate = false)
public boolean runnerStarted(RunnerStarted runnerStarted) {
//add runner start time to map
raceTimeMap.put(runnerStarted.runnerId(), new RunningRecord(runnerStarted.startTime()));
return false;
}

@OnEventHandler
public boolean runnerFinished(RunnerFinished runnerFinished) {
//calc runner total race time and add to map
public boolean runnerFinished(RunnerFinished runner) {
raceTimeMap.computeIfPresent(
runner.runnerId(),
(id, startRecord) -> new RunningRecord(startRecord.startTime(), runner.finishTime()));
return true;
}
}

@RequiredArgsConstructor
public static class ResultsPublisherImpl implements @ExportService ResultsPublisher{
public static class ResultsPublisherImpl implements @ExportService ResultsPublisher {

private final RaceTimeTracker raceTimeTracker;

@OnEventHandler(propagate = false)
public boolean runnerFinished(RunnerFinished runnerFinished) {
//get the runner race time and send individual their results
long raceTime = raceTimeTracker.getRunnerRaceTimeMap().get(runnerFinished.runnerId());
public boolean runnerFinished(RunnerFinished runner) {
var raceTime = raceTimeTracker.getRaceTimeMap().get(runner.runnerId());
System.out.format("Crossed the line runner:%d time:%s%n", runner.runnerId(), raceTime.runDuration());
return false;
}

@Override
public void publishAllResults() {
//get all results and publish
var runnerRaceTimeMap = raceTimeTracker.getRunnerRaceTimeMap();
System.out.println("FINAL RESULTS");
raceTimeTracker.getRaceTimeMap().forEach((l, r) ->
System.out.println("id:" + l + " final time:" + r.runDuration()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@

public class RaceCalculatorApp {
public static void main(String[] args) {
RaceCalculatorProcessor raceCalculatorProcessor = new RaceCalculatorProcessor();
raceCalculatorProcessor.init();
RaceCalculatorProcessor raceCalculator = new RaceCalculatorProcessor();
raceCalculator.init();

ResultsPublisher resultsPublisher = raceCalculatorProcessor.getExportedService();
ResultsPublisher resultsPublisher = raceCalculator.getExportedService();

//connect to event stream and process runner timing events
raceCalculatorProcessor.onEvent(new RunnerStarted(1, Instant.now()));
raceCalculatorProcessor.onEvent(new RunnerStarted(2, Instant.now()));
raceCalculatorProcessor.onEvent(new RunnerStarted(3, Instant.now()));
raceCalculator.onEvent(new RunnerStarted(1, "2019-02-14T09:00:00Z"));
raceCalculator.onEvent(new RunnerStarted(2, "2019-02-14T09:02:10Z"));
raceCalculator.onEvent(new RunnerStarted(3, "2019-02-14T09:06:22Z"));

raceCalculatorProcessor.onEvent(new RunnerFinished(2, Instant.now()));
raceCalculatorProcessor.onEvent(new RunnerFinished(3, Instant.now()));
raceCalculatorProcessor.onEvent(new RunnerFinished(1, Instant.now()));
raceCalculator.onEvent(new RunnerFinished(2, "2019-02-14T10:32:15Z"));
raceCalculator.onEvent(new RunnerFinished(3, "2019-02-14T10:59:10Z"));
raceCalculator.onEvent(new RunnerFinished(1, "2019-02-14T11:14:32Z"));

//publish full results
resultsPublisher.publishAllResults();
Expand Down

0 comments on commit 03fe89b

Please sign in to comment.