]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/AdaptionExample3.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / examples / org / simantics / databoard / example / old / AdaptionExample3.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 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.example.old;
13
14 import java.util.Arrays;
15
16 import org.simantics.databoard.Bindings;
17 import org.simantics.databoard.annotations.Length;
18 import org.simantics.databoard.binding.Binding;
19 import org.simantics.databoard.serialization.Serializer;
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);
57                 
58                                 
59         
60                                 
61         }
62         
63
64 }
65