2

I have the following method:

    public XAP_ENUM_EnumConverter put(Type enumType, XAP_ENUM_EnumWrapper enumWrapper) {
        ENUM_WRAPPERS_BY_TYPE_MAP.put(enumType, enumWrapper);
        return this;
    }

which i can invoke in a constructor like this:

    private ALP_CONX_ColourHelper(XAP_ENUM_EnumConverter converter) {
        this.converter = converter;
        this.converter.put(ALP_CONX_Colour.class, new XAP_ENUM_EnumWrapper(ALP_CONX_Colour.values()));
    }

Where ALP_CONX_Colour is an enum, e.g.

public enum ALP_CONX_Colour {
    BLACK,
    WHITE,
    NONE
}

A helper for a different enum would have a similar constructor.

But this rather redundant, as the name of the enum-class is required to provide both paramenters and this should always be consistent. It would make no sense to have a Type for "Colours" and a List for "Shapes". So, I'd like to refactor this function to one parameter like:

    public XAP_ENUM_EnumConverter put(Type enumType) {
        List<Object> enumList = this.createEnumList(enumType);
        XAP_ENUM_EnumWrapper enumWrapper = new XAP_ENUM_EnumWrapper(enumList);
        ENUM_WRAPPERS_BY_TYPE_MAP.put(enumType, enumWrapper);
        return this;
    }


    private List<Object> createEnumList(Type enumType) {
         // What do I need to do here?
         return ???
    }

Is it possible to do this? If so, how?

2

There is no generic, dynamic approach that will work for all types. Probably the best you can do if you want to avoid maintaining a map is to use a switch statement.

switch on String.valueOf(enumType)
{
    when 'MyCustomEnum'
    {
        return MyCustomEnum.values();
    }
    when 'MyClass.Enum'
    {
        return MyClass.Enum.values
    }
    when 'System.LoggingLevel'
    {
        return LoggingLevel.values();
    }
    when else
    {
        return new List<String>();
        // alternatively consider to be an error case
    }
}

Probably slower than just calling values() concretely; I wouldn consider such an implementation to be of questionable benefit.

7
  • 2
    Switches perform well enough (better than if-else chains), but I agree that this is basically more trouble than its worth. – sfdcfox May 28 '20 at 20:30
  • I'm not trying to avoid a map, but rather avoid redundant information in the method invocations I use to populate the map. Currently, lets say I am only interested in enums which are like 'MyCustomEnum', ignoring the other possibilities. Is there a way I can handle just those dynamically? – Brian Kessler May 28 '20 at 21:01
  • 1
    No, you can only interact with Enum concretely. You have to build your own layer on top if you want any dynamic functionality. – Adrian Larson May 28 '20 at 21:07
  • Building my own layer is what I'm doing, but I was hoping it would be possible to do it in a more intelligent way. Sigh. – Brian Kessler May 30 '20 at 8:11
  • 1
    Only by calling instanceof against a concrete type. That comparison also can't be dynamic. :/ – Adrian Larson Jun 7 '20 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.