From e90198249f9379ca86c01274bb4b19de5f877fa7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 14 Oct 2010 14:43:03 +0200 Subject: [PATCH] Rewrite shell cache to create shells, if necessary. --- .../net/pterodactylus/sone/data/ShellCache.java | 47 ++++++++++++++++++++-- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/data/ShellCache.java b/src/main/java/net/pterodactylus/sone/data/ShellCache.java index 9da9a06..0b19429 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(); + 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(); } } -- 2.7.4