1 package org.simantics.db.layer0.variable;
3 import java.util.concurrent.TimeUnit;
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;
12 * @author Antti Villberg
17 public class NodeSupport<Node> {
19 public final NodeManager<Node> manager;
20 public final NodeCache<Node,Variant> valueCache;
21 public final NodeCache<Node,NodeStructure> structureCache;
23 public NodeSupport(NodeManager<Node> manager) {
24 this(manager, 1, TimeUnit.SECONDS);
27 public NodeSupport(NodeManager<Node> manager, long defaultExpirationTime, TimeUnit expirationTimeUnit) {
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);
37 public int hashCode() {
40 result = prime * result + valueCache.hashCode();
41 result = prime * result + structureCache.hashCode();
42 result = prime * result + manager.hashCode();
47 public boolean equals(Object obj) {
52 if (getClass() != obj.getClass())
54 NodeSupport<?> other = (NodeSupport<?>) obj;
55 return valueCache.equals(other.valueCache)
56 && structureCache.equals(other.structureCache)
57 && manager.equals(other.manager);
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);
65 Variant value = manager.getValue(node);
66 valueCache.put(node, value);
67 } catch (NoValueException e) {
68 valueCache.remove(node);
72 public void dispose() {
74 structureCache.dispose();