Make custom Writer and Reader
As mentioned before, the Value
interface is to storage the cost information into a database. Diana already has support to the Java type such as primitives types, wrappers types, new Java 8 date time. Furthermore, the developer can create custom converter easily and quickly. It has two interfaces:
ValueWriter
: This interface represents aValue
instance to write in a database.ValueReader
: This interface represents how theValue
will convert to Java application. This interface will use on the<T> T get(Class<T> clazz)
and<T> T get(TypeSupplier<T> typeSupplier)
.
Both class implementations load from Java SE ServiceLoader resource. So, to Diana learn a new type just register on ServiceLoader, e.g., Given a Money type:
Just to be more didactic the book creates a simple money representation. As everyone knows that is not a good practice reinventing the wheel, so in production the Java Developer must use mature Money APIS such as moneta that is the reference implementation of JSR 354.
The first step is to create the converter to a custom type to a database, the ValueWriter
. It has two methods:
boolean isCompatible(Class clazz)
: Check if the given class has support for this implementation.S write(T object)
: Once the implementation supports the type, the next step converts aT
instance toS
type.
With the MoneyValueWriter
created and the Money type will save as String
, then the next step is read information to Java application. As can be seen, a ValueReader
implementation. This interface has two methods:
boolean isCompatible(Class clazz)
; Check if the given class has support for this implementation.<T> T read(Class<T> clazz, Object value)
; Converts to theT
type from Object instance.
After all, the both implementation were done, the last step is to register them into two files:
META-INF/services/org.jnosql.diana.api.ValueReader
META-INF/services/org.jnosql.diana.api.ValueWriter
Each file will have the qualified of this respective implementation:
The file org.jnosql.diana.api.ValueReader
will have:
The file org.jnosql.diana.api.ValueWriter
will have:
Last updated