diff options
author | Marko Živković <[email protected]> | 2015-03-03 00:51:20 +0000 |
---|---|---|
committer | Marko Živković <[email protected]> | 2015-03-03 00:51:20 +0000 |
commit | 439a646b5edfde260d4b14821e6d7c18f819c7d7 (patch) | |
tree | f97b3d7d9aec96fc60499eff5b8781e62c7c2295 /logo/src/xlogo/storage/cache/ObjectCache.java | |
parent | cff1ce418df126fc68bf4046c3becd9fadea6872 (diff) |
add ObjectCache for StorableObjects
git-svn-id: https://svn.code.sf.net/p/xlogo4schools/svn/trunk@32 3b0d7934-f7ef-4143-9606-b51f2e2281fd
Diffstat (limited to 'logo/src/xlogo/storage/cache/ObjectCache.java')
-rw-r--r-- | logo/src/xlogo/storage/cache/ObjectCache.java | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/logo/src/xlogo/storage/cache/ObjectCache.java b/logo/src/xlogo/storage/cache/ObjectCache.java new file mode 100644 index 0000000..0fc6a30 --- /dev/null +++ b/logo/src/xlogo/storage/cache/ObjectCache.java @@ -0,0 +1,91 @@ +package xlogo.storage.cache;
+
+import java.io.File;
+import java.util.HashMap;
+
+import xlogo.interfaces.Observable;
+import xlogo.storage.StorableObject;
+
+/**
+ * Cache {@link StorableObject} based on {@link StorableObject#getFilePath()}
+ * @author Marko
+ *
+ */
+public class ObjectCache {
+
+ /*
+ * Singleton
+ */
+
+ private static ObjectCache instance;
+
+ public static final ObjectCache getInstance(){
+ if(instance == null){
+ instance = new ObjectCache();
+ }
+ return instance;
+ }
+
+ /*
+ * Cache fields
+ */
+
+ /**
+ * Class of T -> Map({@link StorableObject#getFilePath()} -> {@link StorableObject} of T)
+ */
+ private HashMap<Class<?>,HashMap<File,StorableObject<?,?>>> cache;
+
+ private ObjectCache(){
+ cache = new HashMap<Class<?>, HashMap<File,StorableObject<?,?>>>();
+ }
+
+ /**
+ * Cache {@link StorableObject} based on {@link StorableObject#getFilePath()}
+ * @param storable
+ */
+ public <T extends Observable<?>> void cache(StorableObject<T,?> storable){
+ if (storable == null || storable.get() == null){
+ // TODO log warning
+ return;
+ }
+ Class<?> c = storable.get().getClass();
+ HashMap<File, StorableObject<?,?>> classCache = cache.get(c);
+ if (classCache == null){
+ classCache = new HashMap<File, StorableObject<?, ?>>();
+ cache.put(c, classCache);
+ }
+ classCache.put(storable.getFilePath(), storable);
+ }
+
+ /**
+ *
+ * @param file
+ * @param c
+ * @return
+ */
+ @SuppressWarnings("unchecked") // implementation of cache guarantees that the cast succeeds
+ public <T extends Observable<E>,E extends Enum<E>> StorableObject<T, E> get(File file, Class<T> c){
+ HashMap<File, StorableObject<?, ?>> classCache = cache.get(c);
+ if (classCache == null){
+ return null;
+ }
+ return (StorableObject<T, E>) classCache.get(file);
+ }
+
+ public <T extends Observable<?>> void remove(StorableObject<T,?> storable){
+ if (storable == null || storable.get() == null){
+ return;
+ }
+ Class<?> c = storable.get().getClass();
+ remove(storable.getFilePath(), c);
+ }
+
+ public void remove(File file, Class<?> c){
+ HashMap<File, StorableObject<?,?>> classCache = cache.get(c);
+ if (classCache == null){
+ return;
+ }
+ classCache.remove(file);
+ }
+
+}
|