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