Merge two object in One
Hi,
I have two objects of the same Class instaciated such as:
Object obj1 {field1="", field2="cat",field3="dog"};
Object obj2 {field1="pig", field2="",field3=""};
I would like to have a method merge(obj1, obj2) that returns an object of the same class:
Object obj3 {field1="pig", field2="cat",field3="dog"};
Any idea of the best way to do so?
Thanks a lot,
G.
# 1
the difficult bit is deciding what "merge" means, and then explaining it to the JVM. what do you think merge means?
# 2
thanks for the quick feedback. Merge meaning all fields from the return object will be completed with the non empty or null values of the objects in parameter.
Exemple:
public class ExampleDto extends TemplateDto implements Dto {
private static final long serialVersionUID = 3013116528295502747L;
private String conseillerFinancierNomComplet = "";
private String conseillerFinancierTelephone = "";
private String conseillerFinancierMobile = "";
//+Setters and Getters
}
I have obj1 an instance of ExampleDto instanciated with the following values:
private static final long serialVersionUID = 3013116528295502747L;
private String name= "";
private String phone = "415.55.55.55";
private String address = "";
I have obj2 an instance of ExampleDto instanciated with the following values:
private static final long serialVersionUID = 3013116528295502747L;
private String name= "John";
private String phone = "";
private String address = "San Francisco";
I would like to call a method obj3 = merge(obj1,obj2) that returns an object of the class ExampleDto instanciated (it can be obj1 or obj2) with the completed values:
private static final long serialVersionUID = 3013116528295502747L;
private String name= "John";
private String phone = "415.55.55.55";
private String address = "San Francisco";
G.
# 3
how will you ensure that the data makes sense? that's the real problem, deciding whether the merge makes sense. for example, if I try to merge the following 2 objects, what should happen?
String name = "John";
String phone = "555-546846";
String address = "";
and
String name = "Bill";
String phone = "555-927839";
String address = "timbuktu";
you end up with an object that doesn't represent either John or Bill properly. what about this:
String name =- "John";
int balance = 4504;
and
String name = "";
int balance = 0;
now, how will you know whether the second one has a balance of '0' or if the balance just hasn't been set yet?
filling in the "blank" fields is easy, but it won't necessarily make any sense. you'll end up with customer records that are part about one customer, part about another, and wholly useless as a result. what's the bigger picture here?
# 4
In my case, it's really easy. (it's for a report purpose)- The fields are all String. - The case you mentioned is not possible for me ( the same field cannot have 2 diff. values)- I just want to complete the obj1 with the values of obj2 if possible.
# 5
1) put all the values of fields from instance 'A' in an array or a collection
2) put all the values of fields from instance 'B' in an array or a collection
3) iterate over the first array/collection and whenever you encounter an empty string, grab the corresponding one from the second array/collection
# 6
and after doing so, using reflection is the only way to instanciate the values to the object...right?thanks a lot!GMessage was edited by: Radost1972
# 7
> and after doing so, using reflection is the only way
> to instanciate the values to the object...right?
>
> thanks a lot!
>
> G
>
> Message was edited by:
> Radost1972
depends entirely on whether you're working with a class or interface you already know about. if this is all work to be done on your ExampleDTO or whatever, then, no, you needn't use reflection. simple method calls will be enough. if you're planning on using unknown types, then reflection will probably be necessary. but don't use reflection unless there's no other way to do it
# 8
No; I don't know the class in advance, that's my problem. I think I will have to use Reflection then. Thanks a lot.G.
# 9
> No; I don't know the class in advance, that's my
> problem. I think I will have to use Reflection then.
> Thanks a lot.
>
> G.
yep, reflection it is then. how are you going to determine which methods/fields to copy? if you're using java 5 or above, you could consider using [url="http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html"]annotations[/url] for source-level metadata