[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

Java generics: how the return the same type defined as a wildcard on a method's input ?

Hi experts,

I would like to have the following work with generics.
Let's say I have a service facade class. This class has methods for use cases. Each method will open a session (database session), execute a series of DAOs under the same session and at the end, close it. For that purpose, I have a SessionManager class with one method: executeWorkUnit(). This method will contain logic for opening and closing the shared session, and delegate the actual logic code to the responsible unit. This unit will be an instance of ISessionWorkUnit. This interface defines one method called performWork(). This method is to be extended by implementing classes and will optionally return an Object.. the result of that work (could be a domain object retrieved from the database).

So my question is how to do this with generics. I want that the return type of the ISessionWorkUnit's executeWorkUnit be defined with generics. For this I have the code:

public interface ISessionWorkUnit<T> {
      public T performWork();
}

public class SessionManager {
      public Object executeWorkUnit(ISessionWorkUnit<?> workUnit) {
            return workUnit.performWork();
      }      
}

returnObject =
  (IMyObject) sessionManager.executeWorkUnit(new ISessionWorkUnit<IMyObject>(){
     public IMyObject performWork() {
        IMyObjectDAO myObjectDao= new IMyObjectDaoImpl();
                        
        return myObjectDao.createMyObject();
    }
  });

As you can see in the code above, I still have to cast the object to the right type on the executeWorkUnit method. This is because I'm forced to define its return type as Object. How can I defined that it will return the exact same wildcard type that is defined on the method's input parameter ?
0
MindWalker
Asked:
MindWalker
  • 2
1 Solution
 
Bart CremersJava ArchitectCommented:
Change the SessionManager to:

public class SessionManager {
      public <T> T executeWorkUnit(ISessionWorkUnit<T> workUnit) {
            return workUnit.performWork();
      }
}

0
 
MindWalkerAuthor Commented:
That worked sweet as a charm! Damn... I really need to read some extensive info about generics. What does that notation mean exactly ?

So you're accepting as an argument a parametrized object of type T, and you're returning an instance T of that parametrized object T ?
0
 
Bart CremersJava ArchitectCommented:
It means you can pass a typed ISessionWorkUnit and the compiler will check that the return type of the method is of the specified type.

Don't know if whay I'm saying makes any sense though.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now