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