英文:
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("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(bl -> 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 "hello";
}
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论