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?
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.
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.
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.
Generic methods in non-generic classYes, you can define a generic method in a non-generic class in Java.
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();
}
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());
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With