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