public class Pseudonym<T extends Pseudo> {
private T alias;
private T original;
public Pseudonym(T alias, T original) {
if (!alias.getClass().equals(original.getClass()))
throw new IllegalArgumentException("alias and original must be of same type.");
this.alias = alias;
this.original = original;
}
public T getAlias() {
return alias;
}
public T getOriginal() {
return original;
}
}
interface Pseudo{
//marker interface
}
Refactorings
No refactoring yet !
Ryan
July 4, 2010, July 04, 2010 08:09, permalink
I don't see how the check is necessary at all. When you instantiate Pseudonym, you have to specify what T is explicitly, and both alias and original have to be of type T. Unless you foresee a situation where there's a subclass X of Pseudo which itself has subclasses Y and Z, and someone (who?) might try to create a Pseudonym<X> where alias is Y and original is Z, it doesn't seem like there's any problem.
Hi, I just want to disallow two different implemantations of Pseudo using Pseudonym. Any idea how to do this in a better way than right now?