2 There can be different Java Class Bindings for a single data type. For example, <code>Double</code> type can be have bindings <code>DoubleJavaBinding</code> and <code>MutableDoubleBinding</code> to two respective classes <code>java.lang.Double</code> and <code>MutableDouble</code>. Instance of one binding can be adapted to instance of another with an <code>[../javadoc/org/simantics/databoard/adapter/Adapter.html|Adapter]</code>.
4 Adapter can be created automatically or implemented self.
6 Adapter adapter = new Adapter() { ... };
7 Adapter adapter = Bindings.getAdapter( domainBinding, rangeBinding );
12 Adapter adapter = Bindings.getAdapter(Bindings.MUTABLE_DOUBLE, Bindings.DOUBLE);
13 java.lang.Double double = adapter.adapt( new MutableDouble(5.0) );
16 There is also convenience.
18 java.lang.Double double = Bindings.adapt( new MutableDouble(5.0), Bindings.MUTABLE_DOUBLE, Bindings.DOUBLE );
21 The argument given to <code>Adapter#adapt(Object)</code> may be re-used in the result unless the adapter is a cloning adapter which guarantees a clone. Note, even wih cloning adapters immutable classes, (eg java.lang.Integer) are never cloned.
23 Adapter cloner = Bindings.adapterCache.getAdapter(domain, range, false, true);
26 Rectangle2D rect2 = Bindings.clone( rect1, rectBinding, rectBinding );
30 In some cases different types may be are type-conversion compatible. An instance of one type is convertible to instance of another.
32 '''Engineering Units of same quantity are convertible.'''
36 @Unit("km/h") double maxVelocity;
37 @Unit("kg") double mass;
38 @Unit("cm") double length;
39 @Unit("kW") double power;
44 @Unit("mph") float maxVelocity;
45 @Unit("lbs") float mass;
46 @Unit("ft") float length;
47 @Unit("hp(M)") float power;
50 Adapter si2imAdapter = Bindings.getTypeAdapter(
51 Bindings.getBinding(CarSI.class),
52 Bindings.getBinding(CarIm.class) );
54 CarIm americanCarInfo = si2imAdapter.adapt( europeanCarInfo );
57 '''Primitive Types.''' Note, primitive adapter throws an exception at runtime if values are not adaptable.
59 Adapter adapter = getTypeAdapter( integerBinding, doubleBinding );
60 Double double = adapter.adapt( new Integer( 5 ) );
63 '''Records are matched by field names.'''
71 Adapter adapter = getTypeAdapter( fooBinding, barBinding );
74 '''Subtype to supertype:''' Note, this conversion cannot be not symmetric, supertypes cannot be converted to subtypes.
79 class ValueNode extends Node {
82 Adapter adapter = getTypeAdapter( valueNodeBinding, nodeBinding );
85 '''Non-existing fields to Optional fields'''
92 @Optional String name;
94 Adapter adapter = getTypeAdapter( nodeBinding, nominalNodeBinding );
99 enum Cars { Audio, BMW, Mercedes, Honda, Mazda, Toyota, Ford, Mitsubishi, Nissan, GM }
100 enum JapaneseCars { Honda, Mazda, Toyota, Nissan, Mitsubishi }
102 Binding carsBinding = Bindings.getBinding( Cars.class );
103 Binding japaneseCarsBinding = Bindings.getBinding( JapaneseCars.class );
104 Adapter adapter = Bindings.adapterCache.getAdapter(japaneseCarsBinding, carsBinding, true, false);