This is an extension of an earlier question I made.
So, using the answer from the previous question I mentioned, I am able to make a generic utility that would convert/parse an Enumeration to a String.
Now the only problem I have with my solution is, I can't seem to refactor the code to my liking. It works the way I want it to, but it is a tad more on the code heavy side than I had hoped for. I was curious if there was a design pattern (or any other way to code it) to help solve this issue. I am thinking there is a creational design pattern that will help in this case. I would be easy if I had lambda expression but, Apex is not quite there yet ;).
The code I use has kind of patterns dug into it already. It uses the Strategy, Factory, and Template(ish) pattern I believe. I have the resources to find something to solve this issue (my favorite Design Pattern books), but nothing I can think of jumps out at me.
Here is the code for my utility for just one enumeration:
private interface EnumerationToStringHelper
{
List<Object> ProvideEnumerationValues();
Boolean CompareEnumerationToString(String possibleEnumeration, Object enumerationValue);
}
private class ExampleEnumToStringHelper implements EnumerationToStringHelperExampleEnum
{
public List<Object> ProvideEnumerationValues(){ return (List<Object>)ExampleEnum.values(); }
public Boolean CompareEnumerationToString(String possibleEnumeration, Object enumerationValue)
{
return ((ExampleEnum)enumerationValue).name().equalsIgnoreCase(possibleEnumeration);
}
}
private static Map<Type, EnumerationToStringHelper> TypeToEnumerationMap;
private static void PopulateTypeToEnumerationMap()
{
TypeToEnumerationMap = new Map<Type, EnumerationToStringHelper>
{
Type.forName('ExampleEnum') => new ExampleEnumToStringHelper()
};
}
public static Object ConvertStringToEnumeration(Type enumerationType, String possibleEnumeration)
{
if(possibleEnumeration == null) return null;
if(TypeToEnumerationMap == null)
PopulateTypeToEnumerationMap();
EnumerationToStringHelper conversionAction = TypeToEnumerationMap.get(enumerationType);
if(conversionAction == null)
throw new EnumerationToStringException('The Type you enter is either a) not an enumeration or b) not a supported enumeration.');
for(Object particalarEnumValue : conversionAction.ProvideEnumerationValues())
if(conversionAction.CompareEnumerationToString(possibleEnumeration, particalarEnumValue))
return particalarEnumValue;
return null;
}
public class EnumerationToStringException extends Exception { }
Now in order to use the utility, it is exactly what I hoped for.
return (ExampleEnum)RT_GeneralUtilities.ConvertStringToEnumeration(Type.forName('ExampleEnum'), 'Testing');
It is a little long, but it beats having the entire for loop for 12 or more different Enum
types. That is why I wanted to build the utility - essentially for better readability and test coverage purposes.
The downside with this is I have to make a helper for every single Enum
type. So it makes it kind of an eyesore maintainability-wise.
I was hoping there was a pattern I could implement to assuage that eyesore and make it appealing on both sides. Any suggestions?