Reputation: 109
I have a very generic heavy class where i am testing differents approach to a problem and this class is used in a lot different places and everytime i change the number of type parameters i have to fix errors everywhere i use the type even where i dont use that class genericity
public class HeavyGen<TYPE1, TYPE2, TYPE3, ETC>{...}
//now anywhere else i have to do either this and refactor my code at each
//change in the number of type parameters
public HeavyGen<?,?,?,?> func(){...}
//or this but @SupressWarning seem like a dirty fix
@SupressWarning
public HeavyGen func(){...}
So: is there a way to be able to change the number of type parameter of HeavyGen
while not having to refactor code in places that dont use it genericity?
Ideas I had:
using a class type that would """encapsulate""" the type parameters, like TypeBag<TYPE1,TYPE2,TYPE3,ETC>
ans then use Container<BAG extends TypeBag>
, but it doesn't seem to work
since those type parameters are mostly used inside the class, do some trick with methods type parameters to pass them around. I haven't managed to make it work so far either.
having a nested class inheritance system (like <CHILD extends Container> CHILD.Inner
) but Java doesn't seem to like that either (the more i think of it the more i would love to be able to do this :D )
Upvotes: 3
Views: 794
Reputation: 2492
You can simply rename your Container<...>
to AbstractContainer<...>
and extract a non-generic Container
interface from it:
public interface Container {
// put non-generic methods here
}
public abstract class AbstractContainer<...> implements Container {
// put generic methods here
}
Now client code can use Container
without carrying about generics, and implementations can extend AbstractContainer<...>
and still be able to use/implement generic methods.
Upvotes: 2