简体   繁体   中英

Java : Define an enum in a framework (workaround for abstract enum)

I want to create a global framework, that could be reused in many different systems. I would like to include a kind of enum, whose content depends on the project. Concretely, this enum defined different kind of rights with int as the priority (admin=100, advanced user=50, basic user=10 etc.)<- These are just examples, the content of the enum could be totally different for another project. Since this is not possible to have an "abstract" enum, I have unfortunately no idea how I could implement it. Can someone give me some advice (Design pattern for instance, or something else)?

Some use cases :

if myauthorizationLevel=MyEnum.admin then...

if myauthorizationLevel.value>25 then ...

Thanks a lot for your help.

EDIT : According to your answers (all great), I understand what my problem is (Sorry to not have say this before, I couldn't see this then!): I need a function to transform a string in my enum. So, how can I do this interface with a "static" function (I know that static is not possible in interfaces, and that would be a problem), and it gives :

public interface IRole
{}

public enum ERole implements IRole
{}

Then the last function in my library would be :

public function IRole getRole()
{
          return IRole.getValueAsRole(myString);
}       

Sorry again to not have explained this before. I hope this is clear. This is actually my problem since the beginning.

Many thanks

A role interface in your framework (Role.java):

public interface Role {
    int authorizationLevel();
}

A set roles supplied by your framework (Roles.java):

public enum Roles implements Role {
    ADMIN(50),
    POWER_USER(30),
    LIMITED_USER(25),
    GUEST(10);

    private final int authorizationLevel;

    Role(int authorizationLevel) {
        this.authorizationLevel = authorizationLevel;
    }

    @Override
    public int authorizationLevel() {
        return authorizationLevel;
    }
}

A permissions checker in your framework (Authorization.java):

public final class Authorization {
    static boolean isAuthorized(Role role) {
        return Roles.ADMIN.equals(role) ||
            role.authorizationLevel() > 25;
    }
}

A set of roles provided by a client of your framework (ClientRoles.java):

enum ClientRoles implements Role {
    WEB_DESIGNER(50);

    private final int authorizationLevel;

    @Override
    public int authorizationLevel() {
        return authorizationLevel;
    }
}

A client application (RoleBased MSMain.java):

public final class RoleBasedCMSMain {
    public static void main(String[] args) {
        Role role = ClientRoles.WEB_DESIGNER;
        if (Authorization.isAuthorized(role)) {
            System.out.println("You are authorized!");
        }
    }
}

This is reinventing the wheel though. It's probably worth looking at JAAS (Java Authentication and Authorization Service) which solves these problems and integrates with existing frameworks.

An enum can implement an interface, so you use the interface as an "abstract enum"

interface Role{

}
enum SomeRoles implements Role{
ADMIN
}

So in your external API you can use the interface so people can provide their own implementation of Role and yet they can use whats available in your enum

 void fn(Role role);

 fn(new MyRole()); // provided by user
 fn(SomeRoles.ADMIN); //provided by framework 

It is type-safe and yet extensible

You could define an interface with the necessary methods like authorizationLevel(). Since enums can implement interfaces on the project there could be an enum created which implements it. Where such enum should be used, the interface could be used. With generics you can even force that enums implementing the interface should be used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM