抽象 Java 枚举

发布于 2024-08-21 00:48:11 字数 360 浏览 3 评论 0原文

我编写了一个应该依赖于枚举的库,但实际的枚举应该由我的库的用户定义。

在以下示例中,authorize 方法需要枚举类型Permission 的参数。

acl.authorize(userX, Permission.READ, Permission.WRITE)

我的库应该能够处理库用户定义的任意权限。但如果没有 Permission 枚举,我无法编译我的库。所以我需要像

abstract enum Permission

我的图书馆一样的东西。有解决方法可以做到这一点吗?

I write a library that should rely on enums but the actual enum should be defined by the user of my library.

In the following example the authorize method requires parameters of the enum type Permission.

acl.authorize(userX, Permission.READ, Permission.WRITE)

My library should be able to handle arbitrary permissions defined by the library user. But I cannot compile my library without a Permission enum. So I would need something like

abstract enum Permission

in my library. Is there a workaround to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话墙 2024-08-28 00:48:11

我将使用枚举然后实现的接口。例如,

public interface PermissionType{}

客户端将使用类似的东西来定义枚举,例如

public enum Permission implements PermissionType
[...]

然后您的 API 将使用 PermissionType 类型接受参数

I would use an interface which the enum would then implement. Something along the lines of

public interface PermissionType{}

which would be used by e.g. the client to define an enum such as

public enum Permission implements PermissionType
[...]

Then your API would accept parameters using the PermissionType type

梦里°也失望 2024-08-28 00:48:11

以下是我建议的步骤。

  1. 编写注释 - public @interface Permission
  2. 让用户使用该注释来注释他的每个权限枚举:

    <前><代码>@Permission
    公共枚举 ConcretePermissionEnum {..}

  3. 使您的 authorize 方法如下所示:

    公共布尔授权(用户用户,枚举...权限){
        for(枚举权限:权限){
           if (permission.getClass().isAnnotationPresent(Permission.class)){
              // 处理权限
           }
        }
    }
    

如果你希望你的 Permission 枚举有一些特定的方法,或者只是想要一个“标记”,那么你可以让用户枚举实现您的接口(而不是注释):

interface PermissionInterface {..}
enum ConcretePermission implements PermissionInterface

这将启用编译时检查,而不是运行时检查,就像注释方法一样,授权方法签名如下所示:

public boolean authorize(User user, PermissionInterface... permissions)

Here are the steps I'd suggest.

  1. write an annotation - public @interface Permission
  2. make the user annotate each of his permission enums with that annotation:

    @Permission
    public enum ConcretePermissionEnum {..}
    
  3. Make your authorize method look like:

    public boolean authorize(User user, Enum... permissions) {
        for (Enum permission : permissions) {
           if (permission.getClass().isAnnotationPresent(Permission.class)){
              // handle the permission
           }
        }
    }
    

If you want your Permission enums to have some specific methods, or just want a 'marker', then you can make the user enums implement an interface of yours (instead of being annotated):

interface PermissionInterface {..}
enum ConcretePermission implements PermissionInterface

This will enable a compile-time, rather than a run-time check, as with the annotation approach, with the authorize method signature looking like:

public boolean authorize(User user, PermissionInterface... permissions)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文