X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.graph%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fstore%2FValueStore.java;fp=bundles%2Forg.simantics.graph%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fstore%2FValueStore.java;h=8a490f3f6a78d016d581827f9da41cb07a0b78b4;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=96f8b0d161923c020f2ee669f1abce3c8508b294;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/store/ValueStore.java b/bundles/org.simantics.graph/src/org/simantics/graph/store/ValueStore.java index 96f8b0d16..8a490f3f6 100644 --- a/bundles/org.simantics.graph/src/org/simantics/graph/store/ValueStore.java +++ b/bundles/org.simantics.graph/src/org/simantics/graph/store/ValueStore.java @@ -1,118 +1,118 @@ -package org.simantics.graph.store; - -import gnu.trove.map.hash.TIntIntHashMap; -import gnu.trove.map.hash.TIntObjectHashMap; -import gnu.trove.procedure.TIntObjectProcedure; -import gnu.trove.procedure.TIntProcedure; -import gnu.trove.set.hash.TIntHashSet; - -import java.util.ArrayList; - -import org.simantics.databoard.Bindings; -import org.simantics.databoard.adapter.AdaptException; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.binding.mutable.Variant; -import org.simantics.databoard.serialization.Serializer; -import org.simantics.databoard.type.Datatype; -import org.simantics.graph.representation.Value; - -public class ValueStore implements IStore { - - public static Binding DatatypeBinding = - Bindings.getBindingUnchecked(Datatype.class); - public static Serializer DatatypeSerializer = - Bindings.getSerializerUnchecked(DatatypeBinding); - - TIntObjectHashMap byteValues = new TIntObjectHashMap(); - TIntObjectHashMap datatypeValues = new TIntObjectHashMap(); - TIntHashSet collisions = new TIntHashSet(); - - public void map(final TIntIntHashMap map) { - collisions = IndexMappingUtils.map(map, collisions); - byteValues = IndexMappingUtils.map(map, byteValues, collisions); - datatypeValues = IndexMappingUtils.map(map, datatypeValues, collisions); - } - - public void setValue(int id, Variant value) { - if(byteValues.put(id, value) != null) - collisions.add(id); - } - - public void setValue(int id, Datatype value) { - datatypeValues.put(id, value); - } - - public Variant getByteValue(int id) { - return byteValues.get(id); - } - - private static final Binding DATATYPE_BINDING = - Bindings.getBindingUnchecked(Datatype.class); - - public Datatype getDatatypeValue(int id) { - Datatype datatype = datatypeValues.get(id); - if(datatype == null) { - Variant bytes = byteValues.get(id); - if(bytes != null) - try { - return (Datatype)bytes.getValue(DATATYPE_BINDING); - } catch (AdaptException e) { - throw new RuntimeException(e); - } - } - return datatype; - } - - public Value[] toArray() { - final ArrayList values = new ArrayList(); - byteValues.forEachEntry(new TIntObjectProcedure() { - @Override - public boolean execute(int a, Variant b) { - values.add(new Value(a, b)); - return true; - } - }); - datatypeValues.forEachEntry(new TIntObjectProcedure() { - Binding datatypeBinding = Bindings.getBindingUnchecked(Datatype.class); - @Override - public boolean execute(int a, Datatype b) { - if(!byteValues.containsKey(a)) - try { - if(b == null) - System.out.println("Resource " + a + " has null Datatype value."); - else - values.add(new Value(a, new Variant(datatypeBinding, b))); - } catch (Exception e) { - throw new RuntimeException(e); - } - return true; - } - }); - return values.toArray(new Value[values.size()]); - } - - public void collectReferences(final boolean[] set) { - TIntProcedure proc = new TIntProcedure() { - @Override - public boolean execute(int value) { - set[value] = true; - return true; - } - }; - - byteValues.forEach(proc); - datatypeValues.forEach(proc); - } - - public TIntHashSet getCollisions() { - datatypeValues.forEachKey(new TIntProcedure() { - @Override - public boolean execute(int value) { - if(byteValues.containsKey(value)) - collisions.add(value); - return true; - } - }); - return collisions; - } -} +package org.simantics.graph.store; + +import gnu.trove.map.hash.TIntIntHashMap; +import gnu.trove.map.hash.TIntObjectHashMap; +import gnu.trove.procedure.TIntObjectProcedure; +import gnu.trove.procedure.TIntProcedure; +import gnu.trove.set.hash.TIntHashSet; + +import java.util.ArrayList; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.adapter.AdaptException; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.databoard.serialization.Serializer; +import org.simantics.databoard.type.Datatype; +import org.simantics.graph.representation.Value; + +public class ValueStore implements IStore { + + public static Binding DatatypeBinding = + Bindings.getBindingUnchecked(Datatype.class); + public static Serializer DatatypeSerializer = + Bindings.getSerializerUnchecked(DatatypeBinding); + + TIntObjectHashMap byteValues = new TIntObjectHashMap(); + TIntObjectHashMap datatypeValues = new TIntObjectHashMap(); + TIntHashSet collisions = new TIntHashSet(); + + public void map(final TIntIntHashMap map) { + collisions = IndexMappingUtils.map(map, collisions); + byteValues = IndexMappingUtils.map(map, byteValues, collisions); + datatypeValues = IndexMappingUtils.map(map, datatypeValues, collisions); + } + + public void setValue(int id, Variant value) { + if(byteValues.put(id, value) != null) + collisions.add(id); + } + + public void setValue(int id, Datatype value) { + datatypeValues.put(id, value); + } + + public Variant getByteValue(int id) { + return byteValues.get(id); + } + + private static final Binding DATATYPE_BINDING = + Bindings.getBindingUnchecked(Datatype.class); + + public Datatype getDatatypeValue(int id) { + Datatype datatype = datatypeValues.get(id); + if(datatype == null) { + Variant bytes = byteValues.get(id); + if(bytes != null) + try { + return (Datatype)bytes.getValue(DATATYPE_BINDING); + } catch (AdaptException e) { + throw new RuntimeException(e); + } + } + return datatype; + } + + public Value[] toArray() { + final ArrayList values = new ArrayList(); + byteValues.forEachEntry(new TIntObjectProcedure() { + @Override + public boolean execute(int a, Variant b) { + values.add(new Value(a, b)); + return true; + } + }); + datatypeValues.forEachEntry(new TIntObjectProcedure() { + Binding datatypeBinding = Bindings.getBindingUnchecked(Datatype.class); + @Override + public boolean execute(int a, Datatype b) { + if(!byteValues.containsKey(a)) + try { + if(b == null) + System.out.println("Resource " + a + " has null Datatype value."); + else + values.add(new Value(a, new Variant(datatypeBinding, b))); + } catch (Exception e) { + throw new RuntimeException(e); + } + return true; + } + }); + return values.toArray(new Value[values.size()]); + } + + public void collectReferences(final boolean[] set) { + TIntProcedure proc = new TIntProcedure() { + @Override + public boolean execute(int value) { + set[value] = true; + return true; + } + }; + + byteValues.forEach(proc); + datatypeValues.forEach(proc); + } + + public TIntHashSet getCollisions() { + datatypeValues.forEachKey(new TIntProcedure() { + @Override + public boolean execute(int value) { + if(byteValues.containsKey(value)) + collisions.add(value); + return true; + } + }); + return collisions; + } +}