Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Extracting code to a generic method when method names are different

Tags:

java

oop

generics

I have a code that iterates on some objects of type MyType:

// result is of type Map<Long, MyType>
for (final Map.Entry<Long, MyType> someMyTypeObject: result.entrySet() {
   // Do a bunch of stuff

   Status status = someMyTypeObject.getStatus();

   // Do some more stuff
}

No I'm adding a flow that handles result of type Map<Long, MyNewType> that needs to do exactly the same thing while the only difference is that the method that returns the status is named differently (let's say - getObjectStatus() instead of getStatus()). I can't change either method's name.

I wanted to do something like this:

public <T> doIterationWork(Map<Long, T> result) {
   // Do a bunch of stuff

   Status status = someMyTypeObject.getStatus();

   // Do some more stuff
}

The problem is - I can't use this method because I can't tell which type is T. I thought about overloading 3 methods:

public void getStatus(MyType obj) { return obj.getStatus(); }

public void getStatus(MyNewType obj) { return obj.getObjectStatus(); }

public void getStatus(Object obj) { // throw exception }

But I can't do it because the call is set according to the static type and not the dynamic type (thus I will always get to the Object overload).

Is there a good pattern to handle such case without changing the original classes which is not an option right now?

like image 794
Avi Avatar asked Mar 10 '14 13:03

Avi


People also ask

How does a generic method is different from generic type?

From the point of view of reflection, the difference between a generic type and an ordinary type is that a generic type has associated with it a set of type parameters (if it is a generic type definition) or type arguments (if it is a constructed type). A generic method differs from an ordinary method in the same way.

Can generics be used with inheritance in several ways what are they?

Generics also provide type safety (ensuring that an operation is being performed on the right type of data before executing that operation). Hierarchical classifications are allowed by Inheritance. Superclass is a class that is inherited. The subclass is a class that does inherit.

Do generic methods have to be static?

Static and non-static generic methods are allowed, as well as generic class constructors. The syntax for a generic method includes a list of type parameters, inside angle brackets, which appears before the method's return type.

Can you have a generic method in a non generic class?

Generic methods in non-generic classYes, you can define a generic method in a non-generic class in Java.


2 Answers

It's not really elegant, but you could check the type with instanceof, cast the object and call the corresponding method.

Something like this:

Status status;

if (someMyTypeObject instanceof MyType) { // It's a MyType object

    MyType myTypeObject = (MyType) someMyTypeObject;
    status = myTypeObject.getStatus();

} else { // It's a MyNewType object

    MyNewType myNewTypeObject = (MyNewType) someMyTypeObject;
    status = myNewTypeObject.getObjectStatus();

}
like image 135
JMP Avatar answered Oct 17 '22 07:10

JMP


You don't know T inside doIterationWork(), but you should known it somewhere upstream, right? If so, you can define a strategy at that point and pass it to doIterationWork():

public <T> doIterationWork(Map<Long, T> result, StatusExtractor<T> statusExtractor) {
    ...
     Status status = statusExtractor.extractStatus(result.getValue());
    ...    
}
like image 33
axtavt Avatar answered Oct 17 '22 08:10

axtavt