]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/SCLValueAccessor.java
Initial support for concurrency in databoard, bindings and serializers
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / SCLValueAccessor.java
1 package org.simantics.db.layer0.variable;
2
3 import org.simantics.databoard.binding.Binding;
4 import org.simantics.databoard.type.Datatype;
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.WriteGraph;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.scl.runtime.SCLContext;
9 import org.simantics.scl.runtime.function.Function1;
10 import org.simantics.scl.runtime.function.Function2;
11 import org.simantics.scl.runtime.function.Function3;
12
13 public class SCLValueAccessor implements ValueAccessor {
14
15         private final Function1<Variable,Object> getValue1;
16         private final Function2<Variable,Binding,Object> getValue2;
17         private final Function2<Variable,Object,Object> setValue2;
18         private final Function3<Variable,Object,Binding,Object> setValue3;
19         private final Function1<Variable,Datatype> getDatatype;
20         
21         public SCLValueAccessor(Function1<Variable, Object> getValue1, Function2<Variable, Binding, Object> getValue2,
22                         Function2<Variable, Object, Object> setValue2, Function3<Variable, Object, Binding, Object> setValue3,
23                         Function1<Variable, Datatype> getDatatype) {
24                 this.getValue1 = getValue1;
25                 this.getValue2 = getValue2;
26                 this.setValue2 = setValue2;
27                 this.setValue3 = setValue3;
28                 this.getDatatype = getDatatype;
29         }
30
31         @Override
32         public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
33         SCLContext sclContext = SCLContext.getCurrent();
34         Object oldGraph = sclContext.put("graph", graph);
35         try {
36             return getValue1.apply(context);
37         } catch (Throwable t) {
38             throw new DatabaseException(t);
39         } finally {
40             sclContext.put("graph", oldGraph);
41         }
42         }
43
44         @Override
45         public Object getValue(ReadGraph graph, Variable context, Binding binding) throws DatabaseException {
46         SCLContext sclContext = SCLContext.getCurrent();
47         Object oldGraph = sclContext.put("graph", graph);
48         try {
49             return getValue2.apply(context, binding);
50         } catch (Throwable t) {
51             throw new DatabaseException(t);
52         } finally {
53             sclContext.put("graph", oldGraph);
54         }
55         }
56
57         @Override
58         public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException {
59         SCLContext sclContext = SCLContext.getCurrent();
60         Object oldGraph = sclContext.put("graph", graph);
61         try {
62             setValue2.apply(context, value);
63         } catch (Throwable t) {
64             throw new DatabaseException(t);
65         } finally {
66             sclContext.put("graph", oldGraph);
67         }
68         }
69
70         @Override
71         public void setValue(WriteGraph graph, Variable context, Object value, Binding binding) throws DatabaseException {
72         SCLContext sclContext = SCLContext.getCurrent();
73         Object oldGraph = sclContext.put("graph", graph);
74         try {
75             setValue3.apply(context, value, binding);
76         } catch (Throwable t) {
77             throw new DatabaseException(t);
78         } finally {
79             sclContext.put("graph", oldGraph);
80         }
81         }
82
83         @Override
84         public Datatype getDatatype(ReadGraph graph, Variable context) throws DatabaseException {
85         SCLContext sclContext = SCLContext.getCurrent();
86         Object oldGraph = sclContext.put("graph", graph);
87         try {
88             return getDatatype.apply(context);
89         } catch (Throwable t) {
90             throw new DatabaseException(t);
91         } finally {
92             sclContext.put("graph", oldGraph);
93         }
94         }
95
96 }