Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way to compare string param to enum list to perform correct action

 
Karina Guenther
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got an enum list of values:
public static enum ValidRptSizeValues {
INVOICE("invoice"),
NATIVE_ALLOC("nativeAll"),
CONVERTED("converted"),
CONVERT_ALLOC("convertAll"),
CONVERT_USED("convertUsed");

String reportCalcSource;
ValidRptSizeValues(String rptCalcSource){
this.reportCalcSource = rptCalcSource;
}

}

I've got an inbound parameter that I want to compare with these valid values and perform a specific action based on what it is. I've not found a good example of how to do this and I'm beginining to wonder if ENUM is the right thing to use. What do you suggest?
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you mean you want to, for example, find the INVOICE enum given the string "invoice". How about mimicking the static Enum.valueOf() method where you either loop through your enum values until you find the correct one to return or create a (static) Hashmap in your enum so that you don't have to loop in your valueOf() method. That is, the string is the key of the Hashmap and the value is the enum. Given the size of the enum, I would do the looping because it is easier to implement and support.
 
Karina Guenther
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was hoping for more like a case statement pattern. What I currently have is if (reportSizeValuePicked.equals(ValidRptSizeValues.NATIVE_ALLOC.reportCalcSource)).
I don't see how looping thru works as I need to perform a different action when I find the matching value.
 
Karina Guenther
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved my own problem - here are my code snippets in case anyones interested:
/**
*
* @author karina
* This controls the types of values that can be displayed on the reports.
* INVOICE is used to control the rules of the invoice value.
* The constructor string value matches the parameter value obtained from the screen.
*/
public enum ValidRptSizeValues {
INVOICE("invoice"){BigDecimal reportValue(IntLun lun) { return value1; } },
NATIVE("native"){BigDecimal reportValue(IntLun lun) {return value2;}},
.....

String reportCalcSource;

/**
* Constructor that sets the ENUM's value that matches the radio button value on the UI.
* @param rptCalcSource
*/
ValidRptSizeValues(String rptCalcSource){
this.reportCalcSource = rptCalcSource;
}
/**
* Overridden method for each ENUM value.
* Used to return the appropriate lun size value from a IntLun object.
* @param lun
* @return
*/
abstract BigDecimal reportValue(IntLun lun);

/**
* Based upon the input value returns the ENUM, defaulting to INVOICE.
* @param sizePicked
* @return
*/
ValidRptSizeValues setSizeUsed(String sizePicked){
for (ValidRptSizeValues sizeValue: ValidRptSizeValues.values()){
if (sizePicked.equals(ValidRptSizeValues.valueOf(reportCalcSource))) {
return sizeValue;
}

}
return INVOICE;
}
/**
* return the string value for the report calculating source
* @return
*/
public String getReportCalcSource() {
return reportCalcSource;
}

/**
* Based upon the input value returns the ENUM, defaulting to INVOICE.
* @param sizePicked
* @return
*/
ValidRptSizeValues setSizeUsed(String sizePicked){
for (ValidRptSizeValues sizeValue: ValidRptSizeValues.values()){
if (sizePicked.equals(ValidRptSizeValues.valueOf(reportCalcSource))) {
return sizeValue;
}

}
return INVOICE;
}


later on it's implemented like this:
String myParamValue = "invoice";
ValidRptSizeValues sizeValue;
sizeValue = this.setSizeUsed(myParamValue );
switch (sizeValue) {
case INVOICE:
System.out.println("INVOICE COLUMN HEADER");

break;
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags and proper indentation when posting code. This is almost unreadable.

I find your code weird. From where do you get the value1 and value2? Which are horrible variable names by the way. setSizeUsed doesn't set anything and does almost the same as ValidRptSizeValues.valueOf(). ValidRptSizeValue is an abbreviation which is generally not recommended. And these kinds of problems can usually be solved by polymorphism.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic