I have several enumerations with the name and byName , which is approximately the same for all of them:
public static Condition byName(String name) throws NotFoundException { for (Condition c : values()) { if (c.name.equals(name)) { return c; } } throw new NotFoundException("Condition with name [" + name + "] not found"); }
Since the byName method byName duplicated in different enumerations, I would like to include it in one place and avoid code duplication.
But:
- Enumerations cannot extend an abstract class
- Java8 interfaces with default methods cannot access
values() method
I know that this could probably be done with AspectJ, but I would prefer not to introduce compilation in time for something simple, and this is Spring AOP (which I have at hand, since this is a Spring project). only allows you to bind to existing methods and not add new ones.
Any other viable solution to add a generic enumeration method?
source share