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