X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FShellCache.java;h=7e28285ef785afbcbe7fb67b8e9de2a610786a32;hb=6a0938d8f6fdf9dbc45f03384a3cd83efeef022c;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..7e28285 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 @@ -42,11 +63,20 @@ public class ShellCache { * The ID of the object * @param object * The object to store + * @return The real object, or the shell if there is no real object yet */ - public void put(String id, T object) { - if (!(object instanceof Shell) || !cache.containsKey(id)) { - cache.put(id, object); + @SuppressWarnings("unchecked") + public T put(String id, T object) { + if (!(object instanceof Shell)) { + objectCache.put(id, object); + shellCache.remove(id); + return object; } + if (objectCache.containsKey(id)) { + return objectCache.get(id); + } + shellCache.put(id, (Shell) object); + return object; } /** @@ -58,7 +88,23 @@ public class ShellCache { * with the given ID */ public T get(String id) { - return cache.get(id); + if (!objectCache.containsKey(id)) { + if (!shellCache.containsKey(id)) { + Shell shell = shellCreator.createShell(id); + shellCache.put(id, shell); + } + return shellCache.get(id).getShelled(); + } + return objectCache.get(id); + } + + /** + * Returns all cached shells. + * + * @return All cached shells + */ + public Collection> getShells() { + return shellCache.values(); } }