2 Binding is a mechanism for mapping a Java Class to a Datatype.
3 For example, take a java.lang.Double. The instance is a container (<code>private final double value;</code>) for the data and its Binding (DoubleBinding) is a way to access the data (<code>.valueOf()</code>, <code>.getDouble()</code>).
4 Java Object + Binding = Databoard Value
6 Bindings have the exact same composition tree structure as its respective <code>Datatype</code> - structural types have structural Bindings, and primitive types a single binding. To acquire a binding, the developer can use a utility that creates one using Java reflection functions.
8 Binding binding = Binding.getBinding( Double.class );
11 Sometimes classes cannot bound using automatic tool, for instance when using 3rd party classes which cannot be modified. The developer must then write binding self by sub-classing on of the 13 base binding classes (There is a base binding class for each Datatype).
13 Binding binding = new RecordBinding() { ... };
16 '''[../javadoc/org/simantics/databoard/binding|org.simantics.databoard.binding]'''.
17 {| style="background-color: #f9f9f9; border: 1px solid #aaaaaa; "
18 |- style="background-color: #e9e9e9; " |
19 | '''Class''' || '''Description'''
21 | [../javadoc/org/simantics/databoard/binding/DataBinding.html|DataBinding]
22 | Base class for all data Bindings
24 | [../javadoc/org/simantics/databoard/binding/RecordBinding.html|RecordBinding]
27 | [../javadoc/org/simantics/databoard/binding/ArrayBinding.html|ArrayBinding]
28 | Array - an ordered sequence of elements of one value.
30 | [../javadoc/org/simantics/databoard/binding/MapBinding.html|MapBinding]
31 | Map - an ''ordered'' map of keys to values.
33 | [../javadoc/org/simantics/databoard/binding/UnionBinding.html|UnionBinding]
36 | [../javadoc/org/simantics/databoard/binding/BooleanBinding.html|BooleanBinding],[../javadoc/org/simantics/databoard/binding/IntBinding.html|IntBinding],[../javadoc/org/simantics/databoard/binding/LongBinding.html|LongBinding],[../javadoc/org/simantics/databoard/binding/FloatBinding.html|FloatBinding],[../javadoc/org/simantics/databoard/binding/DoubleBinding.html|DoubleBinding]
37 | Primitive and numeric Bindings
39 | [../javadoc/org/simantics/databoard/binding/StringBinding.html|StringBinding]
42 | [../javadoc/org/simantics/databoard/binding/OptionalBinding.html|OptionalBinding]
45 | [../javadoc/org/simantics/databoard/binding/VariantBinding.html|VariantBinding]
50 Binding can be acquired or created using one of the following methods:
53 * Reflection-Read from a Class
54 * Created using [../javadoc/org/simantics/databoard/bindingscheme/BindingScheme.html|BindingScheme]
56 Binding binding = new DoubleBinding( doubleType );
57 Binding binding = new RecordBinding() { ... };
58 Binding binding = Bindings.DOUBLE;
59 Binding binding = Binding.getBinding( Double.class );
60 Binding binding = Binding.getBinding( Datatypes.DOUBLE );
64 '''Data Type and Binding can be read automatically from a Class by utility.'''
66 Datatype type = Datatypes.getDatatype( Foo.class );
67 Binding binding = Bindings.getBinding( Foo.class );
70 Bindings for generics classes can be created by passing arguments.
72 Binding e = Bindings.getBinding(List.class, String.class);
73 List<String> list = (List<String>) e.createRandom(5);
75 Binding binding = Bindings.getBinding( Map.class, Integer.class, Integer.class );
76 Map<Integer, Integer> value = (Map<Integer, Integer>) binding.createDefault();
79 Even cascading generics...
81 Binding e = Bindings.getBinding(List.class, List.class, String.class);
82 List<List<String>> listList = (List<List<String>>) e.createRandom(5);
85 '''Classes are RecordTypes'''
91 Is a binding to the following Datatype
93 type Foo = { x : Integer, y : Integer, z : Integer }
96 '''There are three types of classes supported, and therefore three ways how objects are constructed.'''
97 If you create binding for your class with Bindings#getBinding( clazz ), the class must adhere one of these format. You may have to add annotations such as @Recursive, @Optional, @Arguments.
99 ''Record-like classes:''
107 ''Immutable classes:''
111 private Object value;
113 public Foo(String name, Object value) {
118 public String getName() {
122 public Object getValue() {
129 ''Bean-like classes:''
133 private Object value;
135 public void setName(String name) {
139 public void setValue(Object value) {
143 public String getName() {
147 public Object getValue() {
154 '''Static and transient fields are omited:'''
156 static final long serialVersionUID = -3387516993124229943L;
157 transient int hashCode;
160 '''Enumerations are Union Types'''
162 enum Cars { Ferrari, Porche, Lamborghini, Jaguar }
164 is interpreted as union type
165 type Cars = | Ferrari | Porche | Lamborghini | Jaguar
167 If you cannot modify the class, you have to create binding for it by subclassing base binding classes, eg. RecordBinding.
169 '''Other exceptions:'''
170 *<code>java.lang.Object</code> is <tt>Variant</tt>.
171 *<code>java.lang.Set<T></code> is <tt>Map(T, {})</tt>.
172 *<code>java.lang.TreeSet<T></code> is <tt>Map(T, {})</tt>.
173 *<code>java.lang.HashSet<T></code> is <tt>Map(T, {})</tt>. (Note HashSet binding has very low performance.)
174 *<code>java.lang.Map<K, V></code> is <tt>Map(K, V)</tt>.
175 *<code>java.lang.TreeMap<K, V></code> is <tt>Map(K, V)</tt>.
176 *<code>java.lang.HashMap<K, V></code> is <tt>Map(K, V)</tt>. (Note HashMap binding has very low performance.)
177 *<code>java.lang.List<T></code> is <tt>Array(T)</tt>.
178 *<code>java.lang.ArrayList<T></code> is <tt>Array(T)</tt>.
179 *<code>java.lang.LinkedList<T></code> is <tt>Array(T)</tt>.
180 *<code>void</code> is <tt>{}</tt>.
181 *The stacktrace of <code>Exception.class</code> is omited.
184 Java Classes / Fields can be annotated with the following annotations ('''[../javadoc/org/simantics/databoard/annotations/|org.simantics.databoard.annotations]]''').
187 '''UnionTypes are abstract classes or interfaces with <code>@Union</code> annotation.'''
189 @Union({A.class, B.class}) interface Union1 {
192 class A implements Union1 {
196 class B implements Union1 {
201 '''<code>@Referable</code> denotes that the class has recursion and is a referable record.'''
203 public @Referable class Node {
204 public Node[] children;
208 '''Fields that can have <tt>null</tt> value have <code>@Optional</code> annotation.'''
210 @Optional String name;
213 '''String valid values are set with <code>@Pattern</code> as regular expression. ([http://en.wikipedia.org/wiki/Regular_expression])'''
215 String @Pattern("(19|20)\\d\\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])") date;
217 type Date = String( Pattern = "(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])" )
220 '''String content type is set with a <code>@MIMEType</code>. ([http://en.wikipedia.org/wiki/Mime_type MIME Type])'''
222 @MIMEType("text/xml") String document;
225 '''Array size restricted with @Length.'''
227 @Length("[0..10]") int[] array;
228 @Length({"[320]", "[240]"}) int[][] image;
231 '''Valid numeric range is set with @Range.'''
233 @Range("[0..100]") double alpha;
234 @Range("[0..]" double length;
237 '''<tt>Range</tt> and <tt>Length</tt> notation:'''
241 *Inclusive range "[0..100]"
242 *Exclusive range "(0..100)"
243 *Inclusive lower bound and exclusive upper bound "[0..100)"
246 '''Engineering unit type is given with @Unit.'''
248 @Unit("km/h") double maxVelocity;
251 '''The serializer generated with reflection can be overriden with @SpecializedSerializer'''
253 @SpecializedSerializer(MySerializer.class)
254 public class MyRecord {
260 A ''binding scheme'' associates some data types with a unique binding. The mapping of types to bindings is bijective, there is one binding for each type and vice-versa.
262 <code>DefaultBindingScheme</code> is a scheme that converts any datatype to a binding. It prefers java.lang.X primitives.
263 The Class mapping for each type is listed below.
264 {| style="background-color: #f9f9f9; border: 1px solid #aaaaaa; "
265 |- style="background-color: #e9e9e9;
266 | '''Type''' || '''Class'''
268 | <code>BooleanType</code> || <code>Boolean.class</code>
270 | <code>ByteType</code> || <code>Byte.class</code>
272 | <code>FloatType</code> || <code>Float.class</code>
274 | <code>DoubleType</code> || <code>eDouble.class</code>
276 | <code>IntegerType</code> || <code>Integer.class</code>
278 | <code>LongType</code> || <code>Long.class</code>
280 | <code>StringType</code> || <code>String.class</code>
282 | <code>UnionType</code> || <code>TaggedObject.class</code>
284 | <code>OptionType</code> || <code>ValueContainer.class</code>
286 | <code>RecordType</code> || <code>Object[].class</code>
288 | <code>ArrayType</code> || <code>ArrayList.class</code>
290 | <code>Array(Byte)</code> || <code>byte[].class</code>
292 | <code>MapType</code> || <code>TreeMap.class</code>
294 | <code>VariantType</code> || <code>Variant.class</code>
297 <code>MutableBindingScheme</code> is a scheme that provides a fully implementing mutable binding for all data types.
298 The Class mapping for each type is listed below.
300 {| style="background-color: #f9f9f9; border: 1px solid #aaaaaa; "
301 |- style="background-color: #e9e9e9; " |
302 | '''Type''' || '''Class'''
304 | <code>BooleanType</code> || <code>MutableBoolean.class</code>
306 | <code>ByteType</code> || <code>MutableByte.class</code>
308 | <code>FloatType</code> || <code>MutableFloat.class</code>
310 | <code>DoubleType</code> || <code>MutableDouble.class</code>
312 | <code>IntegerType</code> || <code>MutableInt.class</code>
314 | <code>LongType</code> || <code>MutableLong.class</code>
316 | <code>StringType</code> || <code>MutableString.class</code>
318 | <code>UnionType</code> || <code>TaggedObject.class</code>
320 | <code>OptionType</code> || <code>ValueContainer.class</code>
322 | <code>RecordType</code> || <code>Object[].class</code>
324 | <code>ArrayType</code> || <code>ArrayList.class</code>
326 | <code>MapType</code> || <code>TreeMap.class</code>
328 | <code>VariantType</code> || <code>Variant.class</code>
333 [../javadoc/org/simantics/databoard/serialization/binary/Serializer.html|Serializer] is a class that serializes Values into and from binary serialization format. It follows the Databoard [Databoard_Specification#Binary_File_Format|Binary File Format].
335 Binding binding = Bindings.DOUBLE;
336 Serializer serializer = Bindings.getSerializer( binding );
337 byte[] data = serializer.serialize( new Double( 100.0 ) );
339 Double value = (Double) serializer.deserialize( data );
342 Files can be partially accessed using BinaryAccessor, see [accessor|Accessors]. This is useful when handling larger than memory files.
345 '''Value''' can be ''well-formed'' or ''valid''.
346 The domain of valid values are defined with restrictions in data types, and <code>@Length</code>, <code>@Range</code>, <code>@Pattern</code> and <code>@MIMEType</code> Annotations in Classes
348 Validation mechanism in Binding asserts that the instance is a valid value of the respective Data Type.
351 Binding.assertInstaceIsValid( object );
352 } catch( BindingException e ) {
358 *<tt>Binding</tt> is a <tt>Comparator</tt>, all data values are comparable, the order is defined in [[Databoard_Specification#Comparison|Specification]].
359 *<tt>Binding#createDefault()</tt> creates a valid instance of the Datatype.
360 *<tt>Binding#createRandom(int)</tt> creates a valid instance with random values. Useful for unit tests.
361 *<tt>Binding#clone(Object)</tt> creates a new instance with same content.
362 *<tt>Binding#readFrom(Object, Binding, Binding)</tt> copies contents from another object of same type.
364 ===Parsing & Printing===
366 Data values are printed and parsed of the [[Databoard_Specification|Text notation]] with the following <code>Binding</code> methods:
368 String text = binding.printValue( value, true );
370 Object value = binding.parseValue( text );
372 And also to value definitions <tt>''name : type = value''</tt>
375 StringBuilder sb = new StringBuilder();
376 DataValueRepository repo = new DataValueRepository();
377 repo.put( "temp", binding, value );
378 binding.printValue( value, sb, repo, true );
379 String text = sb.toString();
381 Object value = binding.parseValueDefinition( text );