Java 1.4 中 enum 的替代方案

发布于 2024-07-25 04:00:39 字数 465 浏览 5 评论 0原文

由于Java 1.4没有枚举,我正在做这样的事情:

public class SomeClass {
     public static int SOME_VALUE_1 = 0;
     public static int SOME_VALUE_2 = 1;
     public static int SOME_VALUE_3 = 2;

     public void receiveSomeValue(int someValue) {
            // do something
     }
 }

receiveSomeValue的调用者应该传递这3个值之一,但他可以传递任何其他int。 如果它是一个枚举,调用者只能传递一个有效值。

receiveSomeValue 应该抛出 InvalidValueException 吗?

Java 5 枚举的良好替代品有哪些?

Since Java 1.4 doesn't have enums I'm am doing something like this:

public class SomeClass {
     public static int SOME_VALUE_1 = 0;
     public static int SOME_VALUE_2 = 1;
     public static int SOME_VALUE_3 = 2;

     public void receiveSomeValue(int someValue) {
            // do something
     }
 }

The caller of receiveSomeValue should pass one those 3 values but he can pass any other int.
If it were an enum the caller could only pass one valid value.

Should receiveSomeValue throw an InvalidValueException?

What are good alternatives to Java 5 enums?

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

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

发布评论

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

评论(4

不…忘初心 2024-08-01 04:00:39

在 1.5 之前版本中最好使用的是 类型安全枚举模式,书中有最好的描述Josh Bloch 的《Effective Java》。 然而它有一些限制,特别是当你处理不同的类加载器、序列化等时。

您还可以查看 Apache Commons Lang 项目,特别是枚举类,就像 John 所写的那样。 它是此模式的实现,并支持构建您自己的枚举。

Best to use in pre 1.5 is the Typesafe Enum Pattern best described in the book Effective Java by Josh Bloch. However it has some limitations, especially when you are dealing with different classloaders, serialization and so on.

You can also have a look at the Apache Commons Lang project and espacially the enum class, like John has written. It is an implementation of this pattern and supports building your own enums.

So尛奶瓶 2024-08-01 04:00:39

我通常会创建所谓的常量类,如下所示:

public class MyConstant 
{
  public static final MyConstant SOME_VALUE = new MyConstant(1);
  public static final MyConstant SOME_OTHER_VALUE = new MyConstant(2);
  ...

  private final int id;


  private MyConstant(int id)
  {
    this.id = id;
  }

  public boolean equal(Object object) 
  {
    ...
  }

  public int hashCode() 
  {
    ...
  }
}

其中 equalshashCode 使用 id

I'd typically create what I call a constant class, some thing like this:

public class MyConstant 
{
  public static final MyConstant SOME_VALUE = new MyConstant(1);
  public static final MyConstant SOME_OTHER_VALUE = new MyConstant(2);
  ...

  private final int id;


  private MyConstant(int id)
  {
    this.id = id;
  }

  public boolean equal(Object object) 
  {
    ...
  }

  public int hashCode() 
  {
    ...
  }
}

where equals and hashCode are using the id.

晨曦÷微暖 2024-08-01 04:00:39

Apache Commons Lang 有一个 Enum 类 运行良好,并且很好地涵盖了 Java 5 Enum 提供的功能。

Apache Commons Lang has an Enum class that works well and pretty well covers what Java 5 Enums offer.

梦回梦里 2024-08-01 04:00:39

如果应用程序代码库将使用大量枚举,那么我更喜欢以下解决方案,我已在应用程序中使用了该解决方案。

基类

public class Enum {
    protected int _enumValue;
    protected Enum(int enumValue) {
        this._enumValue = enumValue;
    }

    public int Value() {
        return this._enumValue;
    }
}

然后,您的枚举将遵循以下模式

实际枚举

public class DATE_FORMAT extends Enum {
    public static final int DDMMYYYY = 1;
    public static final int MMDDYYYY = 2;
    public static final int YYYYMMDD = 3;

    public DATE_FORMAT(int enumValue) {
        super(enumValue);
    }
}

您的代码可以按如下所示使用此枚举

String getFormattedDate(DATE_FORMAT format) {

    String sDateFormatted = "";

    switch (format.Value()) {

        case DATE_FORMAT.DDMMYYYY : 
            break;
        case DATE_FORMAT.MMDDYYYY :
            break;
        case DATE_FORMAT.YYYYMMDD :
            break;
        default:
            break;
    }

    return sDateFormatted;
}

调用者可以使用该函数 as

void callerAPI() {
    DATE_FORMAT format = new DATE_FORMAT(DATE_FORMAT.DDMMYYYY);

    String sFormattedDate = getFormattedDate(format);

}

这还不能完全证明可以防止使用任何整数值初始化派生枚举对象。 然而,它可以为在非枚举环境中工作提供良好的语法指南。

If the application code base is going to use lot of enums, then I would prefer following solution, which I have used in my application.

Base Class

public class Enum {
    protected int _enumValue;
    protected Enum(int enumValue) {
        this._enumValue = enumValue;
    }

    public int Value() {
        return this._enumValue;
    }
}

Your enumerations will then follow these pattern

Actual Enum

public class DATE_FORMAT extends Enum {
    public static final int DDMMYYYY = 1;
    public static final int MMDDYYYY = 2;
    public static final int YYYYMMDD = 3;

    public DATE_FORMAT(int enumValue) {
        super(enumValue);
    }
}

And your code can consume this enum as follows

String getFormattedDate(DATE_FORMAT format) {

    String sDateFormatted = "";

    switch (format.Value()) {

        case DATE_FORMAT.DDMMYYYY : 
            break;
        case DATE_FORMAT.MMDDYYYY :
            break;
        case DATE_FORMAT.YYYYMMDD :
            break;
        default:
            break;
    }

    return sDateFormatted;
}

Caller can use the function as

void callerAPI() {
    DATE_FORMAT format = new DATE_FORMAT(DATE_FORMAT.DDMMYYYY);

    String sFormattedDate = getFormattedDate(format);

}

This is yet not full proof against intitializing derived Enum objects with any integer value. However it can provide good syntactic guideline to work in non-enum environment.

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