Say I have a generic class Foo
which can hold an object of type T
. Furthermore, let's say I only want to be able to instantiate the class with objects that are one of two types. Finally, let's say that the lowest common upper bound of these two types is a type that has many more subclasses than those two types that I want to allow, so I can't simply specify an upper bound for the type parameter (as in class Foo<T extends Something>
), because then I would allow to instantiate the class with other types than the two I expect.
For illustration, let's say I want Foo
to hold only either a String
or an Integer
. The lowest common upper bound is Object
, so specifying an upper bound won't do the trick.
Certainly, I could do something along the lines of
class Foo<T> {
private T obj;
public Foo(T obj) throws IllegalArgumentException {
if (!(obj instanceof String || obj instanceof Integer)) {
throw new IllegalArgumentException("...");
}
this.obj = obj;
}
}
However, in this case, I can still call the constructor with any object; if I try to instantiate it with something that is neither a String
nor an Integer
, I will get an exception at runtime.
I would like to do better. I would like the compiler to infer statically (i.e., at compile time) that I can only instantiate this class with objects that are either String
or Integer
.
I was thinking something along those lines might do the trick:
class Foo<T> {
private T obj;
public Foo(String s) {
this((T) s);
}
public Foo(Integer i) {
this((T) i);
}
private Foo(T obj) {
this.obj = obj;
}
}
This works, but it looks really, really odd. The compiler warns (understandably) about unchecked casts. Of course I could suppress those warnings, but I feel this is not the way to go. In addition, it looks like the compiler can't actually infer the type T
. I was surprised to find that, with the latter definition of class Foo
, I could do this, for instance:
Foo<Character> foo = new Foo<>("hello");
Of course, the type parameter should be String
here, not Character
. But the compiler lets me get away with the above assignment.
- Is there a way to achieve what I want, and if yes, how?
- Side question: why does the compiler let me get away with the assignment to an object of type
Foo<Character>
above without even so much as a warning (when using the latter definition of classFoo
)? :)
note: If you can't directly modify
A
and/orB
, create wrapper classesWA
andWB
for them beforehand.example:
private Foo(T obj)
due to diamond type inference. As such, it's equal to callingFoo<Character> foo = new Foo<Character>("hello");
Try using
static
factory method to prevent compiler warning.Now you create instance using:
However the following are still valid since you can declare a Foo of any type T, but not instantiate it:
Long story short: You are trying to create a union of two classes in java generics which is not possible but there are some workarounds. See this post
Well the compiler uses the Character class in T parameter. Then the String constructor is used where String is casted to T (Character in this case). Trying to use the private field obj as a Character will most likely result in an error as the saved value is an instance of the final class String.
Generics is not suitable here.
Generics are used when any class can be used as the type. If you only allow
Integer
andString
, you should not use generics. Create two classesFooInteger
andFooString
instead.The implementations should be pretty different anyway. Since
Integer
s andString
s are very different things and you would probably handle them differently. "But I am handling them the same way!" you said. Well then what's wrong withFoo<Double>
orFoo<Bar>
. If you can handleInteger
andString
with the same implementation, you probably can handleBar
andDouble
and anything else the same way as well.Regarding your second question, the compiler will see that you want to create a
Foo<Character>
, so it tries to find a suitable overload. And it finds theFoo(T)
overload to call, so the statement is perfectly fine as far as the compiler is concerned.