X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc-isv%2Fdoc%2Fadapter.mediawiki;h=dddf060476d942335bc98e97c8b6aef709b61b23;hb=refs%2Fchanges%2F68%2F468%2F2;hp=8cead3650c353c845560abd48edd087d88e031ef;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git
diff --git a/bundles/org.simantics.databoard/src-isv/doc/adapter.mediawiki b/bundles/org.simantics.databoard/src-isv/doc/adapter.mediawiki
index 8cead3650..dddf06047 100644
--- a/bundles/org.simantics.databoard/src-isv/doc/adapter.mediawiki
+++ b/bundles/org.simantics.databoard/src-isv/doc/adapter.mediawiki
@@ -1,105 +1,105 @@
-=Adapter=
-There can be different Java Class Bindings for a single data type. For example, Double
type can be have bindings DoubleJavaBinding
and MutableDoubleBinding
to two respective classes java.lang.Double
and MutableDouble
. Instance of one binding can be adapted to instance of another with an [../javadoc/org/simantics/databoard/adapter/Adapter.html|Adapter]
.
-
-Adapter can be created automatically or implemented self.
-
- Adapter adapter = new Adapter() { ... }; - Adapter adapter = Bindings.getAdapter( domainBinding, rangeBinding ); -- -Example: -
- Adapter adapter = Bindings.getAdapter(Bindings.MUTABLE_DOUBLE, Bindings.DOUBLE); - java.lang.Double double = adapter.adapt( new MutableDouble(5.0) ); -- -There is also convenience. -
- java.lang.Double double = Bindings.adapt( new MutableDouble(5.0), Bindings.MUTABLE_DOUBLE, Bindings.DOUBLE ); -- -The argument given to
Adapter#adapt(Object)
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.
-- Adapter cloner = Bindings.adapterCache.getAdapter(domain, range, false, true); - cloner.adapt( ... ); - - Rectangle2D rect2 = Bindings.clone( rect1, rectBinding, rectBinding ); -- -==Type Conversion== -In some cases different types may be are type-conversion compatible. An instance of one type is convertible to instance of another. - -'''Engineering Units of same quantity are convertible.''' -
- class CarSI { - String modelName; - @Unit("km/h") double maxVelocity; - @Unit("kg") double mass; - @Unit("cm") double length; - @Unit("kW") double power; - } - - class CarIm { - String modelName; - @Unit("mph") float maxVelocity; - @Unit("lbs") float mass; - @Unit("ft") float length; - @Unit("hp(M)") float power; - } - - Adapter si2imAdapter = Bindings.getTypeAdapter( - Bindings.getBinding(CarSI.class), - Bindings.getBinding(CarIm.class) ); - - CarIm americanCarInfo = si2imAdapter.adapt( europeanCarInfo ); -- -'''Primitive Types.''' Note, primitive adapter throws an exception at runtime if values are not adaptable. -
- Adapter adapter = getTypeAdapter( integerBinding, doubleBinding ); - Double double = adapter.adapt( new Integer( 5 ) ); -- -'''Records are matched by field names.''' -
- class Foo { - int x, y, z; - } - class Bar { - int z, y, x; - } - Adapter adapter = getTypeAdapter( fooBinding, barBinding ); -- -'''Subtype to supertype:''' Note, this conversion cannot be not symmetric, supertypes cannot be converted to subtypes. -
- class Node { - String id; - } - class ValueNode extends Node { - Object value; - } - Adapter adapter = getTypeAdapter( valueNodeBinding, nodeBinding ); -- -'''Non-existing fields to Optional fields''' -
- class Node { - String id; - } - class NominalNode { - String id; - @Optional String name; - } - Adapter adapter = getTypeAdapter( nodeBinding, nominalNodeBinding ); -- -'''Enumerations''' -
- enum Cars { Audio, BMW, Mercedes, Honda, Mazda, Toyota, Ford, Mitsubishi, Nissan, GM } - enum JapaneseCars { Honda, Mazda, Toyota, Nissan, Mitsubishi } - - Binding carsBinding = Bindings.getBinding( Cars.class ); - Binding japaneseCarsBinding = Bindings.getBinding( JapaneseCars.class ); - Adapter adapter = Bindings.adapterCache.getAdapter(japaneseCarsBinding, carsBinding, true, false); +=Adapter= +There can be different Java Class Bindings for a single data type. For example,Double
type can be have bindingsDoubleJavaBinding
andMutableDoubleBinding
to two respective classesjava.lang.Double
andMutableDouble
. Instance of one binding can be adapted to instance of another with an[../javadoc/org/simantics/databoard/adapter/Adapter.html|Adapter]
. + +Adapter can be created automatically or implemented self. ++ Adapter adapter = new Adapter() { ... }; + Adapter adapter = Bindings.getAdapter( domainBinding, rangeBinding ); ++ +Example: ++ Adapter adapter = Bindings.getAdapter(Bindings.MUTABLE_DOUBLE, Bindings.DOUBLE); + java.lang.Double double = adapter.adapt( new MutableDouble(5.0) ); ++ +There is also convenience. ++ java.lang.Double double = Bindings.adapt( new MutableDouble(5.0), Bindings.MUTABLE_DOUBLE, Bindings.DOUBLE ); ++ +The argument given toAdapter#adapt(Object)
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. ++ Adapter cloner = Bindings.adapterCache.getAdapter(domain, range, false, true); + cloner.adapt( ... ); + + Rectangle2D rect2 = Bindings.clone( rect1, rectBinding, rectBinding ); ++ +==Type Conversion== +In some cases different types may be are type-conversion compatible. An instance of one type is convertible to instance of another. + +'''Engineering Units of same quantity are convertible.''' ++ class CarSI { + String modelName; + @Unit("km/h") double maxVelocity; + @Unit("kg") double mass; + @Unit("cm") double length; + @Unit("kW") double power; + } + + class CarIm { + String modelName; + @Unit("mph") float maxVelocity; + @Unit("lbs") float mass; + @Unit("ft") float length; + @Unit("hp(M)") float power; + } + + Adapter si2imAdapter = Bindings.getTypeAdapter( + Bindings.getBinding(CarSI.class), + Bindings.getBinding(CarIm.class) ); + + CarIm americanCarInfo = si2imAdapter.adapt( europeanCarInfo ); ++ +'''Primitive Types.''' Note, primitive adapter throws an exception at runtime if values are not adaptable. ++ Adapter adapter = getTypeAdapter( integerBinding, doubleBinding ); + Double double = adapter.adapt( new Integer( 5 ) ); ++ +'''Records are matched by field names.''' ++ class Foo { + int x, y, z; + } + class Bar { + int z, y, x; + } + Adapter adapter = getTypeAdapter( fooBinding, barBinding ); ++ +'''Subtype to supertype:''' Note, this conversion cannot be not symmetric, supertypes cannot be converted to subtypes. ++ class Node { + String id; + } + class ValueNode extends Node { + Object value; + } + Adapter adapter = getTypeAdapter( valueNodeBinding, nodeBinding ); ++ +'''Non-existing fields to Optional fields''' ++ class Node { + String id; + } + class NominalNode { + String id; + @Optional String name; + } + Adapter adapter = getTypeAdapter( nodeBinding, nominalNodeBinding ); ++ +'''Enumerations''' ++ enum Cars { Audio, BMW, Mercedes, Honda, Mazda, Toyota, Ford, Mitsubishi, Nissan, GM } + enum JapaneseCars { Honda, Mazda, Toyota, Nissan, Mitsubishi } + + Binding carsBinding = Bindings.getBinding( Cars.class ); + Binding japaneseCarsBinding = Bindings.getBinding( JapaneseCars.class ); + Adapter adapter = Bindings.adapterCache.getAdapter(japaneseCarsBinding, carsBinding, true, false);\ No newline at end of file