Imagine I have two objects ("from" and "to").
I need to scan object "from" for all getters. If object "to" contains correspondent setter, it will invoke it to set property value for "to" which equals to the property of "from".
The type in setter should be compatible to the value returned by getter (if not, no invocation performed). Compatible means that parameter type in setter should be the same or be superclass of the return type of the getter. I'm sure I know the approach to solve this task. I need to write some method "assign", for example:
public static void assign(Object to, Object from) {
Class<?> classTo = to.getClass();
Class<?> classFrom = from.getClass();
Method[] methodsTo = classTo.getMethods();
Method[] methodsFrom = classFrom.getMethods();
for (Method methodFrom : methodsFrom) {
if...
}
}
And for each get-method I need to go through the list of set-methods and the result of invoking get-method pass as a parameter to correspondent set-method.
In this case complexity of this algorithm will be n*m (where n - number of methods in a first class, m - in second).
So my question is - how to solve this task with the algorithm whose complexity will be equals n + m?
Are there any well known algorithms or maybe I should use an implementation of one of the some data structures (hashmap for example)?
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire