First, my use case: I have a list of Transaction Types that belong to two separate categories (let's say A and B). We ingest a file with a column designating the transaction type, but one file can contain transactions that belong to both "A" and "B". In all my previous logic, I could treat these transaction types exactly the same so I had ONE Enum for TransactionType, but now in a few instances I need to know whether they belong to group "A" or "B".
After researching I decided to try to implement an interface, BaseTransactionTypeEnum, and two enums ATransactionTypeEnum and BTransactionTypeEnum that implement this interface.
public interface BaseTransactionTypeEnum {
BaseTransactionTypeEnum fromString(String text);
String getText();
String getCategory();
Then ATransactionTypeEnum and BTransactionTypeEnum look like:
public enum ATransactionTypeEnum implements BaseTransactionTypeEnum{
//**Enum values.....//
private String text;
public String getCategory(){
return "ATransactionType";
}
ATransactionTypeEnum(String text) {
this.text = text;
}
@JsonValue
public String getText() {
return this.text;
}
@JsonCreator
public ATransactionTypeEnum fromString(String text) {
for (ATransactionTypeEnum t : ATransactionTypeEnum.values()) {
if (t.text.equalsIgnoreCase(text)) {
return t;
}
}
return UNKNOWN;
}
But I'm running into issues where I previously used valueOf to set the TransactionType to transactions read from the file. Is there a way to use valueOf() when you use an enum implementing an interface? I tried utilizing the fromString method but getting errors about it being a non-static method.
I'm starting to wonder if I'm just totally off-base here, perhaps there is a better way to designate the Category of each Enum value that would be easy to check later in my code?