X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fserialization%2Fimpl%2FUnionSerializer.java;h=a677c86a2be10a2dc9745db3f77df4850f6ab984;hp=97bd6301c6adea0ba984c2bcf44d203047035a3e;hb=694c553a3c402b9397ff99e904b4f99b77b51df1;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/impl/UnionSerializer.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/impl/UnionSerializer.java index 97bd6301c..a677c86a2 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/impl/UnionSerializer.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/impl/UnionSerializer.java @@ -1,150 +1,150 @@ -package org.simantics.databoard.serialization.impl; - -import gnu.trove.map.hash.TObjectIntHashMap; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.util.List; - -import org.simantics.databoard.binding.UnionBinding; -import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.binding.util.IsReferableQuery; -import org.simantics.databoard.binding.util.Result; -import org.simantics.databoard.serialization.SerializationException; -import org.simantics.databoard.serialization.Serializer; -import org.simantics.databoard.serialization.Serializer.CompositeSerializer; -import org.simantics.databoard.type.UnionType; -import org.simantics.databoard.util.binary.Endian; - -public class UnionSerializer extends CompositeSerializer { - - UnionBinding binding; - public Serializer[] componentSerializers; - int tagTypeCount; - Integer fixedSize; - int minSize; - - /** - * - * @param binding - * @param componentSerializers (optional) can be set later - */ - public UnionSerializer(UnionBinding binding, Serializer[] componentSerializers) - { - super( IsReferableQuery.isReferable( binding.type() ) != Result.No ); - tagTypeCount = ((UnionType) binding.type()).components.length; - this.binding = binding; - this.componentSerializers = componentSerializers; - } - - @Override - public void finalizeConstruction() { - // Calculate Fixed Size - for (Serializer s : componentSerializers) - { - Integer fixedSizeOfComponentSerializer = s.getConstantSize(); - if (fixedSizeOfComponentSerializer==null) { - fixedSize = null; - break; - } - if (fixedSize!=null && fixedSizeOfComponentSerializer!=fixedSize) { - fixedSize = null; - break; - } - fixedSize = fixedSizeOfComponentSerializer; - } - if (componentSerializers.length == 0) fixedSize = 0; - if (fixedSize!=null) fixedSize += Endian.getUIntLength(tagTypeCount-1); - - if (componentSerializers.length>0) { - minSize = Integer.MAX_VALUE; - for (Serializer s : componentSerializers) minSize = Math.min(minSize, s.getMinSize()); - minSize += Endian.getUIntLength( tagTypeCount-1 ); - } - - } - - @Override - public Object deserialize(DataInput in, List identities) throws IOException { - try { - int tag = Endian.getUInt(in, tagTypeCount-1); - return binding.create(tag, componentSerializers[tag].deserialize(in, identities)); - } catch (BindingException e) { - throw new IOException( e ); - } - } - - public Object deserializeToTry(DataInput in, List identities, Object dst) throws IOException - { - if (binding.isTagMutable()) { - return deserialize(in, identities); - } else { - deserializeTo(in, identities, dst); - return dst; - } - } - - - @Override - public void deserializeTo(DataInput in, List identities, Object obj) throws IOException { - try { - int tag = Endian.getUInt(in, tagTypeCount-1); - int oldTag = binding.getTag(obj); - Serializer cs = componentSerializers[tag]; - boolean csImmutable = binding.getComponentBinding(tag).isImmutable(); - - if (oldTag==tag && !csImmutable) { - Object component = binding.getValue(obj); - component = componentSerializers[tag].deserializeToTry(in, identities, component); - binding.setValue(obj, tag, component); - } else { - Object component = cs.deserialize(in, identities); - binding.setValue(obj, tag, component); - } - } catch (BindingException e) { - throw new IOException( e ); - } - } - - @Override - public void skip(DataInput in, List identities) throws IOException, SerializationException { - int tag = Endian.getUInt(in, tagTypeCount-1); - componentSerializers[tag].skip(in, identities); - } - - @Override - public void serialize(DataOutput out, TObjectIntHashMap identities, Object obj) throws IOException { - try { - int tag = binding.getTag(obj); - Endian.putUInt(out, tag, tagTypeCount-1); - componentSerializers[tag].serialize(out, identities, binding.getValue(obj)); - } catch (BindingException e) { - throw new IOException( e ); - } - } - - @Override - public Integer getConstantSize() { - return fixedSize; - } - - @Override - public int getSize(Object obj, TObjectIntHashMap identities) throws IOException { - try { - if (fixedSize!=null) return fixedSize; - int tag = binding.getTag(obj); - return Endian.getUIntLength( tagTypeCount-1 ) + - componentSerializers[tag].getSize(binding.getValue(obj), identities); - } catch (BindingException e) { - throw new IOException( e ); - } - } - - @Override - public int getMinSize() { - return minSize; - } - - -} +package org.simantics.databoard.serialization.impl; + +import gnu.trove.map.hash.TObjectIntHashMap; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.List; + +import org.simantics.databoard.binding.UnionBinding; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.util.IsReferableQuery; +import org.simantics.databoard.binding.util.Result; +import org.simantics.databoard.serialization.SerializationException; +import org.simantics.databoard.serialization.Serializer; +import org.simantics.databoard.serialization.Serializer.CompositeSerializer; +import org.simantics.databoard.type.UnionType; +import org.simantics.databoard.util.binary.Endian; + +public class UnionSerializer extends CompositeSerializer { + + UnionBinding binding; + public Serializer[] componentSerializers; + int tagTypeCount; + Integer fixedSize; + int minSize; + + /** + * + * @param binding + * @param componentSerializers (optional) can be set later + */ + public UnionSerializer(UnionBinding binding, Serializer[] componentSerializers) + { + super( IsReferableQuery.isReferable( binding.type() ) != Result.No ); + tagTypeCount = ((UnionType) binding.type()).components.length; + this.binding = binding; + this.componentSerializers = componentSerializers; + } + + @Override + public void finalizeConstruction() { + // Calculate Fixed Size + for (Serializer s : componentSerializers) + { + Integer fixedSizeOfComponentSerializer = s.getConstantSize(); + if (fixedSizeOfComponentSerializer==null) { + fixedSize = null; + break; + } + if (fixedSize!=null && !fixedSize.equals(fixedSizeOfComponentSerializer)) { + fixedSize = null; + break; + } + fixedSize = fixedSizeOfComponentSerializer; + } + if (componentSerializers.length == 0) fixedSize = 0; + if (fixedSize!=null) fixedSize += Endian.getUIntLength(tagTypeCount-1); + + if (componentSerializers.length>0) { + minSize = Integer.MAX_VALUE; + for (Serializer s : componentSerializers) minSize = Math.min(minSize, s.getMinSize()); + minSize += Endian.getUIntLength( tagTypeCount-1 ); + } + + } + + @Override + public Object deserialize(DataInput in, List identities) throws IOException { + try { + int tag = Endian.getUInt(in, tagTypeCount-1); + return binding.create(tag, componentSerializers[tag].deserialize(in, identities)); + } catch (BindingException e) { + throw new IOException( e ); + } + } + + public Object deserializeToTry(DataInput in, List identities, Object dst) throws IOException + { + if (binding.isTagMutable()) { + return deserialize(in, identities); + } else { + deserializeTo(in, identities, dst); + return dst; + } + } + + + @Override + public void deserializeTo(DataInput in, List identities, Object obj) throws IOException { + try { + int tag = Endian.getUInt(in, tagTypeCount-1); + int oldTag = binding.getTag(obj); + Serializer cs = componentSerializers[tag]; + boolean csImmutable = binding.getComponentBinding(tag).isImmutable(); + + if (oldTag==tag && !csImmutable) { + Object component = binding.getValue(obj); + component = componentSerializers[tag].deserializeToTry(in, identities, component); + binding.setValue(obj, tag, component); + } else { + Object component = cs.deserialize(in, identities); + binding.setValue(obj, tag, component); + } + } catch (BindingException e) { + throw new IOException( e ); + } + } + + @Override + public void skip(DataInput in, List identities) throws IOException, SerializationException { + int tag = Endian.getUInt(in, tagTypeCount-1); + componentSerializers[tag].skip(in, identities); + } + + @Override + public void serialize(DataOutput out, TObjectIntHashMap identities, Object obj) throws IOException { + try { + int tag = binding.getTag(obj); + Endian.putUInt(out, tag, tagTypeCount-1); + componentSerializers[tag].serialize(out, identities, binding.getValue(obj)); + } catch (BindingException e) { + throw new IOException( e ); + } + } + + @Override + public Integer getConstantSize() { + return fixedSize; + } + + @Override + public int getSize(Object obj, TObjectIntHashMap identities) throws IOException { + try { + if (fixedSize!=null) return fixedSize; + int tag = binding.getTag(obj); + return Endian.getUIntLength( tagTypeCount-1 ) + + componentSerializers[tag].getSize(binding.getValue(obj), identities); + } catch (BindingException e) { + throw new IOException( e ); + } + } + + @Override + public int getMinSize() { + return minSize; + } + + +}