]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/NodeSupport.java
Update structure and value cache when refreshing variable
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / NodeSupport.java
1 package org.simantics.db.layer0.variable;
2
3 import java.util.concurrent.TimeUnit;
4
5 import org.simantics.databoard.binding.mutable.Variant;
6 import org.simantics.db.layer0.variable.Variables.NodeStructure;
7 import org.simantics.simulator.variable.NodeManager;
8 import org.simantics.simulator.variable.exceptions.NoValueException;
9 import org.simantics.simulator.variable.exceptions.NodeManagerException;
10
11 /**
12  * @author Antti Villberg
13  *
14  * @param <Node>
15  * @since 1.23
16  */
17 public class NodeSupport<Node> {
18
19         public final NodeManager<Node> manager;
20         public final NodeCache<Node,Variant> valueCache;
21         public final NodeCache<Node,NodeStructure> structureCache;
22
23         public NodeSupport(NodeManager<Node> manager) {
24                 this(manager, 1, TimeUnit.SECONDS);
25         }
26
27         public NodeSupport(NodeManager<Node> manager, long defaultExpirationTime, TimeUnit expirationTimeUnit) {
28                 if (manager == null)
29                         throw new NullPointerException("null NodeManager");
30                 long ns = expirationTimeUnit.toNanos(defaultExpirationTime);
31                 this.manager = manager;
32                 this.valueCache = new NodeCache<>(ns);
33                 this.structureCache = new NodeCache<>(ns);
34         }
35
36         @Override
37         public int hashCode() {
38                 final int prime = 31;
39                 int result = 1;
40                 result = prime * result + valueCache.hashCode();
41                 result = prime * result + structureCache.hashCode();
42                 result = prime * result + manager.hashCode();
43                 return result;
44         }
45
46         @Override
47         public boolean equals(Object obj) {
48                 if (this == obj)
49                         return true;
50                 if (obj == null)
51                         return false;
52                 if (getClass() != obj.getClass())
53                         return false;
54                 NodeSupport<?> other = (NodeSupport<?>) obj;
55                 return valueCache.equals(other.valueCache)
56                                 && structureCache.equals(other.structureCache)
57                                 && manager.equals(other.manager);
58         }
59
60         public void refreshCache(Node node) throws NodeManagerException {
61                 VariableNode<Node> variableNode = new VariableNode<>(this, node);
62                 NodeStructure ns = NodeStructureRequest.get(variableNode);
63                 structureCache.put(node, ns);
64                 try {
65                         Variant value = manager.getValue(node);
66                         valueCache.put(node, value);
67                 } catch (NoValueException e) {
68                         valueCache.remove(node);
69                 };
70         }
71
72         public void dispose() {
73                 valueCache.dispose();
74                 structureCache.dispose();
75         }
76
77 }