在Java中使用具有字符串值的枚举的Switch-Case?

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

Using a Switch-Case with an enum that has string values, in Java?

问题

在我的应用程序中,我收到一个包含硬编码值的响应。我已经创建了一个枚举,其中包含这些响应,如下所示:

enum ResponseCode {
    APPROVED("AP"),
    REJECTED("RJ"),
    ...;

    private final String code;

    private ResponseCode(String code) {
        this.code = code;
    }
}

根据我收到的代码,我希望以不同的方式处理响应。我考虑使用switch-case来解决这个问题。

String response = functionThatReturnsString(); // 可能是"AP"或"RJ"等。

switch(response) {
case ResponseCode.APPROVED:
    // 在这里处理响应
    break;
...
}

然而,我得到错误消息 类型不匹配:无法从ResponseCode转换为String

是否有一种方法可以解决这个问题?或者有没有更好的方法来做这件事?

英文:

In my application, I receive a response that has hard-coded values it can be.
I have created an enum with those responses as so:

enum ResponseCode {
    APPROVED("AP"),
    REJECTED("RJ"),
    ...;

    private final String code;

    private ResponseCode(String code) {
        this.code = code;
    }
}

I would handle a response differently depending on which code I receive. I thought of using a switch-case to solve this problem.

String response = functionThatReturnsString(); // Could be "AP" or "RJ" etc.

switch(response) {
case ResponseCode.APPROVED:
    // Response handling here
    break;
...
}

However, I get the error Type mismatch: cannot convert from ResponseCode to String

Is there a way to get around this? Or is there a better way to do this?

答案1

得分: 3

你正在使用String进行切换,但你却将ResponseCode作为情况,这是没有意义的。String永远无法与ResponseCode匹配。

有多种解决方法。你可以使用static final常量代替枚举:

final class ResponseCode {
    private ResponseCode() {}
    public static final String APPROVED = "AP";
    public static final String REJECTED = "RJ";
}

然后可以这样使用switch

switch (someResponseCode) {
    case ResponseCode.APPROVED -> { ... }
    case ResponseCode.REJECTED -> { ... }
    // 记得处理不匹配任何响应代码的情况!
    default -> { ... }
}

另一种方法是编写一个将String转换为枚举的方法:

public static ResponseCode fromString(String s) {
    return switch (s) {
        case "AP" -> APPROVED;
        case "RJ" -> REJECTED;
        default -> null;
    };
    // 或者:Arrays.stream(ResponseCode.values()).filter(x -> x.code.equals(s)).findFirst().orElse(null);
}

然后可以像这样使用switch

switch (ResponseCode.fromString(someResponseCode)) {
    case APPROVED -> { ... }
    case REJECTED -> { ... }
    // 记得处理不匹配任何响应代码的情况!
    case null -> { ... }
}
英文:

You are switching on a String, but you are using ResponseCode as the cases, which doesn't make sense. A String can never match a ResponseCode.

There are multiple ways around this. Instead of an enum, you can use static final constants:

final class ResponseCode {
    private ResponseCode() {}
    public static final String APPROVED = "AP";
    public static final String REJECTED = "RJ";
}
switch (someResponseCode) {
    case ResponseCode.APPROVED -> { ... }
    case ResponseCode.REJECTED -> { ... }
    // remember to handle the case of not matching any response code!
    default -> { ... }
}

Alternatively, write a method that converts Strings to your enum:

public static ResponseCode fromString(String s) {
    return switch (s) {
        case "AP" -> APPROVED;
        case "RJ" -> REJECTED;
        default -> null;
    };
    // or: Arrays.stream(ResponseCode.values()).filter(x -> x.code.equals(s)).findFirst().orElse(null);
}

Note that you can remove the code field, if you don't need it for anything else.

Then you can switch like this:

switch (ResponseCode.fromString(someResponseCode)) {
    case APPROVED -> { ... }
    case REJECTED -> { ... }
    // remember to handle the case of not matching any response code!
    case null -> { ... }
}

答案2

得分: 2

将您的字符串转换为ResponseCode:

ResponseCode rc = ResponseCode.valueOf(response);

然后根据该值进行切换:

switch (rc) {
  case APPROVED:
    // 在这里处理响应
    break;
  // 其他情况...
}

您可能还应该处理无法将字符串转换为枚举值的情况。

英文:

Convert your String to a ResponseCode:

ResponseCode rc= ResponseCode.valueOf(response);

then switch on that value:

switch (rc) {
  case APPROVED:
    // Response handling here
    break;
...
}

You should probably also handle the case where the String can't be converted to an enum value.

答案3

得分: 1

你可以直接使用枚举:

ResponseCode responseCode = ...

switch(responseCode) {
case APPROVED:
    // 在这里处理响应
    break;
...
}
英文:

You can use the enum directly:

ResponseCode responseCode = ...

switch(responseCode) {
case APPROVED:
    // Response handling here
    break;
...
}

huangapple
  • 本文由 发表于 2023年8月10日 14:59:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873286.html
匿名

发表评论

匿名网友

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

确定