简体   繁体   English

从 ENUM 返回值

[英]Return value from ENUM

I have a Java ENUM where I store different statuses:我有一个 Java ENUM,用于存储不同的状态:

public enum BusinessCustomersStatus {
    A("active", "Active"),
    O("onboarding", "Onboarding"),
    NV("not_verified", "Not Verified"),
    V("verified", "Verified"),
    S("suspended", "Suspended"),
    I("inactive", "Inactive");

    @Getter
    private String shortName;

    @JsonValue
    @Getter
    private String fullName;

    BusinessCustomersStatus(String shortName, String fullName) {
        this.shortName = shortName;
        this.fullName = fullName;
    }

    // Use the fromStatus method as @JsonCreator
    @JsonCreator
    public static BusinessCustomersStatus fromStatus(String statusText) {
        for (BusinessCustomersStatus status : values()) {
            if (status.getShortName().equalsIgnoreCase(statusText)) {
                return status;
            }
        }

        throw new UnsupportedOperationException(String.format("Unknown status: '%s'", statusText));
    }
}

Full code: https://github.com/rcbandit111/Search_specification_POC/blob/main/src/main/java/org/merchant/database/service/businesscustomers/BusinessCustomersStatus.java完整代码: https : //github.com/rcbandit111/Search_specification_POC/blob/main/src/main/java/org/merchant/database/service/businesscustomers/BusinessCustomersStatus.java

The code works well when I want to get the list of items into pages for the value fullName because I use @JsonValue annotation.由于我使用@JsonValue注释,因此当我想将项目列表放入页面中以获取值fullName时,该代码运行良好。

I have a case where I need to get the shortValue for this code:我有一个需要获取此代码的shortValue的情况:

return businessCustomersService.findById(id).map( businessCustomers -> businessCustomersMapper.toFullDTO(businessCustomers));

source: https://github.com/rcbandit111/Search_specification_POC/blob/316c97aa5dc34488771ee11fb0dcf6dc1e4303da/src/main/java/org/merchant/service/businesscustomers/BusinessCustomersRestServiceImpl.java#L77来源: https : //github.com/rcbandit111/Search_specification_POC/blob/316c97aa5dc34488771ee11fb0dcf6dc1e4303da/src/main/java/org/merchant/service/businesscustomers/BusinessCustomersRestServiceImpl.java#L77

But I get fullValue .但我得到fullValue Do you know for single row how I can map the shortValue ?您知道单行如何映射shortValue吗?

Yo can use this :你可以使用这个:

public enum decizion{
         YES("Y"), NO("N"), OTHER;
          
         String key;
      
         decizion(String key) { this.key = key; }
     
         //default constructor, used only for the OTHER case, 
         //because OTHER doesn't need a key to be associated with. 
         decizion() { }

         static decizion getValue(String x) {
             if ("Y".equals(x)) { return YES; }
             else if ("N".equals(x)) { return NO; }
             else if (x == null) { return OTHER; }
             else throw new IllegalArgumentException();
         }
    }

Then, in the method, you can just do:然后,在该方法中,您可以执行以下操作:

public static decizion yourDecizion() {
    ...
   String key = ...
   return decizion.getValue(key);
}

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

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