aspose file tools*
The moose likes Java in General and the fly likes how to unite 2 similar methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to unite 2 similar methods? " Watch "how to unite 2 similar methods? " New topic
Author

how to unite 2 similar methods?

Joe Shannow
Ranch Hand

Joined: Dec 01, 2010
Posts: 35
Hi, I'm newbie in Java.
I have some methods:



and the next two methods, which are completely the same, but instead of User there is Scrap object called (List<Scrap> dataList(); Scrap dataItem).

Is there some way to merge them and save the code and count of the methods???

Thank you
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

Do class User and class Scrap have anything in common - for example, do they implement a common interface or superclass? If so, then you could write these to methods to work with the common interface or superclass, and then you could use those methods for anything that implements or extends that.

Suppose that User and Scrap both implement an interface DataItem. (I don't know, because I don't know what the rest of your code looks like - I'm just imagining it). Then you could write the methods to work on a DataItem:

You can then call those methods with a User, a Scrap or anything else that implements DataItem.

Another note: In line 13, you are catching Exception and not doing anything with the exception. That is bad practice, because when something goes wrong, the error will just be ignored and you'll not even notice that something went wrong. At least print out an error message when you catch an exception, but it would be better if you implemented more elaborate error handling.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Joe Shannow
Ranch Hand

Joined: Dec 01, 2010
Posts: 35
Thanks for your help.

And what in the case, that empty class User extends UserBean and class Scrap extends ScrapBean (data beans wuth getters and setters for work with the datatables of the same names)??

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

As long as the two classes have some common interface or superclass somewhere in their class hierarchies, you might be able to write methods that work on that common interface or superclass. So if you have User extends UserBean, Scrap extends ScrapBean, then do the superclasses UserBean and ScrapBean have a common interface or superclass?

If they don't, then there's no good and simple way of unifying the methods - you'd have to write methods for the separate types.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1824
    
    7

The following article might be an interesting read: http://community.jboss.org/wiki/GenericDataAccessObjects
It describes how to create generic data access objects, which seems to match the context of the methods you described.
The article uses Hibernate though, but that's really just an implementation detail.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Too difficult a question for "beginning". Moving thread.
Joe Shannow
Ranch Hand

Joined: Dec 01, 2010
Posts: 35
Thanks guys, I'll try to catch it!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

I think generics would work here as well. Forgive me if the below code doesn't work, no compiler. But I think it will...



Then you can call it like:


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Actually, I take that back. If generics work or not depends on the method signature for session.save(...). If I were a session.save(...) method, I might look like:

In which case I think I have to modify the code I produced earlier to this:


Or some close variation. It would work if the save method used some common interface or parent class between the two. But if the session.save() method is overloaded for each data type, I think you are out of luck:

Joe Shannow
Ranch Hand

Joined: Dec 01, 2010
Posts: 35
Thank you steve, I've not completely tested it yet, but the first example (without serializable) seems to be perfect !!!
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Above code should work for all the objects; but object that passed must be a valid and mapped javabean.
I think it's better to move the thread to Hibernate related forum.


A.A.Anbarasu
Joe Shannow
Ranch Hand

Joined: Dec 01, 2010
Posts: 35
The variant with <E> above works better. Your method with Object class reports something like: Unknown entity: java.util.RandomAccessSubList
But, I'm at the start of learning java, so I'm doing something wrong probably (javabeans are valid and mapped).
In any case, thanks
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Oops... I forgot to ask you one thing.
Is the code you have posted are trying to store the objects into database using hibernate framework.
If NO. Please kindly ignore my previous post.
If yes, then the code that I have posted would work like common/master save method. It will accept any object and will try to save it in the database using hibernate framework. It stores/saves the object/javabean if it is already mapped.

So anywhere in the application if you want to save a javabean; you just need to call/use this method regardless of which bean you are passing.
Joe Shannow
Ranch Hand

Joined: Dec 01, 2010
Posts: 35
Hmmm.. it really storing the objects into database using hibernate..... but it doesn't works for me, but I hadn't test it yet properly.
Last error I've got was: Unknown entity: java.util.RandomAccessSubList
Joe Shannow
Ranch Hand

Joined: Dec 01, 2010
Posts: 35
Aha, your code is alright and the method for saving one object works properly.
But there is some problem in the method for saving whole list, not the one object:



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Generics are not polymorphic. By having List<Object> you cannot pass a List<String> to it. It's a good habit to use "? extends X" or "? super X" unless you need to both retrieve items and store items.

In this case, I'd use a List<? extends Object>, or its short form List<?>.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Joe Shannow wrote:Hmmm.. it really storing the objects into database using hibernate.....
I thought I posted at wrong place. Thanks for confirming
IMHO It's good to not use generics here to make the program much flexible/simple; since it's all about compile time prevention.
If you need to save bulk/list of objects thenAbove code does not have full code; it has only the required information's.

In my both the posts there is an important point. I am not using typed collections anywhere in order the code to work for any objects.
Joe Shannow
Ranch Hand

Joined: Dec 01, 2010
Posts: 35
Thanks Rob, your method is good form me too. But I don't really understand a principle of it neither the method with <E> above. But it works and it is important for this moment

And Anbarasu could you be more specific?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to unite 2 similar methods?