diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java @ 686:3dc61e00385e facets-slt

Merged with trunk and introduced hashing of computed values. flys-artifacts/branches/facets-slt@2126 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 15 Jun 2011 15:28:54 +0000
parents fdc898a134a7
children 06689035024c
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Wed Jun 08 13:03:21 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Wed Jun 15 15:28:54 2011 +0000
@@ -3,7 +3,7 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
+import java.util.TreeMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -12,6 +12,8 @@
 
 import gnu.trove.TDoubleArrayList;
 
+import net.sf.ehcache.Cache;
+
 import org.apache.log4j.Logger;
 
 import org.w3c.dom.Document;
@@ -40,6 +42,9 @@
 
 import de.intevation.flys.artifacts.context.FLYSContext;
 
+import de.intevation.flys.artifacts.cache.CacheFactory;
+
+import de.intevation.flys.artifacts.model.ComputeCallback;
 import de.intevation.flys.artifacts.model.DischargeTables;
 import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.artifacts.model.Segment;
@@ -59,6 +64,8 @@
     private static Logger logger = Logger.getLogger(FLYSArtifact.class);
 
 
+    public static final String COMPUTING_CACHE = "computed.values";
+
     /** The XPath that points to the input data elements of the FEED document.*/
     public static final String XPATH_FEED_INPUT =
         "/art:action/art:data/art:input";
@@ -98,7 +105,7 @@
      * The default constructor that creates an empty FLYSArtifact.
      */
     public FLYSArtifact() {
-        data             = new HashMap<String, StateData>();
+        data             = new TreeMap<String, StateData>();
         previousStateIds = new ArrayList<String>();
     }
 
@@ -1001,6 +1008,54 @@
         return DoubleUtil.explode(from, to, step);
     }
 
+
+    /**
+     * Computes the hash code of the entered values.
+     *
+     * @return a hash code.
+     */
+    @Override
+    public String hash() {
+        Set<Map.Entry<String, StateData>> entries = data.entrySet();
+
+        int hash  = 0;
+        int shift = 3;
+
+        for (Map.Entry<String, StateData> entry: entries) {
+            String key   = entry.getKey();
+            Object value = entry.getValue().getValue();
+
+            hash ^= (key.hashCode() << shift) | (value.hashCode() << 2 * shift);
+            shift += 2;
+        }
+
+        return getCurrentStateId() + hash;
+    }
+
+
+    public Object compute(String key, ComputeCallback callback) {
+        Cache cache = CacheFactory.getCache(COMPUTING_CACHE);
+
+        if (cache == null) {
+            return callback.compute();
+        }
+
+        net.sf.ehcache.Element element = cache.get(key);
+        if (element != null) {
+            logger.debug("Got computation values from cache.");
+            return element.getValue();
+        }
+
+        Object result = callback.compute();
+
+        if (result != null) {
+            cache.put(new net.sf.ehcache.Element(key, result));
+        }
+
+        return result;
+    }
+
+
     /**
      * Method to dump the artifacts state/data.
      */

http://dive4elements.wald.intevation.org