简体   繁体   中英

java conditional how do I make sure it checks all if statements

I am having trouble getting the below query method to work. I need it to return if isRestricted all of the reports with restricted in the status. If isClosed then return all reports that are closed for the status. It works for isRestricted , returns all reports in restricted status. It also works for returning everything except restricted and closed which is good, but just returns everything for the if isClosed condition. Now if I move the isClosed conditions to the top then it works for isClosed, but doesn't work for isRestricted . What am I missing?

 protected String getRestrictedOrClosedTerm() {
    if (isRestricted == null || "false".equals(isRestricted)) {
        return "-status:restricted  -status:closed ";
    }
    if (isRestricted.isEmpty()) {
        return "";
    }
    if ("true".equals(isRestricted)) {
       return "+status:restricted ";
    }
    if (isClosed == null || "false".equals(isClosed)) {
        return "-status:closed ";
    }
    if (isClosed.isEmpty()) {
        return "";
    }
    if ("true".equals(isClosed)) {
        return "+status:closed ";
    }

    return "";
} 

It is like it is stopping at the:

 if ("true".equals(isRestricted)) {
       return "+status:restricted ";
    }

Interestingly if I use this I get the desired results, but not sure why:

  protected String getRestrictedOrClosedTerm() {
     if ("true".equals(isClosed)) {
         return "+status:closed ";
     }
     if ("true".equals(isRestricted)) {
         return "+status:restricted ";
     }
     if (isRestricted == null || "false".equals(isRestricted)) {
        return "-status:restricted  -status:closed ";
     }
    return "";
}

When you use a return in a method, it exits that method with the value returned. If you use something like a StringBuilder , you can see all the output:

        protected String getRestrictedOrClosedTerm() 
        {
            StringBuilder sb = new StringBuilder();
            if (isRestricted == null || "false".equals(isRestricted)) {
                sb.append("-status:restricted  -status:closed\n");
            }
            if (isRestricted.isEmpty()) {
                sb.append("empty\n");
            }
            if ("true".equals(isRestricted)) {
                sb.append("+status:restricted\n");
            }
            if (isClosed == null || "false".equals(isClosed)) {
                sb.append("-status:closed\n");
            }
            if (isClosed.isEmpty()) {
                sb.append("empty\n");
            }
            if ("true".equals(isClosed)) {
                sb.append("+status:closed\n");
            }

            return sb.toString();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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