8e21dd65dca319ca81a0cd80393d4c13

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?

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 !

D41d8cd98f00b204e9800998ecf8427e

Ryan

July 4, 2010, July 04, 2010 08:09, permalink

No rating. Login to rate!

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.

Your refactoring





Format Copy from initial code

or Cancel