Skip to content

Instantly share code, notes, and snippets.

@GLitchfield
Created November 11, 2013 15:52
Show Gist options
  • Save GLitchfield/7415388 to your computer and use it in GitHub Desktop.
Save GLitchfield/7415388 to your computer and use it in GitHub Desktop.
public abstract class Identifier<T extends Identifier<T>>
implements Comparable<T>, Existential {
private final String id;
private final Class<T> clazz;
public Identifier(final String id, final Class<T> clazz) {
if(id == null) {
throw new IllegalArgumentException("ID cannot be null");
}
if(clazz == null) {
throw new IllegalArgumentException("Class cannot be null");
}
this.id = id;
this.clazz = clazz;
}
@Override
public int compareTo(final T o) {
return id.compareTo(o.toString());
}
@Override
public boolean isNull() {
return false;
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public boolean equals(final Object o) {
if(!(clazz.isInstance(o))) {
return false;
}
return id.equals(o.toString());
}
@Override
public String toString() {
return id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment