1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.example.old;
14 import java.util.Arrays;
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;
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.
25 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
27 public class AdaptionExample3 {
29 static class DoubleVector {
30 public DoubleVector() {}
31 public double x, y, z;
33 static final Binding DoubleVectorBinding = Bindings.getBindingUnchecked( DoubleVector.class );
34 static final Serializer DoubleVectorSerializer = Bindings.getSerializerUnchecked( DoubleVectorBinding );
36 static class DoubleArray {
37 public DoubleArray() {}
39 public @Length("3") double[] array;
41 static final Binding DoubleArrayBinding = Bindings.getBindingUnchecked( DoubleArray.class );
42 static final Serializer DoubleArraySerializer = Bindings.getSerializerUnchecked( DoubleArrayBinding );
44 public static void main(String[] args) throws Exception {
46 // Create double array
47 DoubleArray doubleArray = new DoubleArray();
48 doubleArray.array = new double[] {3, 6, 9};
50 // Serialize doubleArray to binary data format
51 byte[] data = DoubleArraySerializer.serialize( doubleArray );
52 System.out.println("DoubleArray: " + Arrays.toString(doubleArray.array) );
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);