X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fexamples%2Forg%2Fsimantics%2Fdataboard%2Fexample%2Fold%2FAdaptionExample3.java;fp=bundles%2Forg.simantics.databoard%2Fexamples%2Forg%2Fsimantics%2Fdataboard%2Fexample%2Fold%2FAdaptionExample3.java;h=06a676aa1cf09ecef087079649f8f4d27caeb324;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/AdaptionExample3.java b/bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/AdaptionExample3.java new file mode 100644 index 000000000..06a676aa1 --- /dev/null +++ b/bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/AdaptionExample3.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2010 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.databoard.example.old; + +import java.util.Arrays; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.annotations.Length; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.serialization.Serializer; + +/** + * In this duck-typing example, there are two classes with compatible contents. + * Contents of one class is moved to another using binary serialization. + * + * @author Toni Kalajainen + */ +public class AdaptionExample3 { + + static class DoubleVector { + public DoubleVector() {} + public double x, y, z; + } + static final Binding DoubleVectorBinding = Bindings.getBindingUnchecked( DoubleVector.class ); + static final Serializer DoubleVectorSerializer = Bindings.getSerializerUnchecked( DoubleVectorBinding ); + + static class DoubleArray { + public DoubleArray() {} + + public @Length("3") double[] array; + } + static final Binding DoubleArrayBinding = Bindings.getBindingUnchecked( DoubleArray.class ); + static final Serializer DoubleArraySerializer = Bindings.getSerializerUnchecked( DoubleArrayBinding ); + + public static void main(String[] args) throws Exception { + + // Create double array + DoubleArray doubleArray = new DoubleArray(); + doubleArray.array = new double[] {3, 6, 9}; + + // Serialize doubleArray to binary data format + byte[] data = DoubleArraySerializer.serialize( doubleArray ); + System.out.println("DoubleArray: " + Arrays.toString(doubleArray.array) ); + + // Unserialize binary data to DoubleVector + DoubleVector doubleVector = (DoubleVector) DoubleVectorSerializer.deserialize( data ); + System.out.printf("DoubleVector: %f, %f, %f\n", doubleVector.x, doubleVector.y, doubleVector.z); + + + + + } + + +} +