diff --git a/src/main/java/org/cryptomator/common/EventMap.java b/src/main/java/org/cryptomator/common/EventMap.java index f34f592a5..22f07bde7 100644 --- a/src/main/java/org/cryptomator/common/EventMap.java +++ b/src/main/java/org/cryptomator/common/EventMap.java @@ -18,16 +18,23 @@ import javafx.collections.MapChangeListener; import javafx.collections.ObservableMap; import java.nio.file.Path; import java.util.Collection; +import java.util.Comparator; import java.util.Map; import java.util.Set; /** * Map containing {@link VaultEvent}s. * The map is keyed by the ciphertext path of the affected resource _and_ the {@link FilesystemEvent}s class in order to group same events + *

+ * Use {@link EventMap#put(VaultEvent)} to add an element and {@link EventMap#remove(VaultEvent)} to remove it. + *

+ * The map is size restricted to {@value MAX_SIZE} elements. If a _new_ element (i.e. not already present) is added, the least recently added is removed. */ @Singleton public class EventMap implements ObservableMap { + private static final int MAX_SIZE = 300; + public record EventKey(Path ciphertextPath, Class c) {} private final ObservableMap delegate; @@ -123,6 +130,11 @@ public class EventMap implements ObservableMap { //if-else var nullOrEntry = delegate.get(key); if (nullOrEntry == null) { + if (size() == MAX_SIZE) { + delegate.entrySet().stream() // + .min(Comparator.comparing(entry -> entry.getValue().timestamp())) // + .ifPresent(oldestEntry -> delegate.remove(oldestEntry.getKey())); + } delegate.put(key, e); } else { delegate.put(key, nullOrEntry.incrementCount(e.timestamp()));