8

I have such enum class in java

    public enum MockTypes
    {
        // Atlantis mocks
        ATLANTIS_VERIFY("ATLANTIS", "verify"),
        ATLANTIS_CREATE_RECORD("ATLANTIS", "createRecord"),

    ...

        private String m_adaptor;

        private String m_step;

private MockTypes( String adaptor, String step)
    {
        m_adaptor = adaptor;
        m_step = step;
    }

             public String getAdaptor()
        {
            return m_adaptor;
        }

        public String getStep()
        {
            return m_step;
        }

I have to implement method that returns enum value by adaptor and step parameter.

public MockTypes getMockTypeByName(String adaptor, String step)

but I have no idea how. Could someone help me?

3 Answers 3

14
public MockTypes getMockTypeByName(String adaptor, String step)
{
    for(MockTypes m : MockTypes.values())
    {
        if(m.getAdaptor().equals(adaptor) && 
           m.getStep().equals(step)) return m;
    }
    return null;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Why equalsIgnoreCase? (It really, really shouldn't be your default.)
@LouisWasserman The OP can use equals() instead if he wants, it's up to him.
Yes, but...for you to suggest it is weird. equals is the default, no? And nothing the OP's mentioned implies that casing is a problem?
@LouisWasserman I edited my answer as you suggested, and you are right the OP didn't mention anything about case-sensitive :)
Fun fact, by the way: Calling values() for an enum type rebuilds the whole array every time, but iterating over EnumSet.allOf(MockTypes.class) only does O(1) work.
|
2

If you want a "constant-time" solution that doesn't involve looking up values, your best option is to initialize a constant Map in a static block in the MockType class.

If you're up for using Guava, it'll actually be relatively pleasant:

public enum MockType {
  ...

  private static final ImmutableTable<String, String, MockType> LOOKUP_TABLE;

  static {
    ImmutableTable.Builder<String, String, MockType> builder =
      ImmutableTable.builder();
    for (MockType mockType : MockType.values()) {
      builder.put(mockType.getAdaptor(), mockType.getStep(), mockType);
    }
    LOOKUP_TABLE = builder.build();
  }

  public static MockType getMockType(String adaptor, String step) {
    return LOOKUP_TABLE.get(adaptor, step);
  }
}

(Disclosure: I contribute to Guava.)

The alternative is going to be relatively similar -- construct a Map<String, Map<String, LookupType>> in a static block, and do lookups from there -- though it's going to require somewhat more work.

Comments

1

You can use enum's values() method to obtain a list of all the defined values. You can then loop through this list and find the values you're interested in that match the ones sent as parameters to the method.

2 Comments

I need only one value that matches two parameters
@ConstantineGladky, you can loop through what values() returns to find the value you're looking for. values() will return a list of all possible MockTypes. You could also make a Map, caching these values, if the loop performance is a concern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.