aspose file tools*
The moose likes Java in General and the fly likes Best way to compare string param to enum list to perform correct action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Best way to compare string param to enum list to perform correct action" Watch "Best way to compare string param to enum list to perform correct action" New topic
Author

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

Karina Guenther
Ranch Hand

Joined: Sep 16, 2005
Posts: 55
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

Joined: Jun 01, 2010
Posts: 618
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

Joined: Sep 16, 2005
Posts: 55
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

Joined: Sep 16, 2005
Posts: 55
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

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Best way to compare string param to enum list to perform correct action