开发者社区> 问答> 正文

java如何消除繁琐的if else 语句?

求助:一堆的if else 嵌套

private Map versionCheck(Map mapParam) {
    Map msg = new HashMap();
    if(!"".equals(PMSUtil.isNull(mapParam.get("IS_INTERNET")))){
      String checkVersion =SystemGlobals.getProperty("IS_CHECK");
      if("1".equals(checkVersion)){
          String versions =SystemGlobals.getProperty("VERSION");
          String version =PMSUtil.isNull(mapParam.get("VERSION"));
          String taxRegisterNo=PMSUtil.isNull(mapParam.get(""));
          String whiteList =SystemGlobals.getProperty("NO_CHECK_TAX_REGISTER_NO");
          if((","+whiteList+",").indexOf((","+taxRegisterNo+","))==-1){
              if((","+versions+",").indexOf(","+version+",")==-1){//找不到
                  msg.put("RESULT", "0001");
                  msg.put("MSG", "FAILD");
              }else{
                 msg.put("RESULT", ""0000"");
              }
          }else{
            msg.put("RESULT", ""0000"");
          }
      }else{
          msg.put("RESULT", ""0000"");
      }
    }else{
        msg.put("RESULT", ""0000"");
    }
    return msg;
}

展开
收起
蛮大人123 2016-03-04 16:48:03 4619 0
2 条回答
写回答
取消 提交回答
  • 使用tmf框架,通过拓展点的方式来杜绝if else这种方式
    2019-07-17 18:52:32
    赞同 展开评论 打赏
  • 我说我不帅他们就打我,还说我虚伪

    不要用 PMSUtil 了,直接上 org.apache.commons.lang.StringUtils

     private Map versionCheck(Map mapParam) {
            Map msg = new HashMap();
            msg.put("RESULT", "0000");
    
            String isInternet = (String) mapParam.get("IS_INTERNET");
            if (StringUtils.isBlank(isInternet)) {
                return msg;
            }
    
            String isCheck = SystemGlobals.getProperty("IS_CHECK");
            if (!StringUtils.equals(isCheck, "1")) {
                return msg;
            }
    
            String globalVersions = SystemGlobals.getProperty("VERSION");
            String currentVersion = (String) mapParam.get("VERSION");
            if (!StringUtils.contains("," + globalVersions + ",", "," + currentVersion + ",")) {
                return msg;
            }
    
            String whiteList = SystemGlobals.getProperty("NO_CHECK_TAX_REGISTER_NO");
            String taxRegisterNo = (String) mapParam.get("");
            if (!StringUtils.contains("," + whiteList + ",", "," + taxRegisterNo + ",")) {
                msg.put("RESULT", "0001");
                msg.put("MSG", "FAILD");
                return msg;
            }
    
            return Collections.emptyMap();
        }
    2019-07-17 18:52:32
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载