From 84150aadd2ee675e8c0a6a736d806d3c9ab1e23f Mon Sep 17 00:00:00 2001 From: D4rkr34lm Date: Mon, 6 Nov 2023 01:20:59 +0100 Subject: [PATCH] Added getters to TraceManager --- .../bausystem/features/tracer2/Trace.java | 2 + .../features/tracer2/TraceManager.java | 44 +++++++++++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Trace.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Trace.java index aec31127..69bda8ec 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Trace.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Trace.java @@ -20,6 +20,7 @@ package de.steamwar.bausystem.features.tracer2; import de.steamwar.bausystem.region.Region; +import lombok.Getter; import java.util.HashSet; import java.util.Set; @@ -28,6 +29,7 @@ public class Trace { /** * Region this trace has been recorded in */ + @Getter private final Region region; /** diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceManager.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceManager.java index bcce47be..47f8778d 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceManager.java @@ -19,11 +19,20 @@ package de.steamwar.bausystem.features.tracer2; -import java.util.HashMap; -import java.util.Map; +import de.steamwar.bausystem.region.Region; + +import java.util.*; +import java.util.stream.Collectors; public class TraceManager { + /** + * Map of all current traces + */ private final Map traces = new HashMap<>(); + + /** + * Utility variable to keep track of the next open trace id + */ private int currOpenId = 0; /** Adds a new trace to the global record @@ -31,7 +40,7 @@ public class TraceManager { * @param trace Trace to be added * @return id of the newly added trace */ - public int add(Trace trace){ + protected int add(Trace trace){ int id = currOpenId; traces.put(id, trace); currOpenId++; @@ -53,4 +62,33 @@ public class TraceManager { traces.clear(); currOpenId = 1; } + + /** Methode to get all traces in a certain region + * + * @param region Region to look for traces in + * @return All traces recorded in the given Region + */ + public Set get(Region region){ + return traces.values() + .stream() + .filter((Trace trace) -> trace.getRegion() == region) + .collect(Collectors.toSet()); + } + + /** Methode to get the trace with specific id + * + * @param id id of the trace + * @return the trace with given id or null if no trace with id is found + */ + public Trace get(int id){ + return traces.getOrDefault(id, null); + } + + /** Methode to get all traces + * + * @return fresh set of all current traces + */ + public Set getAll(){ + return new HashSet<>(traces.values()); + } }