cache

added support to null objects

6/25/2019 3:43:00 AM

Details

diff --git a/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java b/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java
index 6c225c1..7fdbea5 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java
@@ -6,6 +6,7 @@
 package br.ufrgs.inf.prosoft.cache;
 
 import java.util.HashMap;
+import java.util.Optional;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -18,7 +19,7 @@ import java.util.logging.Logger;
 public class MultiCache<K, V> implements Cache<K, V> {
 
     private static final boolean CACHE_REGISTER_SIZE = System.getenv("CACHE_REGISTER_SIZE") != null && System.getenv("CACHE_REGISTER_SIZE").equals("true");
-    private final HashMap<K, V> map;
+    private final HashMap<K, Optional<V>> map;
     private final CachePerformance cachingPerformance;
     private static final Logger LOGGER = Logger.getLogger(Cache.class.getName());
 
@@ -56,39 +57,49 @@ public class MultiCache<K, V> implements Cache<K, V> {
     @Override
     public void put(K key, V value) {
         invalidate(key);
-        this.map.put(key, value);
+        Optional<V> optional = value != null ? Optional.of(value) : Optional.empty();
+        this.map.put(key, optional);
+        String identifier = getIdentifier(value);
         if (CACHE_REGISTER_SIZE) {
-            this.cachingPerformance.registerEvent(EventType.ADDITION, String.valueOf(value.hashCode()), CachePerformance.calculateObjectSize(value));
+            this.cachingPerformance.registerEvent(EventType.ADDITION, identifier, CachePerformance.calculateObjectSize(value));
         } else {
-            this.cachingPerformance.registerEvent(EventType.ADDITION, String.valueOf(value.hashCode()));
+            this.cachingPerformance.registerEvent(EventType.ADDITION, identifier);
         }
         this.cachingPerformance.registerSize(this.map.size());
     }
 
     @Override
     public V get(K key) {
-        V get = this.map.get(key);
-        if (get == null) {
+        Optional<V> optional = this.map.get(key);
+        if (optional == null) {
             this.cachingPerformance.registerEvent(EventType.MISS);
+            return null;
+        }
+        V get = optional.orElse(null);
+        String identifier = getIdentifier(get);
+        if (CACHE_REGISTER_SIZE) {
+            this.cachingPerformance.registerEvent(EventType.HIT, identifier, CachePerformance.calculateObjectSize(get));
         } else {
-            if (CACHE_REGISTER_SIZE) {
-                this.cachingPerformance.registerEvent(EventType.HIT, String.valueOf(get.hashCode()), CachePerformance.calculateObjectSize(get));
-            } else {
-                this.cachingPerformance.registerEvent(EventType.HIT, String.valueOf(get.hashCode()));
-            }
+            this.cachingPerformance.registerEvent(EventType.HIT, identifier);
         }
         return get;
     }
 
     @Override
     public void invalidate(K key) {
-        V remove = this.map.remove(key);
-        if (remove != null) {
+        Optional<V> optional = this.map.remove(key);
+        if (optional != null) {
+            V remove = optional.orElse(null);
+            String identifier = getIdentifier(remove);
             if (CACHE_REGISTER_SIZE) {
-                this.cachingPerformance.registerEvent(EventType.INVALIDATION, String.valueOf(remove.hashCode()), CachePerformance.calculateObjectSize(remove));
+                this.cachingPerformance.registerEvent(EventType.INVALIDATION, identifier, CachePerformance.calculateObjectSize(remove));
             } else {
-                this.cachingPerformance.registerEvent(EventType.INVALIDATION, String.valueOf(remove.hashCode()));
+                this.cachingPerformance.registerEvent(EventType.INVALIDATION, identifier);
             }
         }
     }
+
+    private String getIdentifier(V value) {
+        return value != null ? String.valueOf(value.hashCode()) : "null";
+    }
 }