=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);