如何将下面的Java查询放入switch case中?

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

How can I put the below java query in switch case?

问题

// 如何将下面的 Java 查询放入 switch case 中?我正在使用 spring-mobile 项目,我需要将下面的代码放入 switch 帮助中。

try {
    switch (device.getType()) {
        case MOBILE:
            redirectUri = cfg.getEmail().getVerification().getMobile_success_redirect_url();
            break;
        case TABLET:
            redirectUri = cfg.getEmail().getVerification().getTablet_success_redirect_url();
            break;
        case NORMAL:
            redirectUri = cfg.getEmail().getVerification().getWeb_success_redirect_url();
            break;
        default:
            redirectUri = cfg.getEmail().getVerification().getWeb_failure_redirect_url();
            break;
    }
} catch (Exception ex) {
    redirectUri = cfg.getEmail().getVerification().getWeb_failure_redirect_url();
    logger.error("Failed to verify email. Cause: {}", ex);
}

response.sendRedirect(redirectUri);
英文:

// How can I put the below java query in switch case? I am working with spring-mobile project and I need to put the below code in switch help.

try {
    if (device.isMobile()) {
        redirectUri = cfg.getEmail().getVerification().getMobile_success_redirect_url();
    }
} catch (Exception ex) {
    redirectUri = cfg.getEmail().getVerification().getMobile_failure_redirect_url();
    logger.error("Failed to verify email. Cause: {}", ex);
}
try {
    if (device.isTablet()) {
        redirectUri = cfg.getEmail().getVerification().getTablet_success_redirect_url();
    }
} catch (Exception ex) {
    redirectUri = cfg.getEmail().getVerification().getTablet_success_redirect_url();
    logger.error("Failed to verify email. Cause: {}", ex);
}
try {
    if (device.isNormal()) {
        redirectUri = cfg.getEmail().getVerification().getWeb_success_redirect_url();
    }
} catch (Exception ex) {
    redirectUri = cfg.getEmail().getVerification().getWeb_failure_redirect_url();
    logger.error("Failed to verify email. Cause: {}", ex);
}

response.sendRedirect(redirectUri);

答案1

得分: 0

阅读@Naman的评论。我同意,这里有更多的细节。

我不确定device类型的类是否在您的控制之下。

但是如果在您的控制之下,您应该可能有一个表示设备类型的字段,可以是一个包含3个值MOBILE,TABLET,NORMAL的枚举。

现在,您可以对这个Enum进行switch case操作,每个case都包含针对特定用例的代码。

这个switch case的方法应该接受两个参数,新创建的DeviceType枚举和由cfg.getEmail().getVerification()返回的对象。

粗略的轮廓,但我希望您能明白要点。

String getRedirectUri(DeviceType deviceType, Verification verification) {
    switch(deviceType) {
        case TABLET:
        case MOBILE:
        case NORMAL:
        default
    }
}
英文:

Read the comment by @Naman. I agree, here are a few more details.

I am not sure if the class of type device is in your control.

But if it is, you should perhaps have field denoting device type, which can be an Enum with 3 values MOBILE, TABLET, NORMAL.

Now you can have a switch case on this Enum with each case containing code for that specific use case.

The method for this switch case should accept 2 params, the newly created DeviceType enum and the Object that is returned by cfg.getEmail().getVerification().

Rough outline but I hope you get the point.

String getRedirectUri(DeviceType deviceType, Verification verification) {
switch(deviceType) {
case TABLET:
case MOBILE:
case NORMAL:
default
}
}

huangapple
  • 本文由 发表于 2020年5月30日 22:22:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/62103784.html
匿名

发表评论

匿名网友

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

确定