1 package org.simantics.graph.store;
\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
9 import java.util.ArrayList;
\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
19 public class ValueStore implements IStore {
\r
21 public static Binding DatatypeBinding =
\r
22 Bindings.getBindingUnchecked(Datatype.class);
\r
23 public static Serializer DatatypeSerializer =
\r
24 Bindings.getSerializerUnchecked(DatatypeBinding);
\r
26 TIntObjectHashMap<Variant> byteValues = new TIntObjectHashMap<Variant>();
\r
27 TIntObjectHashMap<Datatype> datatypeValues = new TIntObjectHashMap<Datatype>();
\r
28 TIntHashSet collisions = new TIntHashSet();
\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
36 public void setValue(int id, Variant value) {
\r
37 if(byteValues.put(id, value) != null)
\r
41 public void setValue(int id, Datatype value) {
\r
42 datatypeValues.put(id, value);
\r
45 public Variant getByteValue(int id) {
\r
46 return byteValues.get(id);
\r
49 private static final Binding DATATYPE_BINDING =
\r
50 Bindings.getBindingUnchecked(Datatype.class);
\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
58 return (Datatype)bytes.getValue(DATATYPE_BINDING);
\r
59 } catch (AdaptException e) {
\r
60 throw new RuntimeException(e);
\r
66 public Value[] toArray() {
\r
67 final ArrayList<Value> values = new ArrayList<Value>();
\r
68 byteValues.forEachEntry(new TIntObjectProcedure<Variant>() {
\r
70 public boolean execute(int a, Variant b) {
\r
71 values.add(new Value(a, b));
\r
75 datatypeValues.forEachEntry(new TIntObjectProcedure<Datatype>() {
\r
76 Binding datatypeBinding = Bindings.getBindingUnchecked(Datatype.class);
\r
78 public boolean execute(int a, Datatype b) {
\r
79 if(!byteValues.containsKey(a))
\r
82 System.out.println("Resource " + a + " has null Datatype value.");
\r
84 values.add(new Value(a, new Variant(datatypeBinding, b)));
\r
85 } catch (Exception e) {
\r
86 throw new RuntimeException(e);
\r
91 return values.toArray(new Value[values.size()]);
\r
94 public void collectReferences(final boolean[] set) {
\r
95 TIntProcedure proc = new TIntProcedure() {
\r
97 public boolean execute(int value) {
\r
103 byteValues.forEach(proc);
\r
104 datatypeValues.forEach(proc);
\r
107 public TIntHashSet getCollisions() {
\r
108 datatypeValues.forEachKey(new TIntProcedure() {
\r
110 public boolean execute(int value) {
\r
111 if(byteValues.containsKey(value))
\r
112 collisions.add(value);
\r