Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return enum value by 2 parameters in java

Tags:

java

enums

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?

like image 493
Constantine Gladky Avatar asked Oct 10 '12 23:10

Constantine Gladky


People also ask

Can enum have 2 values?

The Enum constructor can accept multiple values.

Can an enum have two values Java?

we should do the following steps to have an enum with different values: Create enum constructor which accepts multiple values. Assign each constructor argument to a member field in the enum definition. Create getter methods so we can access any of the values assigned to a particular enum constant.

Can you use == for enums?

Because there is only one instance of each enum constant, it is permissible to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant.

How do you return the value of an enum?

values() method can be used to return all values present inside the enum. Order is important in enums.By using the ordinal() method, each enum constant index can be found, just like an array index. valueOf() method returns the enum constant of the specified string value if exists.


2 Answers

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;
}
like image 154
Eng.Fouad Avatar answered Oct 22 '22 06:10

Eng.Fouad


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.

like image 31
Louis Wasserman Avatar answered Oct 22 '22 05:10

Louis Wasserman