3

I have been trying to make a generic utility and one of the things I'd like to do is pass an Enum definition to a possible Type instance.

It would look something like this:

public enum ExampleEnum
{
   Testing,
   OtherTesting
}

public static void FunctionWithEnum(Type enumarationDefinition)
{
   if(enumarationDefinition instanceOf ExampleEnum)
      return ExampleEnum.CustomaryAction();
   //Using the same format, performing the same answer...
}

//It would then be called like 
FunctionWithEnum(ExampleEnum);

I of course started out with some prototyping in the Apex Anonymous windows:

Type enumarationType = ExampleEnum;

if(enumarationType instanceof ExampleEnum)
    System.Debug('You are working with the ExampleEnum enumeration');

The only problem is when I try to assign an Enum to a Type instance/parameter, I get the following message:

Variable does not exist: ExampleEnum

So the question is, how come this particular example does not work?

I'm am hoping for a solution so, I can use a Map and have a key of the enum and an interface definition for the action. It would be something like this:

public enum ExampleEnum
{
   Testing,
   OtherTesting
}

public static Map<Type, ICustomAction> EnumerationToCustomActionMap = new Map<Type, ICustomAction>
{
   ExampleEnum => ActionUsingInterface(),
   //Other mapped enumeration definitions   
};

public <some return type> FunctionWithEnum(Type enumarationDefinition)
{
   if(!EnumerationToCustomActionMap.containsKey(enumarationDefinition))
      thrown CustomException('No custom iaction found for enumeration'); 
   return EnumerationToCustomActionMap.get(enumarationDefinition);
}
  • Not all of your code will compile. It makes your question a little hard to follow. I think just a few minor edits would make this a lot easier to read. – Adrian Larson Feb 21 '16 at 23:23
  • 1
    Sorry about that. I'm just having a bit of trouble to describe the problem with the correct vocabulary. I'll try to correct it with the best of my ability though. Hopefully then it will make sense. – Programmable Medley Feb 21 '16 at 23:33
2

I think what you're trying to get at is how to get the Type of an Enum? You can use Type.forName('MyEnum'). I verified that you cannot get the Type using MyEnum.class, it won't compile.

public interface ICustomAction { Object execute(); }
public class ConcreteAction implements ICustomAction
{
    public Object execute() { return null; }
}

public Enum Season { SPRING, SUMMER, FALL, WINTER }
static Map<Type, ICustomAction> enumToAction = new Map<Type, ICustomAction>
{ // probably should keep this private
    Type.forName('Season') => new ConcreteAction()
};

With the above, you can now define a method as follows:

public static ICustomAction getAction(Type enumarationDefinition)
{
    return enumToAction.get(enumerationDefinition);
}

Perhaps even more useful:

public static Object executeAction(Type enumarationDefinition)
{
    ICustomAction action = enumToAction.get(enumerationDefinition);
    return (action == null) ? null : action.execute();
}

Hope that was on track. Let me know if I need to change direction a bit.

| improve this answer | |

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.