1 package org.simantics.databoard.serialization.impl;
3 import gnu.trove.map.hash.TObjectIntHashMap;
5 import java.io.DataInput;
6 import java.io.DataOutput;
7 import java.io.IOException;
8 import java.util.ArrayList;
11 import org.simantics.databoard.Bindings;
12 import org.simantics.databoard.binding.Binding;
13 import org.simantics.databoard.binding.VariantBinding;
14 import org.simantics.databoard.binding.error.BindingException;
15 import org.simantics.databoard.serialization.Serializer;
16 import org.simantics.databoard.serialization.Serializer.RecursiveSerializer;
17 import org.simantics.databoard.serialization.SerializerScheme;
18 import org.simantics.databoard.type.Datatype;
21 * Serializes Variant instances using bindings from Bindings.getBinding for
22 * deserialization. These are assumed to be immutable bindings.
24 * @author Toni Kalajainen
26 public class VariantSerializer extends RecursiveSerializer {
28 VariantBinding binding;
29 Serializer datatypeSerializer;
30 SerializerScheme scheme;
32 public VariantSerializer(VariantBinding binding, SerializerScheme scheme) {
33 this.binding = binding;
38 public void finalizeConstruction() {
39 Binding dataTypeBinding = Bindings.getBindingUnchecked( Datatype.class );
40 this.datatypeSerializer = scheme.getSerializerUnchecked(dataTypeBinding);
44 public Object deserialize(DataInput in, List<Object> identities) throws IOException {
46 List<Object> typeIdentities = new ArrayList<Object>(1);
47 Datatype type = (Datatype) datatypeSerializer.deserialize(in, typeIdentities);
48 Binding valueBinding = Bindings.getBinding(type);
49 Serializer valueSerializer = scheme.getSerializerUnchecked(valueBinding);
50 assertRemainingBytes(in, valueSerializer.getMinSize());
51 Object value = valueSerializer.deserialize(in, identities);
52 return binding.create(valueBinding, value);
53 } catch (BindingException e) {
54 throw new IOException( e );
59 public void deserializeTo(DataInput in, List<Object> identities, Object obj) throws IOException {
61 List<Object> typeIdentities = new ArrayList<Object>(1);
62 Datatype type = (Datatype) datatypeSerializer.deserialize(in, typeIdentities);
63 Datatype oldType = binding.getContentType(obj);
65 if (type.equals(oldType)) {
66 Binding valueBinding = binding.getContentBinding(obj);
67 Serializer valueSerializer = scheme.getSerializerUnchecked(valueBinding);
68 Object component = binding.getContent(obj);
69 // assertRemainingBytes(in, valueSerializer.getMinSize());
70 component = valueSerializer.deserializeToTry(in, identities, component);
71 binding.setContent(obj, valueBinding, component);
73 Binding valueBinding = Bindings.getBinding(type);
74 Serializer valueSerializer = scheme.getSerializerUnchecked(valueBinding);
75 // assertRemainingBytes(in, valueSerializer.getMinSize());
76 Object component = valueSerializer.deserialize(in, identities);
77 binding.setContent(obj, valueBinding, component);
79 } catch (BindingException e) {
80 throw new IOException( e );
85 public void skip(DataInput in, List<Object> identities) throws IOException {
86 List<Object> typeIdentities = new ArrayList<Object>(1);
87 Datatype type = (Datatype) datatypeSerializer.deserialize(in, typeIdentities);
88 Binding valueBinding = Bindings.getBinding(type);
89 Serializer valueSerializer = scheme.getSerializerUnchecked(valueBinding);
90 valueSerializer.skip(in, identities);
94 public void serialize(DataOutput out, TObjectIntHashMap<Object> identities, Object variant) throws IOException {
96 TObjectIntHashMap<Object> typeIdentities = new TObjectIntHashMap<Object>(1);
97 Datatype type = binding.getContentType(variant);
98 datatypeSerializer.serialize(out, typeIdentities, type);
100 Binding valueBinding = binding.getContentBinding(variant);
101 Object value = binding.getContent(variant, valueBinding);
103 Serializer valueSerializer = scheme.getSerializerUnchecked(valueBinding);
104 valueSerializer.serialize(out, identities, value);
105 } catch (BindingException e) {
106 throw new IOException( e );
111 public Integer getConstantSize() {
116 public int getSize(Object variant, TObjectIntHashMap<Object> identities) throws IOException
119 TObjectIntHashMap<Object> typeIdentities = new TObjectIntHashMap<Object>(1);
120 Datatype type = binding.getContentType(variant);
121 int size = datatypeSerializer.getSize(type, typeIdentities);
123 Binding valueBinding = binding.getContentBinding(variant);
124 Object value = binding.getContent(variant, valueBinding);
126 Serializer valueSerializer = scheme.getSerializerUnchecked(valueBinding);
127 size += valueSerializer.getSize(value, identities);
129 } catch (BindingException e) {
130 throw new IOException( e );
135 public int getMinSize() {
136 return datatypeSerializer.getMinSize();