X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FShellCache.java;h=fd3d6bc31e4860abd6b1084efc68b59f2aca878b;hb=d2eb4eaaec62d531bdde974c1b9fa5de8b936259;hp=9da9a06093252509bf3aabd3474a113358bcc3a1;hpb=9a727406f8647dffc6598ed11b7cc7bef82b38c5;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/data/ShellCache.java b/src/main/java/net/pterodactylus/sone/data/ShellCache.java index 9da9a06..fd3d6bc 100644 --- a/src/main/java/net/pterodactylus/sone/data/ShellCache.java +++ b/src/main/java/net/pterodactylus/sone/data/ShellCache.java @@ -17,6 +17,7 @@ package net.pterodactylus.sone.data; +import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -31,7 +32,27 @@ import java.util.Map; public class ShellCache { /** The object cache. */ - private final Map cache = new HashMap(); + private final Map objectCache = new HashMap(); + + /** The shell cache. */ + private final Map> shellCache = new HashMap>(); + + /** The shell creator. */ + private final ShellCreator shellCreator; + + /** + * Creates a new shell cache. + * + * @param shellCreator + * The creator for new shells + */ + public ShellCache(ShellCreator shellCreator) { + this.shellCreator = shellCreator; + } + + // + // ACCESSORS + // /** * Stores the given object in this cache. If the given object is not a @@ -43,9 +64,13 @@ public class ShellCache { * @param object * The object to store */ + @SuppressWarnings("unchecked") public void put(String id, T object) { - if (!(object instanceof Shell) || !cache.containsKey(id)) { - cache.put(id, object); + if (!(object instanceof Shell)) { + objectCache.put(id, object); + shellCache.remove(id); + } else { + shellCache.put(id, (Shell) object); } } @@ -58,7 +83,21 @@ public class ShellCache { * with the given ID */ public T get(String id) { - return cache.get(id); + if (!objectCache.containsKey(id)) { + Shell shell = shellCreator.createShell(id); + shellCache.put(id, shell); + return shell.getShelled(); + } + return objectCache.get(id); + } + + /** + * Returns all cached shells. + * + * @return All cached shells + */ + public Collection> getShells() { + return shellCache.values(); } }