S
Sébastien de Mapias
Hi,
What are the limitations of using a cast with a generic ?
How *beautiful* would it be if I could write a generic 'equals()'
method -for exple-, with the following:
public class StuffImpl extends GenericTypes<StuffImpl>
{
// almost empty class...
}
public abstract class GenericTypes<T>
{
...
private String value;
...
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
// final StuffImpl other = (StuffImpl) obj;
final T other = (T) obj;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
This equals is that generated by RAD. Once I do
final T other = (T) obj;
the jdk complains on the line
if (other.value != null)
saying it "cannot find symbol"...
Too bad isn't it ?
Has someone perhaps found a way to implement an "equals()"
method similar to that above, using a casted generic ?
What operations are possible on 'other' (I mean using Class.forName()
etc.) to try to retrieve what 'other' really is, what instanceOf it
is ??
The possibilities seem to be much lesser than with C++ don't they ?
In advance, many thanks.
Regards,
Sébastien
What are the limitations of using a cast with a generic ?
How *beautiful* would it be if I could write a generic 'equals()'
method -for exple-, with the following:
public class StuffImpl extends GenericTypes<StuffImpl>
{
// almost empty class...
}
public abstract class GenericTypes<T>
{
...
private String value;
...
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
// final StuffImpl other = (StuffImpl) obj;
final T other = (T) obj;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
This equals is that generated by RAD. Once I do
final T other = (T) obj;
the jdk complains on the line
if (other.value != null)
saying it "cannot find symbol"...
Too bad isn't it ?
Has someone perhaps found a way to implement an "equals()"
method similar to that above, using a casted generic ?
What operations are possible on 'other' (I mean using Class.forName()
etc.) to try to retrieve what 'other' really is, what instanceOf it
is ??
The possibilities seem to be much lesser than with C++ don't they ?
In advance, many thanks.
Regards,
Sébastien