]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/AdaptionExample3.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / examples / org / simantics / databoard / example / old / AdaptionExample3.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.example.old;
13
14 import java.util.Arrays;\r
15 \r
16 import org.simantics.databoard.Bindings;\r
17 import org.simantics.databoard.annotations.Length;\r
18 import org.simantics.databoard.binding.Binding;\r
19 import org.simantics.databoard.serialization.Serializer;\r
20
21 /**
22  * In this duck-typing example, there are two classes with compatible contents.
23  * Contents of one class is moved to another using binary serialization.  
24  *
25  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
26  */
27 public class AdaptionExample3 {
28         
29         static class DoubleVector {
30                 public DoubleVector() {}
31                 public double x, y, z;
32         }
33         static final Binding DoubleVectorBinding = Bindings.getBindingUnchecked( DoubleVector.class );
34         static final Serializer DoubleVectorSerializer = Bindings.getSerializerUnchecked( DoubleVectorBinding );
35         
36         static class DoubleArray {
37                 public DoubleArray() {}
38                 
39                 public @Length("3") double[] array;
40         }
41         static final Binding DoubleArrayBinding = Bindings.getBindingUnchecked( DoubleArray.class );
42         static final Serializer DoubleArraySerializer = Bindings.getSerializerUnchecked( DoubleArrayBinding );
43         
44         public static void main(String[] args) throws Exception {
45                 
46                 // Create double array
47                 DoubleArray doubleArray = new DoubleArray();
48                 doubleArray.array = new double[] {3, 6, 9};
49                 
50                 // Serialize doubleArray to binary data format
51                 byte[] data = DoubleArraySerializer.serialize( doubleArray );
52                 System.out.println("DoubleArray: " + Arrays.toString(doubleArray.array) );
53                 
54                 // Unserialize binary data to DoubleVector                      
55                 DoubleVector doubleVector = (DoubleVector) DoubleVectorSerializer.deserialize( data );
56                 System.out.printf("DoubleVector: %f, %f, %f\n", doubleVector.x, doubleVector.y, doubleVector.z);\r
57                 \r
58                                 \r
59         \r
60                                 
61         }
62         
63
64 }
65