code: private static String negate(String conditionTemplate) {
      if (conditionTemplate.startsWith("!")
          && !BOOLEAN_BINARY_OPERATOR.matcher(conditionTemplate).find()) {
        return conditionTemplate.substring(1);
      } else if (ANY_OPERATOR.matcher(conditionTemplate).find()) {
        // The condition might already enclosed in a bracket, but we can't simply check for opening
        // and closing brackets at the start and end of the string, as that misses cases like
        // (a || b) && (c || d). Attempting to determine if the initial and closing bracket are paired
        // requires understanding character constants and strings constants, so for simplicity we
        // just add unnecessary brackets.
        return "!(" + conditionTemplate + ")";
      } else {
        return "!" + conditionTemplate;
      }
    }
docs: Negates {@code conditionTemplate}, removing unnecessary brackets and double-negatives if
  possible.