]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/examples/org/simantics/databoard/example/BindingExample.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / examples / org / simantics / databoard / example / BindingExample.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.databoard.example;\r
12 \r
13 import java.util.ArrayList;\r
14 import java.util.Arrays;\r
15 \r
16 import org.simantics.databoard.Bindings;\r
17 import org.simantics.databoard.Datatypes;\r
18 import org.simantics.databoard.binding.Binding;\r
19 import org.simantics.databoard.binding.IntegerBinding;\r
20 import org.simantics.databoard.binding.error.BindingException;\r
21 import org.simantics.databoard.binding.impl.IntegerBindingDefault;\r
22 import org.simantics.databoard.serialization.Serializer;\r
23 import org.simantics.databoard.type.ArrayType;\r
24 import org.simantics.databoard.type.Datatype;\r
25 import org.simantics.databoard.type.IntegerType;\r
26 \r
27 public class BindingExample {\r
28 \r
29         @SuppressWarnings("unchecked")\r
30         public static void main(String[] args) throws Exception {\r
31 \r
32                 //\r
33                 // Binding is an object that makes modifications and conversions  \r
34                 // between Datavalues and Java Objects possible.\r
35         //\r
36                 //\r
37                 // There are 13 datatypes. There is an abstract binding base class \r
38                 // for each datatype, and a concrete class for each java class mapping.\r
39         //\r
40                 // For example, IntegerBinding binds to Integer datatype. It is abstract\r
41                 // class and does not bind to any specific java class.\r
42                 //  \r
43                 // It's sub-classes IntegerJavaBinding and MutableIntegerBinding bind to\r
44                 // specific classes (java.lang.Integer and MutableInteger).\r
45                 //\r
46 \r
47                 IntegerBinding binding = Bindings.INTEGER;\r
48                 \r
49                 // We can create a java object with a binding\r
50                 Object obj = binding.create( 5 /* A valid value of Integer Datatype */ );\r
51                 \r
52                 // We know that the binding class binds java.lang.Integer, we can typecast \r
53                 Integer number = (Integer) obj;\r
54                 \r
55                 // We can read the value in a Java Object into a integer value\r
56                 int value = binding.getValue_( obj );\r
57                                 \r
58                 // The object can be cloned. Immutable classes are recycled  \r
59                 Object obj2 = binding.clone( obj );\r
60 \r
61                 \r
62                 \r
63                 // \r
64                 // All Binding classes are comparators, and all values can be ordered. \r
65                 // The compare algorithm is specified here http://dev.simantics.org/index.php/Databoard_Specification#Order\r
66                 //\r
67                 System.out.println("Sorting array");\r
68                 Integer[] array = new Integer[] {5, 2, 10, 6000, -30};\r
69                 System.out.println(" unsorted = "+Arrays.toString(array));\r
70                 Arrays.sort(array, binding);\r
71                 System.out.println( Arrays.toString( array ) );\r
72                 System.out.println(" sorted = "+Arrays.toString(array));\r
73                 \r
74                 \r
75                 //\r
76                 // Bindings can do hashCode and equals aswell\r
77                 //\r
78                 binding.hashValue( 5 );\r
79                 binding.equals(5, 6);\r
80                 \r
81                 \r
82                 // \r
83                 // Data types can have restrictions. Bindings can validate that datavalues \r
84                 // are valid according to the restrictions of the type.\r
85                 //\r
86                 binding = new IntegerBindingDefault( new IntegerType(null, "[0..10]") );\r
87                 try {\r
88                         value = 0;\r
89                         System.out.print("Validating "+value+" ");\r
90                         binding.assertInstaceIsValid(value);\r
91                         System.out.println("ok");\r
92                 } catch (BindingException e) {\r
93                         System.err.println(e);\r
94                 }\r
95                 try {\r
96                         value = 11;\r
97                         System.out.print("Validating "+value+" ");\r
98                         binding.assertInstaceIsValid(value);\r
99                 } catch (BindingException e) {\r
100                         System.out.println(e);\r
101                 }\r
102                 System.out.println();\r
103                 \r
104                 \r
105                 //\r
106                 // Bindings can be acquired from class description\r
107                 //\r
108                 Binding b1 = Bindings.getBinding( int[].class );\r
109                 Binding b2 = Bindings.getBinding( Integer[].class );\r
110                 \r
111                 // \r
112                 // Bindings can be acquired from a datatype\r
113                 //\r
114                 Datatype arrayType = new ArrayType( Datatypes.INTEGER ); /* Integer[] */                                \r
115                 Binding b3 = Bindings.getMutableBinding( arrayType ); /* ArrayList<Integer> */ \r
116                 \r
117                 \r
118                 //\r
119                 // Objects can be converted between bindings  \r
120                 //              \r
121                 int[] array1 = new int[] {1, 10, 100, 1000, -23432};\r
122                 Integer[] array2 = (Integer[]) Bindings.adapt(array1, b1, b2);\r
123                 ArrayList<Integer> array3 = (ArrayList<Integer>) Bindings.adapt(array2, b2, b3);\r
124                 \r
125                 System.out.println( "Adapting classes:");\r
126                 System.out.println( " int[]              = "+Arrays.toString(array1) );\r
127                 System.out.println( " Integer[]          = "+Arrays.toString(array2) );\r
128                 System.out.println( " ArrayList<Integer> = "+array3 );\r
129                 System.out.println();\r
130                 \r
131                 \r
132                 \r
133                 //\r
134                 // Binding can create a default value for any data type.\r
135                 // Default value is specified at http://dev.simantics.org/index.php/index.php/Databoard_Specification#Default_Value \r
136                 //              \r
137                 value = (Integer) binding.createDefault();\r
138                 \r
139                 // Default value is always valid.\r
140                 Datatype type = new IntegerType(null, "[100..200]");\r
141                 binding = new IntegerBindingDefault( (IntegerType) type );\r
142                 value = (Integer) binding.createDefault();\r
143                 System.out.println("Default Value for value for "+type+" is "+value);\r
144                 \r
145                 //\r
146                 // Binding can create a valid random value\r
147                 //\r
148                 value = (Integer) binding.createRandom(23243);\r
149                 System.out.println("Random Value for value for "+type+" is "+value);\r
150                 \r
151                 \r
152                 // \r
153                 // Values can be serialized to byte[] and back to java Objects\r
154                 // Binary serialization format is specified here, \r
155                 // http://dev.simantics.org/index.php/Databoard_Specification#Binary_Serialization_Format\r
156                 //\r
157                 Serializer s = Bindings.getSerializer( binding );\r
158                 value = 50;\r
159                 byte[] data = s.serialize( value );\r
160                 value = (Integer) s.deserialize( data );\r
161                 \r
162                 \r
163                 //\r
164                 // Values can be converted to ascii notation and back\r
165                 // Ascii notation is specified at http://dev.simantics.org/index.php/index.php/Databoard_Specification#Datatypes\r
166                 //\r
167                                 \r
168                 \r
169         }\r
170         \r
171 }\r
172 \r