--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.example;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Datatypes;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.IntegerBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.binding.impl.IntegerBindingDefault;\r
+import org.simantics.databoard.serialization.Serializer;\r
+import org.simantics.databoard.type.ArrayType;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.type.IntegerType;\r
+\r
+public class BindingExample {\r
+\r
+ @SuppressWarnings("unchecked")\r
+ public static void main(String[] args) throws Exception {\r
+\r
+ //\r
+ // Binding is an object that makes modifications and conversions \r
+ // between Datavalues and Java Objects possible.\r
+ //\r
+ //\r
+ // There are 13 datatypes. There is an abstract binding base class \r
+ // for each datatype, and a concrete class for each java class mapping.\r
+ //\r
+ // For example, IntegerBinding binds to Integer datatype. It is abstract\r
+ // class and does not bind to any specific java class.\r
+ // \r
+ // It's sub-classes IntegerJavaBinding and MutableIntegerBinding bind to\r
+ // specific classes (java.lang.Integer and MutableInteger).\r
+ //\r
+\r
+ IntegerBinding binding = Bindings.INTEGER;\r
+ \r
+ // We can create a java object with a binding\r
+ Object obj = binding.create( 5 /* A valid value of Integer Datatype */ );\r
+ \r
+ // We know that the binding class binds java.lang.Integer, we can typecast \r
+ Integer number = (Integer) obj;\r
+ \r
+ // We can read the value in a Java Object into a integer value\r
+ int value = binding.getValue_( obj );\r
+ \r
+ // The object can be cloned. Immutable classes are recycled \r
+ Object obj2 = binding.clone( obj );\r
+\r
+ \r
+ \r
+ // \r
+ // All Binding classes are comparators, and all values can be ordered. \r
+ // The compare algorithm is specified here http://dev.simantics.org/index.php/Databoard_Specification#Order\r
+ //\r
+ System.out.println("Sorting array");\r
+ Integer[] array = new Integer[] {5, 2, 10, 6000, -30};\r
+ System.out.println(" unsorted = "+Arrays.toString(array));\r
+ Arrays.sort(array, binding);\r
+ System.out.println( Arrays.toString( array ) );\r
+ System.out.println(" sorted = "+Arrays.toString(array));\r
+ \r
+ \r
+ //\r
+ // Bindings can do hashCode and equals aswell\r
+ //\r
+ binding.hashValue( 5 );\r
+ binding.equals(5, 6);\r
+ \r
+ \r
+ // \r
+ // Data types can have restrictions. Bindings can validate that datavalues \r
+ // are valid according to the restrictions of the type.\r
+ //\r
+ binding = new IntegerBindingDefault( new IntegerType(null, "[0..10]") );\r
+ try {\r
+ value = 0;\r
+ System.out.print("Validating "+value+" ");\r
+ binding.assertInstaceIsValid(value);\r
+ System.out.println("ok");\r
+ } catch (BindingException e) {\r
+ System.err.println(e);\r
+ }\r
+ try {\r
+ value = 11;\r
+ System.out.print("Validating "+value+" ");\r
+ binding.assertInstaceIsValid(value);\r
+ } catch (BindingException e) {\r
+ System.out.println(e);\r
+ }\r
+ System.out.println();\r
+ \r
+ \r
+ //\r
+ // Bindings can be acquired from class description\r
+ //\r
+ Binding b1 = Bindings.getBinding( int[].class );\r
+ Binding b2 = Bindings.getBinding( Integer[].class );\r
+ \r
+ // \r
+ // Bindings can be acquired from a datatype\r
+ //\r
+ Datatype arrayType = new ArrayType( Datatypes.INTEGER ); /* Integer[] */ \r
+ Binding b3 = Bindings.getMutableBinding( arrayType ); /* ArrayList<Integer> */ \r
+ \r
+ \r
+ //\r
+ // Objects can be converted between bindings \r
+ // \r
+ int[] array1 = new int[] {1, 10, 100, 1000, -23432};\r
+ Integer[] array2 = (Integer[]) Bindings.adapt(array1, b1, b2);\r
+ ArrayList<Integer> array3 = (ArrayList<Integer>) Bindings.adapt(array2, b2, b3);\r
+ \r
+ System.out.println( "Adapting classes:");\r
+ System.out.println( " int[] = "+Arrays.toString(array1) );\r
+ System.out.println( " Integer[] = "+Arrays.toString(array2) );\r
+ System.out.println( " ArrayList<Integer> = "+array3 );\r
+ System.out.println();\r
+ \r
+ \r
+ \r
+ //\r
+ // Binding can create a default value for any data type.\r
+ // Default value is specified at http://dev.simantics.org/index.php/index.php/Databoard_Specification#Default_Value \r
+ // \r
+ value = (Integer) binding.createDefault();\r
+ \r
+ // Default value is always valid.\r
+ Datatype type = new IntegerType(null, "[100..200]");\r
+ binding = new IntegerBindingDefault( (IntegerType) type );\r
+ value = (Integer) binding.createDefault();\r
+ System.out.println("Default Value for value for "+type+" is "+value);\r
+ \r
+ //\r
+ // Binding can create a valid random value\r
+ //\r
+ value = (Integer) binding.createRandom(23243);\r
+ System.out.println("Random Value for value for "+type+" is "+value);\r
+ \r
+ \r
+ // \r
+ // Values can be serialized to byte[] and back to java Objects\r
+ // Binary serialization format is specified here, \r
+ // http://dev.simantics.org/index.php/Databoard_Specification#Binary_Serialization_Format\r
+ //\r
+ Serializer s = Bindings.getSerializer( binding );\r
+ value = 50;\r
+ byte[] data = s.serialize( value );\r
+ value = (Integer) s.deserialize( data );\r
+ \r
+ \r
+ //\r
+ // Values can be converted to ascii notation and back\r
+ // Ascii notation is specified at http://dev.simantics.org/index.php/index.php/Databoard_Specification#Datatypes\r
+ //\r
+ \r
+ \r
+ }\r
+ \r
+}\r
+\r