aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/handler/PlayerCache.java
blob: 2206473418b0538f4c6dc344f7f2439a9b63b2f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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<String> nameCache = EvictingQueue.create(50);
    @SuppressWarnings("UnstableApiUsage")
    private final EvictingQueue<String> 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<String> getAllNamesSorted() {
        SortedSet<String> nameList = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
        nameList.addAll(bestFriendCache);
        nameList.addAll(nameCache);
        return nameList;
    }

    public void clearAllCaches() {
        nameCache.clear();
        bestFriendCache.clear();
    }
}