From b393636cb3f7e05ef8b34804eeb06357f1b9cfbe Mon Sep 17 00:00:00 2001 From: Cow Date: Tue, 28 Jul 2020 00:12:36 +0200 Subject: Renamed package to match cowtipper.de --- .../cowtipper/cowlection/handler/PlayerCache.java | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/main/java/de/cowtipper/cowlection/handler/PlayerCache.java (limited to 'src/main/java/de/cowtipper/cowlection/handler/PlayerCache.java') diff --git a/src/main/java/de/cowtipper/cowlection/handler/PlayerCache.java b/src/main/java/de/cowtipper/cowlection/handler/PlayerCache.java new file mode 100644 index 0000000..2206473 --- /dev/null +++ b/src/main/java/de/cowtipper/cowlection/handler/PlayerCache.java @@ -0,0 +1,47 @@ +package de.cowtipper.cowlection.handler; + +import com.google.common.collect.EvictingQueue; +import de.cowtipper.cowlection.Cowlection; + +import java.util.SortedSet; +import java.util.TreeSet; + +public class PlayerCache { + @SuppressWarnings("UnstableApiUsage") + private final EvictingQueue nameCache = EvictingQueue.create(50); + @SuppressWarnings("UnstableApiUsage") + private final EvictingQueue bestFriendCache = EvictingQueue.create(50); + private final Cowlection main; + + public PlayerCache(Cowlection main) { + this.main = main; + } + + public void add(String name) { + // remove old entry (if exists) to 'push' name to the end of the queue + nameCache.remove(name); + nameCache.add(name); + } + + public void addBestFriend(String name) { + // remove old entry (if exists) to 'push' name to the end of the queue + bestFriendCache.remove(name); + bestFriendCache.add(name); + } + + public void removeBestFriend(String name) { + bestFriendCache.remove(name); + } + + public SortedSet getAllNamesSorted() { + SortedSet nameList = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); + nameList.addAll(bestFriendCache); + nameList.addAll(nameCache); + return nameList; + } + + public void clearAllCaches() { + nameCache.clear(); + bestFriendCache.clear(); + } +} -- cgit