简体   繁体   中英

How to return boolean “if-then-else” statement in a single return

I am getting a sonar issue of Replace this if-then-else statement by a single return statement. As I have three conditions in the if condition, how can I return this without if-else?

  public boolean isDateRangeExceedOneYear(CardPluginInterface plugin, Date begin, Date end)
  {
    if (!DateTimeToolkit.getInstance().isEmpty(begin)
        && ((CardValidatorInterface) plugin).isValidateYearDateRangeFor()
        && DateTimeToolkit.getInstance().compare(
        DateTimeToolkit.getInstance().add(new DateTime(begin, new Time((short) 0, (short) 0, (short) 0)), 1, 1).date,
        end) <= 0)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

For the above you can use

return !DateTimeToolkit.getInstance().isEmpty(begin)
    && ((CardValidatorInterface) plugin).isValidateYearDateRangeFor()
    && DateTimeToolkit.getInstance().compare(
    DateTimeToolkit.getInstance().add(new DateTime(begin, new Time((short) 0, (short) 0, (short) 0)), 1, 1).date,
    end) <= 0;

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