]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph/src/org/simantics/graph/store/ValueStore.java
Merge commit '12468c2'
[simantics/platform.git] / bundles / org.simantics.graph / src / org / simantics / graph / store / ValueStore.java
1 package org.simantics.graph.store;\r
2 \r
3 import gnu.trove.map.hash.TIntIntHashMap;\r
4 import gnu.trove.map.hash.TIntObjectHashMap;\r
5 import gnu.trove.procedure.TIntObjectProcedure;\r
6 import gnu.trove.procedure.TIntProcedure;\r
7 import gnu.trove.set.hash.TIntHashSet;\r
8 \r
9 import java.util.ArrayList;\r
10 \r
11 import org.simantics.databoard.Bindings;\r
12 import org.simantics.databoard.adapter.AdaptException;\r
13 import org.simantics.databoard.binding.Binding;\r
14 import org.simantics.databoard.binding.mutable.Variant;\r
15 import org.simantics.databoard.serialization.Serializer;\r
16 import org.simantics.databoard.type.Datatype;\r
17 import org.simantics.graph.representation.Value;\r
18 \r
19 public class ValueStore implements IStore {\r
20         \r
21         public static Binding DatatypeBinding =\r
22                 Bindings.getBindingUnchecked(Datatype.class);\r
23         public static Serializer DatatypeSerializer =\r
24                 Bindings.getSerializerUnchecked(DatatypeBinding);\r
25         \r
26         TIntObjectHashMap<Variant> byteValues = new TIntObjectHashMap<Variant>();\r
27         TIntObjectHashMap<Datatype> datatypeValues = new TIntObjectHashMap<Datatype>();\r
28         TIntHashSet collisions = new TIntHashSet();\r
29         \r
30         public void map(final TIntIntHashMap map) {\r
31                 collisions = IndexMappingUtils.map(map, collisions);\r
32                 byteValues = IndexMappingUtils.map(map, byteValues, collisions);\r
33                 datatypeValues = IndexMappingUtils.map(map, datatypeValues, collisions);\r
34         }\r
35         \r
36         public void setValue(int id, Variant value) {\r
37                 if(byteValues.put(id, value) != null)\r
38                         collisions.add(id);\r
39         }\r
40         \r
41         public void setValue(int id, Datatype value) {\r
42                 datatypeValues.put(id, value);\r
43         }\r
44 \r
45         public Variant getByteValue(int id) {\r
46                 return byteValues.get(id);\r
47         }\r
48         \r
49         private static final Binding DATATYPE_BINDING = \r
50                 Bindings.getBindingUnchecked(Datatype.class);\r
51         \r
52         public Datatype getDatatypeValue(int id) {\r
53                 Datatype datatype = datatypeValues.get(id);\r
54                 if(datatype == null) {\r
55                         Variant bytes = byteValues.get(id);\r
56                         if(bytes != null)\r
57                                 try {\r
58                                         return (Datatype)bytes.getValue(DATATYPE_BINDING);\r
59                                 } catch (AdaptException e) {\r
60                                         throw new RuntimeException(e);\r
61                                 }\r
62                 }\r
63                 return datatype;\r
64         }\r
65 \r
66         public Value[] toArray() {\r
67                 final ArrayList<Value> values = new ArrayList<Value>();\r
68                 byteValues.forEachEntry(new TIntObjectProcedure<Variant>() {\r
69                         @Override\r
70                         public boolean execute(int a, Variant b) {\r
71                                 values.add(new Value(a, b));\r
72                                 return true;\r
73                         }\r
74                 });\r
75                 datatypeValues.forEachEntry(new TIntObjectProcedure<Datatype>() {\r
76                     Binding datatypeBinding = Bindings.getBindingUnchecked(Datatype.class);\r
77                         @Override\r
78                         public boolean execute(int a, Datatype b) {\r
79                                 if(!byteValues.containsKey(a))\r
80                                         try {\r
81                                                 if(b == null)\r
82                                                         System.out.println("Resource " + a + " has null Datatype value.");\r
83                                                 else\r
84                                                         values.add(new Value(a, new Variant(datatypeBinding, b)));\r
85                                         } catch (Exception e) {\r
86                                                 throw new RuntimeException(e);\r
87                                         }\r
88                                 return true;\r
89                         }\r
90                 });\r
91                 return values.toArray(new Value[values.size()]);\r
92         }\r
93         \r
94         public void collectReferences(final boolean[] set) {\r
95                 TIntProcedure proc = new TIntProcedure() {                      \r
96                         @Override\r
97                         public boolean execute(int value) {\r
98                                 set[value] = true;\r
99                                 return true;\r
100                         }\r
101                 };\r
102                 \r
103                 byteValues.forEach(proc);\r
104                 datatypeValues.forEach(proc);\r
105         }\r
106         \r
107         public TIntHashSet getCollisions() {\r
108                 datatypeValues.forEachKey(new TIntProcedure() {\r
109                         @Override\r
110                         public boolean execute(int value) {\r
111                                 if(byteValues.containsKey(value))\r
112                                         collisions.add(value);\r
113                                 return true;\r
114                         }\r
115                 });\r
116                 return collisions;\r
117         }\r
118 }\r