I have a class that is intended to be used for a future internal authentication library ( I know there are already such existing libs ). So to make things as simple as possible to the developers using it in many coming future projects to make use of this library I had in mind them defining an enum with roles, simple example, the role sysadmin:
// Not a project specific class, but Auth is intended to be part of the library
class Auth {
public static enum AUTH_ROLE {
sysadmin( new Rule(AdminController.class, "*") );
private String name;
AUTH_ROLE() {
name = this.name();
Roles.add( name );
}
AUTH_ROLE(String name) {
this.name = name;
Roles.add( name );
}
AUTH_ROLE(Rule rule) {
name = this.name();
Roles.add( name, rule );
}
AUTH_ROLE(String name, Rule rule) {
this.name = name;
Roles.add( name, rule );
}
public String getName() {
return name;
}
}
public boolean hasRole(AUTH_ROLE role) {
String[] usersRoles = getLoggedInUsersRoles();
for ( String userRole : usersRoles ) {
if ( role.getName().equals(userRole) )
return true;
}
return false;
}
}
Now, as you can see, the enum AUTH_ROLE is currently *defined* in what is supposed to be a non project specific class, but the Auth class is supposed to be part of the library that is to be used by many projects.
The problem is that with the current design, I am forced to also define the roles and their rules in that same class, Auth, in order to define the method hasRole ( AUTH_ROLE ...) ...
What I would like to do, is to have this enum with all the current logic in it defined ONCE for ALL PROJECTS and allow for the developers in new projects to be able to simply just define the roles and their rules.
The problem I believe exists, is that you cannot extend an enum in Java, so all that in the enum logic ( although simple, not the point! ) actually has to be repeated for each new project and possibly implement and interface provided in the library.
If it were possible to extend, then a new enum would have been able to simply define those roles, that is :
public enum AUTH_ROLE extends authlibrary.Auth.AUTH_ROLE {
sysadmin( new Rule(AdminController.class, "*") );
}
The other option, as I just mentioned, is to define an interface for the enum to implement, although as you can understand we end up with an implementation for each new project, be it as simple as copy and paste.
I am not interested in converting stuff to string or number back and forth before calling the method and what not... This question is not so much about how you can circumvent *the limitations* of the language, but merely to agree/accept that his is a limitation that would otherwise have resulted in cleaner code.
So, does anyone agree with that in this particular case, extending/including another enum would have been benefitial that would have resulted in less code?
Ps. I might be an idiot, so I want to reserve the right call myself an idiot :)
Ask yourself this question: why do you need this to be an enum? If your code is not dealing with a predefined, known set of items that can never be extended, then what you have isn't really a good fit for an enum at all, and you may be better off with singleton objects extending a predefined base class.
If you're not using the useful features of enums -- the inbuilt string/ordinal conversion stuff -- why use them at all? And those useful features can't work in the situation you describe, because they require generation of code that needs to know the names of all the enum values, which now can't happen.
I don't see how this will be valuable. I don't think that inheritance or extending is always the answer. If the enum needs more values, add them. I don't agree that the code is cleaner, or that this represents a serious limitation in the language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With