]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src-isv/doc/adapter.mediawiki
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src-isv / doc / adapter.mediawiki
1 =Adapter=
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>.
3
4 Adapter can be created automatically or implemented self.
5 <pre class="code">
6     Adapter adapter = new Adapter() { ... };
7     Adapter adapter = Bindings.getAdapter( domainBinding, rangeBinding );
8 </pre>
9
10 Example:
11 <pre class="code">
12     Adapter adapter = Bindings.getAdapter(Bindings.MUTABLE_DOUBLE, Bindings.DOUBLE);
13     java.lang.Double double = adapter.adapt( new MutableDouble(5.0) );
14 </pre>
15
16 There is also convenience.
17 <pre class="code">
18     java.lang.Double double = Bindings.adapt( new MutableDouble(5.0), Bindings.MUTABLE_DOUBLE, Bindings.DOUBLE );
19 </pre>
20
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.
22 <pre class="code">
23     Adapter cloner = Bindings.adapterCache.getAdapter(domain, range, false, true);
24     cloner.adapt( ... );
25
26     Rectangle2D rect2 = Bindings.clone( rect1, rectBinding, rectBinding );
27 </pre>
28
29 ==Type Conversion==
30 In some cases different types may be are type-conversion compatible. An instance of one type is convertible to instance of another.
31
32 '''Engineering Units of same quantity are convertible.'''
33 <pre class="code">
34     class CarSI {
35         String modelName;               
36         @Unit("km/h") double maxVelocity;               
37         @Unit("kg") double mass;                
38         @Unit("cm") double length; 
39         @Unit("kW") double power;
40     }
41     
42     class CarIm {
43         String modelName;               
44         @Unit("mph") float maxVelocity;         
45         @Unit("lbs") float mass;                
46         @Unit("ft") float length; 
47         @Unit("hp(M)") float power;
48     }
49     
50     Adapter si2imAdapter = Bindings.getTypeAdapter(
51         Bindings.getBinding(CarSI.class), 
52         Bindings.getBinding(CarIm.class) );
53     
54     CarIm americanCarInfo = si2imAdapter.adapt( europeanCarInfo );
55 </pre>
56
57 '''Primitive Types.''' Note, primitive adapter throws an exception at runtime if values are not adaptable.
58 <pre class="code">
59     Adapter adapter = getTypeAdapter( integerBinding, doubleBinding );
60     Double double = adapter.adapt( new Integer( 5 ) );
61 </pre>
62
63 '''Records are matched by field names.'''
64 <pre class="code">
65     class Foo {
66         int x, y, z;
67     }
68     class Bar {
69         int z, y, x;
70     }
71     Adapter adapter = getTypeAdapter( fooBinding, barBinding );
72 </pre>    
73
74 '''Subtype to supertype:''' Note, this conversion cannot be not symmetric, supertypes cannot be converted to subtypes.
75 <pre class="code">
76     class Node {
77         String id;
78     }
79     class ValueNode extends Node {
80         Object value;
81     }
82     Adapter adapter = getTypeAdapter( valueNodeBinding, nodeBinding );
83 </pre>
84
85 '''Non-existing fields to Optional fields'''
86 <pre class="code"> 
87     class Node {
88         String id;
89     }
90     class NominalNode {
91         String id;
92         @Optional String name;
93     }
94     Adapter adapter = getTypeAdapter( nodeBinding, nominalNodeBinding );
95 </pre>    
96
97 '''Enumerations'''
98 <pre class="code">
99     enum Cars { Audio, BMW, Mercedes, Honda, Mazda, Toyota, Ford, Mitsubishi, Nissan, GM }
100     enum JapaneseCars { Honda, Mazda, Toyota, Nissan, Mitsubishi }
101     
102     Binding carsBinding = Bindings.getBinding( Cars.class );
103     Binding japaneseCarsBinding = Bindings.getBinding( JapaneseCars.class );
104     Adapter adapter = Bindings.adapterCache.getAdapter(japaneseCarsBinding, carsBinding, true, false);
105 </pre>