Custom type adapters - Zalizyaka-Ney/handy.storage GitHub Wiki
If you want the framework to support a type that is not supported out of box - just create a custom type adapter for that type:
public class BigDecimalTypeAdapter extends CustomTypeAdapter<BigDecimal> {
@Override
protected String valueToString(BigDecimal value) {
return value.toPlainString();
}
@Override
protected BigDecimal parseValue(String s) {
return new BigDecimal(s);
}
}
Any equal Java objects must be represented by the same value in the database and vice versa, so it's highly recommended to have a coherent implementation of equals() method for your data types.
Also, it's posible to customize a way of object creation during reading from a database (in this case you don't need a default constructor):
public class PersonCreator implements ObjectCreator<Person> {
@Override
public Person createObject(CursorValues values) {
String name = values.getValue(Person.NAME);
String surname = values.getValue(Person.SURNAME);
int age = values.getValue(Person.AGE);
return new Person(name, surname, age);
}
}
Those customizations must be registered during HandyStorage creation:
HandyStorage handyStorage = new HandyStorage.Builder()
.setTypeAdapter(BigDecimal.class, new BigDecimalTypeAdapter())
.setObjectCreator(Person.class, new PersonCreator())
.build();