]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/impl/MutableVariantSerializer.java
Add qualifier for SLF4J version to force update of JAR in TP
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / serialization / impl / MutableVariantSerializer.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.serialization.impl;
13
14 import java.io.DataInput;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import org.simantics.databoard.Bindings;
20 import org.simantics.databoard.binding.Binding;
21 import org.simantics.databoard.binding.VariantBinding;
22 import org.simantics.databoard.binding.error.BindingException;
23 import org.simantics.databoard.serialization.Serializer;
24 import org.simantics.databoard.serialization.SerializerScheme;
25 import org.simantics.databoard.type.Datatype;
26
27 /**
28  * A version of VariantSerializer that uses Bindings.getMutableBinding instead
29  * of Bindings.getBinding for deserialization.
30  * 
31  * @author Tuukka Lehtonen
32  */
33 public class MutableVariantSerializer extends VariantSerializer {
34
35         public MutableVariantSerializer(VariantBinding binding, SerializerScheme scheme) {
36                 super(binding, scheme);
37         }
38
39         @Override
40         public Object deserialize(DataInput in, List<Object> identities) throws IOException {
41                 try {
42                         List<Object> typeIdentities = new ArrayList<Object>(1);
43                         Datatype type = (Datatype) datatypeSerializer.deserialize(in, typeIdentities);
44                         Binding valueBinding = Bindings.getMutableBinding(type);
45                         Serializer valueSerializer = scheme.getSerializerUnchecked(valueBinding);
46                         assertRemainingBytes(in, valueSerializer.getMinSize());
47                         Object value = valueSerializer.deserialize(in, identities);
48                         return binding.create(valueBinding, value);
49                 } catch (BindingException e) {
50                         throw new IOException( e ); 
51                 }
52         }
53
54         @Override
55         public void deserializeTo(DataInput in, List<Object> identities, Object obj) throws IOException {
56                 try {
57                         List<Object> typeIdentities = new ArrayList<Object>(1);
58                         Datatype type = (Datatype) datatypeSerializer.deserialize(in, typeIdentities);
59                         Datatype oldType = binding.getContentType(obj);
60
61                         if (type.equals(oldType)) {
62                                 Binding valueBinding = binding.getContentBinding(obj);
63                                 Serializer valueSerializer = scheme.getSerializerUnchecked(valueBinding);
64                                 Object component = binding.getContent(obj);
65 //                              assertRemainingBytes(in, valueSerializer.getMinSize());
66                                 component = valueSerializer.deserializeToTry(in, identities, component);
67                                 binding.setContent(obj, valueBinding, component);
68                         } else {
69                                 Binding valueBinding = Bindings.getMutableBinding(type);
70                                 Serializer valueSerializer = scheme.getSerializerUnchecked(valueBinding);
71 //                              assertRemainingBytes(in, valueSerializer.getMinSize());
72                                 Object component = valueSerializer.deserialize(in, identities);
73                                 binding.setContent(obj, valueBinding, component);
74                         }
75                 } catch (BindingException e) {
76                         throw new IOException( e ); 
77                 }
78         }
79
80         @Override
81         public void skip(DataInput in, List<Object> identities) throws IOException {
82                 List<Object> typeIdentities = new ArrayList<Object>(1);
83                 Datatype type = (Datatype) datatypeSerializer.deserialize(in, typeIdentities);
84                 Binding valueBinding = Bindings.getMutableBinding(type);
85                 Serializer valueSerializer = scheme.getSerializerUnchecked(valueBinding);
86                 valueSerializer.skip(in, identities);
87         }
88
89 }