1 package org.simantics.graph.store;
3 import gnu.trove.map.hash.TIntIntHashMap;
4 import gnu.trove.map.hash.TIntObjectHashMap;
5 import gnu.trove.procedure.TIntObjectProcedure;
6 import gnu.trove.procedure.TIntProcedure;
7 import gnu.trove.set.hash.TIntHashSet;
9 import java.util.ArrayList;
11 import org.simantics.databoard.Bindings;
12 import org.simantics.databoard.adapter.AdaptException;
13 import org.simantics.databoard.binding.Binding;
14 import org.simantics.databoard.binding.mutable.Variant;
15 import org.simantics.databoard.serialization.Serializer;
16 import org.simantics.databoard.type.Datatype;
17 import org.simantics.graph.representation.Value;
19 public class ValueStore implements IStore {
21 public static Binding DatatypeBinding =
22 Bindings.getBindingUnchecked(Datatype.class);
23 public static Serializer DatatypeSerializer =
24 Bindings.getSerializerUnchecked(DatatypeBinding);
26 TIntObjectHashMap<Variant> byteValues = new TIntObjectHashMap<Variant>();
27 TIntObjectHashMap<Datatype> datatypeValues = new TIntObjectHashMap<Datatype>();
28 TIntHashSet collisions = new TIntHashSet();
30 public void map(final TIntIntHashMap map) {
31 collisions = IndexMappingUtils.map(map, collisions);
32 byteValues = IndexMappingUtils.map(map, byteValues, collisions);
33 datatypeValues = IndexMappingUtils.map(map, datatypeValues, collisions);
36 public void setValue(int id, Variant value) {
37 if(byteValues.put(id, value) != null)
41 public void setValue(int id, Datatype value) {
42 datatypeValues.put(id, value);
45 public Variant getByteValue(int id) {
46 return byteValues.get(id);
49 private static final Binding DATATYPE_BINDING =
50 Bindings.getBindingUnchecked(Datatype.class);
52 public Datatype getDatatypeValue(int id) {
53 Datatype datatype = datatypeValues.get(id);
54 if(datatype == null) {
55 Variant bytes = byteValues.get(id);
58 return (Datatype)bytes.getValue(DATATYPE_BINDING);
59 } catch (AdaptException e) {
60 throw new RuntimeException(e);
66 public Value[] toArray() {
67 final ArrayList<Value> values = new ArrayList<Value>();
68 byteValues.forEachEntry(new TIntObjectProcedure<Variant>() {
70 public boolean execute(int a, Variant b) {
71 values.add(new Value(a, b));
75 datatypeValues.forEachEntry(new TIntObjectProcedure<Datatype>() {
76 Binding datatypeBinding = Bindings.getBindingUnchecked(Datatype.class);
78 public boolean execute(int a, Datatype b) {
79 if(!byteValues.containsKey(a))
82 System.out.println("Resource " + a + " has null Datatype value.");
84 values.add(new Value(a, new Variant(datatypeBinding, b)));
85 } catch (Exception e) {
86 throw new RuntimeException(e);
91 return values.toArray(new Value[values.size()]);
94 public void collectReferences(final boolean[] set) {
95 TIntProcedure proc = new TIntProcedure() {
97 public boolean execute(int value) {
103 byteValues.forEach(proc);
104 datatypeValues.forEach(proc);
107 public TIntHashSet getCollisions() {
108 datatypeValues.forEachKey(new TIntProcedure() {
110 public boolean execute(int value) {
111 if(byteValues.containsKey(value))
112 collisions.add(value);