-
Notifications
You must be signed in to change notification settings - Fork 1
Object Mapping
RPF contains a basic Object Mapping interface, that can be used in virtually any occasion where object mapping is needed. It has two type parameters, the data type, and object type. Then, two required methods to convert the two types to and from each other.
This object mapper uses the levenshtein distance algorithm to determine the best variable match from string keys in a map. This class is abstract, but not designed to be extended. It's abstract due to it's usage of a little hack in java that allows you to get the generic of abstract classes at runtime, allowing you to initialise the class like this:
final ObjectMapper<Map<String, Object>, Person> mapper = new LevenshteinObjectMapper<Person>(){};
The class will then find the class Person
, purely from it's type parameter. Naturally there's also a constructor option available, which doesn't find the class dynamically from the type parameter.
Regarding the conversion logic from data to type, the mapper will first check the class for constructors that match the data types inputted. If one is present, it'll use it. If no suitable constructor is present, it'll instead create a blank instance of your class (assuming there's a no-args-constructor), and set the field values via reflection. This is where the levenshtein algorithm is used; against field names.