简体   繁体   English

Java枚举作为BigInteger值

[英]Java enumeration as BigInteger value

I have Enumeration classes in my application and I am using this enumerations to compare value of BigIntegers like this: 我的应用程序中有Enumeration类,并且正在使用此枚举来比较BigIntegers的值,如下所示:

if (user.getObjId.equals(RoleEnum.ADMIN.getValue())) {
    //something happens
}

The question is, can I modify this enumeration class to use it without calling getValue() method, as shown in following code sample? 问题是,是否可以在不调用getValue()方法的情况下修改此枚举类以使用它,如以下代码示例所示?

if (user.getObjId.equals(RoleEnum.ADMIN)) {
    //something happens
}

Here is my Enumeration class: 这是我的枚举类:

public enum RoleEnum {
    ADMIN(1), USER(2);
    private final BigInteger value;

    private RoleEnum(int value) {
        this.value = BigInteger.valueOf(value);
    }

    public BigInteger getValue() {
        return value;
    }
}

the idea behind BigInteger is that you can work with numbers that are so big you can just type in a numeric way on the IDE... BigInteger背后的想法是,您可以使用很大的数字,只需在IDE上以数字方式键入即可。

you should do: 你应该做:

ADMIN("1"), USER("2");
private RoleEnum(String value) {
    this.value = BigInteger.valueOf(value);

The answer to your question is simply 'No'. 您的问题的答案就是“否”。

It all depends on the equals method of the objectId. 这完全取决于objectId的equals方法。 You can not override the equals method of the enum, but you could possibly override/declare the equals method on the objectId (it's hard to say from the information you provided). 你不能覆盖的枚举的equals方法,但你可能重写/声明equals方法的OBJECTID(很难从你提供的信息说)。

The implementation of the equals method can then check for the type of the parameter to be of the enum type, and in that case, compare the values. 然后,equals方法的实现可以检查参数类型是否为枚举类型,并在这种情况下比较这些值。 For example: 例如:

public boolean equals(Object o){
   if(o instanceof RoleEnum){
      return this.value.equals(((RoleEnum)o).getValue());
   }
   ...
}

But I assume, your objId is of type BigInteger, so you cant override the equals method. 但是我认为,您的objId是BigInteger类型,因此您不能覆盖equals方法。 In that case, the answer is as mention above: 'No' 在这种情况下,答案如上所述。

If something's not very neat refactor out a method so it looks neat again (especially if it happens in more than one place): 如果某些东西不是很整洁,则可以重构出一种方法,使其看起来再次整洁(尤其是如果它发生在多个地方):

if (user.isRole(RoleEnum.ADMIN))

on User (I'm assuming the name of the type of user ): 关于User (我假设user类型的名称):

public boolean isRole(RoleEnum role) {
    return getObjId.equals(role.getValue());
}

Or if it's not not possible to modify User or reference the enum from there, then maybe a static: 或者,如果无法修改User或从那里引用枚举,则可能是静态的:

public final class UserHelpers {

    private UserHelpers(){}

    public static boolean userIsRole(User user, RoleEnum role) {
        return user.getObjId.equals(role.getValue());
    }
}

Usage: 用法:

if (UserHelpers.userIsRole(user, RoleEnum.ADMIN))

Or with the static imported: 还是用static导入的:

if (userIsRole(user, RoleEnum.ADMIN))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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