从枚举获取项作为字符串

huangapple go评论56阅读模式
英文:

Get Items from Enum as String

问题

我有这个枚举结构:

public enum PurchaseOrderStatus {
    IN_REVIEW("in_review"),
    CANCELED("canceled"),
    DECLINED("declined"),
    WAITING_REVIEW("waiting_review"),
    IN_TRANSIT("in_transit"),
    COMPLETED("completed");

    private String text;

    PurchaseOrderStatus(String text) {
        this.text = text;
    }

    public String getText() {
        return this.text;
    }

    public static Optional<PurchaseOrderStatus> fromText(String text) {
        return Arrays.stream(values())
                .filter(pos -> pos.text.equalsIgnoreCase(text))
                .findFirst();
    }
}

我如何将项目获取为字符串?当我尝试使用PurchaseOrderStatus.IN_REVIEW.getText()时,我会得到错误消息Attribute value must be constant

英文:

I have this Enum Structure:

public enum PurchaseOrderStatus {
    IN_REVIEW(&quot;in_review&quot;),
    CANCELED(&quot;canceled&quot;),
    DECLINED(&quot;declined&quot;),
    WAITING_REVIEW(&quot;waiting_review&quot;),
    IN_TRANSIT(&quot;in_transit&quot;),
    COMPLETED(&quot;completed&quot;);

    private String text;

    PurchaseOrderStatus(String text) {
        this.text = text;
    }

    public String getText() {
        return this.text;
    }

    public static Optional&lt;PurchaseOrderStatus&gt; fromText(String text) {
        return Arrays.stream(values())
                .filter(bl -&gt; bl.text.equalsIgnoreCase(text))
                .findFirst();
    }
}

How I can get item as String? When I try PurchaseOrderStatus.IN_REVIEW.getText() I get error Attribute value must be constant.

答案1

得分: 1

PurchaseOrderStatus.IN_REVIEW.getText() 是它。

那个错误与“如何将枚举项作为字符串获取”无关。它与您只能在注解选项中使用常量有关。

您可能正在尝试在代码中执行以下操作:

@Foobar(PurchaseOrderStatus.IN_REVIEW.getText())
  • 您不能这样做。

如果这是您自己的注解,请注意注解“字段”可以是枚举类型的。因此,您当前拥有的内容:

public @interface Foobar {
    String value() default "hello";
}

可以改为:

public @interface Foobar {
    PurchaseOrderStatus value() default PurchaseOrderStatus.IN_REVIEW;
}

如果您无法更改注解的定义,那么这将不起作用,真的没有任何解决方法,您将不得不在某个地方“硬编码”该字符串。

请注意,您的整个枚举设计有点奇怪;您也可以编写:

public enum PurchaseOrderStatus {
    IN_REVIEW, CANCELED, DECLINED, WAITING_REVIEW, IN_TRANSIT, COMPLETED;

    public String getText() {
        return name().toLowerCase();
    }
}
英文:

PurchaseOrderStatus.IN_REVIEW.getText() is it.

That error is not related to 'how to get items from enum as string'. it is related to the fact that you can only use constants in annotation options.

You must be trying to do this in your code:

@Foobar(PurchaseOrderStatus.IN_REVIEW.getText()) - you can't do that.

If it's your own annotation, note that annotation 'fields' can be enum-typed. So where you currently have:

public @interface Foobar {
    String value() default &quot;hello&quot;;
}

make that:

public @interface Foobar {
    PurchaseOrderStatus value() default PurchaseOrderStatus.IN_REVIEW;
}

If you can't change the definition of the annotation, well, this won't work, and there really isn't any workaround, you're going to have to 'hardcode' the string someplace.

Note that the entire design of your enum is a bit wonky; you could have just as well written:

public enum PurchaseOrderStatus {
    IN_REVIEW, CANCELED, DECLINED, WAITING_REVIEW, IN_TRANSIT, COMPLETED;

    public String getText() {
        return name().toLowerCase();
    }
}

答案2

得分: -1

你的代码应该能正常工作;然而,我建议进行以下更改(因为它保证了 text 永不改变)。

private String text;

改为

private final String text;
英文:

Your code should work fine; however, I would recommend this change (since it guaranteed that text never changes).

Make

private String text;

into

private final String text;

huangapple
  • 本文由 发表于 2020年9月8日 06:25:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63784958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定