5 Replies Latest reply: Jan 2, 2005 6:34 PM by 807596 RSS

    Maps, objects

    807596
      I'm a complete newbie to Java but have coded in C++ and VB for years. I have to code against a 3rd party's api. They use maps to transfer data:

      Map input = new HashMap();
      input.put("Action","CreateApplication");

      I then call a function that passes that map through a bunch of stuff and eventually another piece of Java gets it back.

      On the other side of things, I need to pull out the action and do something based on it:

      action = (String)input.get("Action");
      output = new HashMap();
      if (action.equals("CreateApplication")){
      output = createApplication(input,out);
      }
      else if(action.equals("CreateUser")){
      }
      //etc. for all functionality I need

      else{
      throw new UnknownAuthenticationTypeException("Unknown action type:" + action);
      }

      This code works perfectly fine, but YUCK! I'm dependent on getting those two strings to be exactly the same.

      An enum would work nicely with a switch, but I don't quite know how to do that. And it seems that the map needs objects so I can't just define integers to represent the strings.

      There's got to be a better way to do this!

      TIA,
      Candi
        • 1. Re: Maps, objects
          807596
          That's a pretty sucky API there.

          But what's your question? I mean, it sounds like you're bound by the API, so you don't have a choice. What are you trying to do?
          • 2. Re: Maps, objects
            794117
            My approach would be to define constants to be used for the valid actions in the map, and only add/remove those constant values.
            public final static String CREATE_APPLICATION = "CreateApplication";
            input.put("Action", CREATE_APPLICATION);

            That will help a little with having to match up strings.
            I would do the same with the key as well ie define constant ACTION="Action"
            and have input.put(ACTION, CREATE_APPLICATION);

            You don't HAVE to put a String object into the hashmap.
            You can always use Integer objects (java.lang.Integer) in maps, so you CAN pass in a number if you so wish .
            int actionValue = ((Integer)input.get("Action")).intValue();
            That will give you something you can switch on.

            You can even code your own custom object to store in there.
            eg
            public class MyAction{
              // action constants
              public final static int CREATE_APPLICATION = 1;
              public final static int DELETE_APPLICATION = 2;
              ...
              private actionId;
              public getActionId(){
                return actionId;
              }
            }
            
            action = (MyAction)input.get("Action");
            switch (action.getActionId()){
              case MyAction.CREATE_APPLICATION:
                output = createApplication(input, out);
                break;
                case MyAction.DELETE_APPLICATION:
                ...
            }
            Hope this helps some,
            evnafets
            • 3. Re: Maps, objects
              807596
              Consider using polymorphism.
              public interface Action {
                  public Object execute();
              }
              
              public class CreateApplication implements Action {
                  public Object execute() {return blah;}
              }
              
              public class CreateUser implements Action {
                  public Object execute() {return blah;}
              }
              Then, in the calling method:
                      Action action = (Action) input.get("Action");
                      Object o = action.execute();
              • 4. Re: Maps, objects
                807596
                This code works perfectly fine, but YUCK! I'm dependent on getting those two strings to be exactly the same.
                This is the same as C++ and most other langauges. You have to lookup what you put in.

                If you want to avoid repeating you code use a constant just as you would in C++
                public static final String ACTION = "Action"
                You shouldn't try good programming practice out the window just because you have a different language.
                • 5. Re: Maps, objects
                  807596
                  Thanks. This is exactly what I was looking for.