L
Lemon Tree
Hello,
I am wondering what is the most elegant (and correct) way to organize
the following type hierarchy.
Let's suppose to have the following parametric class:
class Foo<T extends InfoType> {
private T info;
...
public T getInfo() {
...
}
}
Let's now suppose that we want to create a Container for Foo instances,
and that we want to be able to search that container by using the info
contained in every Foo instance.
I might do the following:
class FooCollection<T extends Foo> extends HashSet<T> {
...
public T findByInfo(InfoType info) {
for(T t : this) {
if(t.getInfo().compareTo(info) == 0) {
return t;
}
}
return null;
}
Obviously the InfoType class contains an int compareTo(Bar info) method
for comparing InfoType (and their subclasses)
The previous code is correct but generates some warnings with respect
to the fact that the type parameter T is of type Foo and Foo is a
parametric type which is not instantiated with respect to its type
parameter.
In order to avoid the warnings I might do the following:
class FooCollection<T extends InfoType> extends HashSet<Foo<T>> {
...
public Foo<T> findByInfo(T info) {
...
}
}
This solution eliminates the warnings but I do not like it because when
I declare a FooCollection<InfoType>, by reading the declaration, it is
not clear that I am actually instantiating a collection of
Foo<InfoType> objects but it gives the impression that I am dealing
with a InfoType collection.
The third solution is to make explicit both the InfoType and the Foo
instances contained in the collection:
class FooCollection<T extends InfoType, S extends Foo<T>> extends
HashSet<S> {
...
public S findByInfo(T info) {
...
}
}
This also works, but when I have to declare a FooCollection I need to
do:
FooCollection<MyInfoType, Foo<MyInfoType>> which is a bit redundant and
unclear.
I do not see any other solution to how to structure the hierarchy.
In my opinion the most elegant and simple solution is the first one.
But I would like to get rid of the warnings. Do you know how to do
this? Can you suggest another more elegant solution (if it exists?)
Thank you
I am wondering what is the most elegant (and correct) way to organize
the following type hierarchy.
Let's suppose to have the following parametric class:
class Foo<T extends InfoType> {
private T info;
...
public T getInfo() {
...
}
}
Let's now suppose that we want to create a Container for Foo instances,
and that we want to be able to search that container by using the info
contained in every Foo instance.
I might do the following:
class FooCollection<T extends Foo> extends HashSet<T> {
...
public T findByInfo(InfoType info) {
for(T t : this) {
if(t.getInfo().compareTo(info) == 0) {
return t;
}
}
return null;
}
Obviously the InfoType class contains an int compareTo(Bar info) method
for comparing InfoType (and their subclasses)
The previous code is correct but generates some warnings with respect
to the fact that the type parameter T is of type Foo and Foo is a
parametric type which is not instantiated with respect to its type
parameter.
In order to avoid the warnings I might do the following:
class FooCollection<T extends InfoType> extends HashSet<Foo<T>> {
...
public Foo<T> findByInfo(T info) {
...
}
}
This solution eliminates the warnings but I do not like it because when
I declare a FooCollection<InfoType>, by reading the declaration, it is
not clear that I am actually instantiating a collection of
Foo<InfoType> objects but it gives the impression that I am dealing
with a InfoType collection.
The third solution is to make explicit both the InfoType and the Foo
instances contained in the collection:
class FooCollection<T extends InfoType, S extends Foo<T>> extends
HashSet<S> {
...
public S findByInfo(T info) {
...
}
}
This also works, but when I have to declare a FooCollection I need to
do:
FooCollection<MyInfoType, Foo<MyInfoType>> which is a bit redundant and
unclear.
I do not see any other solution to how to structure the hierarchy.
In my opinion the most elegant and simple solution is the first one.
But I would like to get rid of the warnings. Do you know how to do
this? Can you suggest another more elegant solution (if it exists?)
Thank you