0

I have this class constructor AppData(Map params, Operations operation)
where Operations is

public enum Operations 
{
create,update,delete,view,compare
}

How can I instantiate AppData with parameters constructor?

for the map parameter its fine i get that from servlet, (req.getParameterMap())
but the main problem for me is knowing the operation type, which also comes from the map parameter, So in order to do this AppData data=new AppData(req.getParameterMap(),op); what shall I assign to op or what shall an op type be?

Note: I can get the String create , update ... from req.getParameterMap() by using iterator and Map.Entry object.getKey() and Value

AabinGunz
  • 12,109
  • 54
  • 146
  • 218
  • duplicate of http://stackoverflow.com/questions/604424/java-enum-converting-string-to-enum ? – chburd May 18 '11 at 12:54
  • 1
    enums should be given names with capital letters: `CREATE` instead of `create` and so on. I suggest a refactoring to implement the Java naming conventions (increases readability). – Andreas Dolk May 18 '11 at 13:01

1 Answers1

3

Assuming you're starting with a string (e.g., "update"), you can use Enum.valueOf. Your Operations enum will inherit a form of that, so:

Operations op = Operations.valueOf(theString);

...or use the one from Enum, though you'd really only have to do this if you don't have direct, compile-time access to Operations. I think you do have compile-time access, so use the above. But for completeness:

Object op = Enum.valueOf(Class.forName("Operations"), theString);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • is it, Enum.valueOf("update") ? Also, I need to store "update" in some var first. – AabinGunz May 18 '11 at 12:52
  • 1
    @Abhishek: I've shown both the usual forms. If you have compile-time access to `Operations`, use `Operations.valueOf(theString);` If not, use `Enum.valueOf` and pass in the `Class` object of the enum and the string. – T.J. Crowder May 18 '11 at 12:57