comparison 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
comparison
equal deleted inserted replaced
667:434146596838 686:3dc61e00385e
1 package de.intevation.flys.artifacts; 1 package de.intevation.flys.artifacts;
2 2
3 import java.util.ArrayList; 3 import java.util.ArrayList;
4 import java.util.Collection; 4 import java.util.Collection;
5 import java.util.Collections; 5 import java.util.Collections;
6 import java.util.HashMap; 6 import java.util.TreeMap;
7 import java.util.List; 7 import java.util.List;
8 import java.util.Map; 8 import java.util.Map;
9 import java.util.Set; 9 import java.util.Set;
10 10
11 import javax.xml.xpath.XPathConstants; 11 import javax.xml.xpath.XPathConstants;
12 12
13 import gnu.trove.TDoubleArrayList; 13 import gnu.trove.TDoubleArrayList;
14
15 import net.sf.ehcache.Cache;
14 16
15 import org.apache.log4j.Logger; 17 import org.apache.log4j.Logger;
16 18
17 import org.w3c.dom.Document; 19 import org.w3c.dom.Document;
18 import org.w3c.dom.Element; 20 import org.w3c.dom.Element;
38 import de.intevation.flys.model.Range; 40 import de.intevation.flys.model.Range;
39 import de.intevation.flys.model.River; 41 import de.intevation.flys.model.River;
40 42
41 import de.intevation.flys.artifacts.context.FLYSContext; 43 import de.intevation.flys.artifacts.context.FLYSContext;
42 44
45 import de.intevation.flys.artifacts.cache.CacheFactory;
46
47 import de.intevation.flys.artifacts.model.ComputeCallback;
43 import de.intevation.flys.artifacts.model.DischargeTables; 48 import de.intevation.flys.artifacts.model.DischargeTables;
44 import de.intevation.flys.artifacts.model.RiverFactory; 49 import de.intevation.flys.artifacts.model.RiverFactory;
45 import de.intevation.flys.artifacts.model.Segment; 50 import de.intevation.flys.artifacts.model.Segment;
46 51
47 import de.intevation.flys.artifacts.states.DefaultState; 52 import de.intevation.flys.artifacts.states.DefaultState;
57 62
58 /** The logger that is used in this artifact.*/ 63 /** The logger that is used in this artifact.*/
59 private static Logger logger = Logger.getLogger(FLYSArtifact.class); 64 private static Logger logger = Logger.getLogger(FLYSArtifact.class);
60 65
61 66
67 public static final String COMPUTING_CACHE = "computed.values";
68
62 /** The XPath that points to the input data elements of the FEED document.*/ 69 /** The XPath that points to the input data elements of the FEED document.*/
63 public static final String XPATH_FEED_INPUT = 70 public static final String XPATH_FEED_INPUT =
64 "/art:action/art:data/art:input"; 71 "/art:action/art:data/art:input";
65 72
66 /** The XPath that points to the name of the target state of ADVANCE.*/ 73 /** The XPath that points to the name of the target state of ADVANCE.*/
96 103
97 /** 104 /**
98 * The default constructor that creates an empty FLYSArtifact. 105 * The default constructor that creates an empty FLYSArtifact.
99 */ 106 */
100 public FLYSArtifact() { 107 public FLYSArtifact() {
101 data = new HashMap<String, StateData>(); 108 data = new TreeMap<String, StateData>();
102 previousStateIds = new ArrayList<String>(); 109 previousStateIds = new ArrayList<String>();
103 } 110 }
104 111
105 112
106 /** 113 /**
999 double step 1006 double step
1000 ) { 1007 ) {
1001 return DoubleUtil.explode(from, to, step); 1008 return DoubleUtil.explode(from, to, step);
1002 } 1009 }
1003 1010
1011
1012 /**
1013 * Computes the hash code of the entered values.
1014 *
1015 * @return a hash code.
1016 */
1017 @Override
1018 public String hash() {
1019 Set<Map.Entry<String, StateData>> entries = data.entrySet();
1020
1021 int hash = 0;
1022 int shift = 3;
1023
1024 for (Map.Entry<String, StateData> entry: entries) {
1025 String key = entry.getKey();
1026 Object value = entry.getValue().getValue();
1027
1028 hash ^= (key.hashCode() << shift) | (value.hashCode() << 2 * shift);
1029 shift += 2;
1030 }
1031
1032 return getCurrentStateId() + hash;
1033 }
1034
1035
1036 public Object compute(String key, ComputeCallback callback) {
1037 Cache cache = CacheFactory.getCache(COMPUTING_CACHE);
1038
1039 if (cache == null) {
1040 return callback.compute();
1041 }
1042
1043 net.sf.ehcache.Element element = cache.get(key);
1044 if (element != null) {
1045 logger.debug("Got computation values from cache.");
1046 return element.getValue();
1047 }
1048
1049 Object result = callback.compute();
1050
1051 if (result != null) {
1052 cache.put(new net.sf.ehcache.Element(key, result));
1053 }
1054
1055 return result;
1056 }
1057
1058
1004 /** 1059 /**
1005 * Method to dump the artifacts state/data. 1060 * Method to dump the artifacts state/data.
1006 */ 1061 */
1007 protected void dumpArtifact() { 1062 protected void dumpArtifact() {
1008 if (logger.isDebugEnabled()) { 1063 if (logger.isDebugEnabled()) {

http://dive4elements.wald.intevation.org