6

I need to find if given String is not in the list of ENUMs.

These Strings come back with spaces, i.e.: "CHILD CARE", "CREDIT CARDS", etc...

Any other ExpenseType should be mapped to OTHER, except HOA. HOA should be completely ignored.

My ENUMs are as follows:

public enum ExpenseType {
    AUTOLOAN("AUTO LOAN"),
    ALIMONY("ALIMONY"),
    CHILDCARE("CHILD CARE"),
    CREDITCARDS("CREDIT CARDS"),
    INSTALLMENTLOANS("INSTALLMENT LOANS"),
    FOOD("FOOD"),
    UTILITIES("UTILITIES"),
    TRANSPORTATION("TRANSPORTATION"),
    OTHER("OTHER");

    private String expenseType; 
    ExpenseType(String expenseType) {
        this.expenseType = expenseType;
    }   
    @Override public String toString() {
        return this.expenseType;
    }
}

The way I am doing this now is as follows:

String expenseDescription = expense.getExpenseDesc().replaceAll(" ", "");
if(EnumUtils.isValidEnum(ExpenseType.class, expenseDescription)) {
    monthlyExpenses.setType(ExpenseType.valueOf(expenseDescription).toString());
} 
else if(!expenseDescription.equals("HOA")) {
   monthlyExpenses.setType(ExpenseType.OTHER.toString());
}

Does anyone know a better way to do this?

nomadSK25
  • 2,350
  • 3
  • 25
  • 36
Angelina
  • 2,175
  • 10
  • 42
  • 82

2 Answers2

4

Why not use getEnum to get Enum if applicable (check for null or use Optional if needed)

ExpenseType monthlyExpenses = EnumUtils.getEnum(ExpenseType.class, expenseDescription);

Gets the enum for the class, returning null if not found.

This method differs from Enum.valueOf(java.lang.Class, java.lang.String) in that it does not throw an exception for an invalid enum name.

Also prefer adding to enum a code (String) as a reference, which won't contains spaces and special characters, e.g.

//...
CHILDCARE("CHILD_CARE","CHILD CARE"),
//...
private String expenseType; 
private String expenseTypeCode; 
ExpenseType(String expenseType, String expenseTypeCode) {
    this.expenseType = expenseType;
    this.expenseTypeCode = expenseTypeCode;
}   
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
1

Here is another alternative.

      Map<String, String> codes = Arrays.stream(ExpenseType.values()).collect(
            Collectors.toMap(ExpenseType::toString, ExpenseType::name));
      codes.put("HOA","TBD");

      String[] submittedCodes = { "CREDIT CARDS", "FOOD", "UTILITIES", "UNKNOWN"
      };
      for (String c : submittedCodes) {
         String expenseType = codes.getOrDefault(c,"OTHER");
         System.out.println(expenseType);
      }

First, I didn't see a reason to remove the spaces from the submitted code unless you are concerned about addition of extra spaces creeping in. In that case you should probably also remove tabs.

Since you are already using the enum value to compare to the name, I figured I would just ignore the name as they are effectively the same.

  • The map is used only to allow a null to be returned in case of a missing key.
  • The enum was only used to conveniently populate the map.

I did not know exactly how to handle HOA. But all of this is just another alternative for you to possibly investigate as you can rearrange the keys and values, etc to suit your requirements.

WJS
  • 36,363
  • 4
  • 24
  • 39