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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.databoard.example;
\r
13 import java.util.ArrayList;
\r
14 import java.util.Arrays;
\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
27 public class BindingExample {
\r
29 @SuppressWarnings("unchecked")
\r
30 public static void main(String[] args) throws Exception {
\r
33 // Binding is an object that makes modifications and conversions
\r
34 // between Datavalues and Java Objects possible.
\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
40 // For example, IntegerBinding binds to Integer datatype. It is abstract
\r
41 // class and does not bind to any specific java class.
\r
43 // It's sub-classes IntegerJavaBinding and MutableIntegerBinding bind to
\r
44 // specific classes (java.lang.Integer and MutableInteger).
\r
47 IntegerBinding binding = Bindings.INTEGER;
\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
52 // We know that the binding class binds java.lang.Integer, we can typecast
\r
53 Integer number = (Integer) obj;
\r
55 // We can read the value in a Java Object into a integer value
\r
56 int value = binding.getValue_( obj );
\r
58 // The object can be cloned. Immutable classes are recycled
\r
59 Object obj2 = binding.clone( obj );
\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
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
76 // Bindings can do hashCode and equals aswell
\r
78 binding.hashValue( 5 );
\r
79 binding.equals(5, 6);
\r
83 // Data types can have restrictions. Bindings can validate that datavalues
\r
84 // are valid according to the restrictions of the type.
\r
86 binding = new IntegerBindingDefault( new IntegerType(null, "[0..10]") );
\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
97 System.out.print("Validating "+value+" ");
\r
98 binding.assertInstaceIsValid(value);
\r
99 } catch (BindingException e) {
\r
100 System.out.println(e);
\r
102 System.out.println();
\r
106 // Bindings can be acquired from class description
\r
108 Binding b1 = Bindings.getBinding( int[].class );
\r
109 Binding b2 = Bindings.getBinding( Integer[].class );
\r
112 // Bindings can be acquired from a datatype
\r
114 Datatype arrayType = new ArrayType( Datatypes.INTEGER ); /* Integer[] */
\r
115 Binding b3 = Bindings.getMutableBinding( arrayType ); /* ArrayList<Integer> */
\r
119 // Objects can be converted between bindings
\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
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
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
137 value = (Integer) binding.createDefault();
\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
146 // Binding can create a valid random value
\r
148 value = (Integer) binding.createRandom(23243);
\r
149 System.out.println("Random Value for value for "+type+" is "+value);
\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
157 Serializer s = Bindings.getSerializer( binding );
\r
159 byte[] data = s.serialize( value );
\r
160 value = (Integer) s.deserialize( data );
\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