diff --git a/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/RaceCalculator.java b/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/RaceCalculator.java index dcb1ab5..f4d2fcf 100644 --- a/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/RaceCalculator.java +++ b/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/RaceCalculator.java @@ -5,6 +5,7 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; +import java.time.Duration; import java.time.Instant; import java.util.HashMap; import java.util.Map; @@ -12,49 +13,69 @@ 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 runnerRaceTimeMap = new HashMap<>(); + private final transient Map 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())); } } } diff --git a/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/RaceCalculatorApp.java b/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/RaceCalculatorApp.java index cfae436..2f37be1 100644 --- a/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/RaceCalculatorApp.java +++ b/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/RaceCalculatorApp.java @@ -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();