Assume we have the next enum and I want to add equals(String) method to it, because other people that working with the same code usually make a mistake comparing enum with string using equals method.
public enum SomeEnum {
CONSTANT1("DATABASE_CONSTANT1"),
CONSTANT2("DATABASE_CONSTANT2");
private final String databaseConstant;
SomeEnum(String databaseConstant) {
this.databaseConstant = databaseConstant;
}
public String getDatabaseConstant() {
return databaseConstant;
}
public boolean equals(String databaseConstant) {
return getDatabaseConstant().equals(databaseConstant);
}
}
Question: are there any pitfalls in using the approach like this?
"DATABASE_CONSTANT1".equals(SomeEnum.CONSTANT1)or if the original equals method of the enum is called.IllegalArgumentExceptionfrom that method to avoid running into more problems later.