This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

A generic headache

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Sheriff
Posts: 3036
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22579
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic