aspose file tools*
The moose likes Java in General and the fly likes A generic headache Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "A generic headache" Watch "A generic headache" New topic
Author

A generic headache

Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Hi all,
I am trying to bridge two, object models, one is our company DB, the other is from a 3rd Party CRM system.

Some interesting choices where made, and I have ended up with three interface that extend from a common interface, and
one object that implement the three interfaces!!!

So I am trying to "sync" two objects, ourCustomer with crmCustomer, and I am trying to use generics to do this, its work fine, all but for this last bit.
I chose this way as 90% of the code is "generic" between the three Classes, only the sync is really different and proving a headache.

I am hoping you guys can tell me if I am doing this right:



RecordType is the type of the crmRecords, I am using this to provide some form of type safety.
What I am trying to say, is that this method will take as a param an Object that extends from OurCrm and will return an Object of is of that type.

Not sure I explained that well.
Thanks
G

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2861
    
  11

I've been trying to handle many of the generics questions lately, partly to make sure I really understand generics, and partly because no one else seems to be. However, I'm stumped by this question. I'm not clear what you're trying to do and, also, I can't think of a case when having generics in a private method makes sense.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
hmmmm an interesting thought. I hadnt really given it much thought before.

I think, partly what I am trying to do is to make something as type safe as possible, from a very bad model.

I think I was seeing things from the API, it should take a generic object, search for it, then synchronise the found object with the existing object (thus syncing to different systems). I was trying to restrict the types of object that could be considered legal.
The method was private as I didnt see any need for it to be an API method.

Having thought about it, and having right a two solution, I think I was trying to use generics as a replacement for overloading/polymorphism due to the fundamentally flawed (in my opinion) models I am having to work with.
The model that we own, has three interfaces, which all inheritate from a base interface, then one class that implements those three interfaces.
Each interface represents an entity in a database.
The other model has no way of creating a search object from one of its records, or no way of linking the two.

I think the implementation I am happiest with, has one delegate for each of the database entities I am trying to sync.

Perhaps I was using generics as a golden hammer. I will look at my implementation again.
Thanks for your reply.

G
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Your syntax is a bit mixed up. Inside the method parameter types the only wildcard allowed are "?", "? super X" and "? extends X", and only as part of the generic type of an argument (e.g. Class<? extends T>). The binding of the generic type must be done in the generic type declaration:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Do you know what, I have read the generics chapter of Effective Java at least twice. Yet I dont think it ever puts it like that, or given as an example, and yet that simple statement made more sense to me, thanks Rob.

Ok, I think I tried that and it didnt work, I was getting lots of red squiggles, perhaps more to do with the model. I will have a look at it again though as I would rather the method, private or otherwise is as robust as possible.

Seems I am not as good at generics as I would like to think I am ;)
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
the other reason I was trying to use generics, I wanted to minimize the amount of casting in the client code, which I would have been able to do if I set the parameter to the OurCrm super interface.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Thanks Rob, that really helped, I am now happier with my generic method, which I hope is robust enough and will reduce the maintenance of the code.

I ended up with this:


So I am passing in the two objects that need to be used in the synchronisation, and return the synchronised object.
The returned object is of the same type as the passed in object.

I am having to cast record to the type I need such as Customer, but I am happy with this, as I would have
to perform some sort of cast if I had made record generic.

G
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A generic headache