Example #1
1
  private void evaluateAndFormat() throws Exception {
    Double result = (new Expression(renderedText)).evaluate();
    Long iResult = new Long(Math.round(result));

    if (formatSpec == null)
      renderedText =
          (result.equals(iResult.doubleValue())) ? iResult.toString() : result.toString();
    else {
      // char conversion = formatSpec.charAt(formatSpec.length() - 1);

      if ("aAdhHoOxX".indexOf(conversion) >= 0) // ...use the integer
      renderedText = String.format(formatSpec, iResult);
      else if ("bB".indexOf(conversion) >= 0) // ...use boolean
      renderedText = (result == 0.0) ? "false" : "true";
      else if ("sScC".indexOf(conversion) >= 0) // ...string & character formatting; use the double
      {
        String sString;
        boolean isInt = result.equals(iResult.doubleValue());
        if (isInt) sString = String.format(formatSpec, iResult.toString());
        else sString = String.format(formatSpec, result.toString());

        renderedText = sString.replaceAll(" ", HtmlUtil.NBSP.html());
      } else if ("tT".indexOf(conversion) >= 0) // ...date
      {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(iResult);
        renderedText = String.format(formatSpec, cal.getTime());
      } else if ("eEfgG".indexOf(conversion) >= 0) // ...use the double
      renderedText = String.format(formatSpec, result);
      else renderedText = makeInvalidVariableExpression("invalid format: " + formatSpec);
    }
  }
  private Maybe<String> evaluateAndFormat() {
    Maybe<Double> evaluation = (new Expression(expression)).evaluate();
    if (evaluation.isNothing()) {
      return Maybe.nothingBecause("invalid expression: " + expression);
    }

    Double result = evaluation.getValue();
    Long iResult = Math.round(result);

    if (format == null)
      return new Maybe<String>(
          result.equals(iResult.doubleValue()) ? iResult.toString() : result.toString());

    if ("aAdhHoOxX".indexOf(conversion) >= 0) // ...use the integer
    return new Maybe<String>(String.format(format, iResult));

    if ("bB".indexOf(conversion) >= 0) // ...use boolean
    return new Maybe<String>(result == 0.0 ? "false" : "true");

    if ("sScC".indexOf(conversion) >= 0) { // ...string & character formatting; use the double
      String sString;
      boolean isInt = result.equals(iResult.doubleValue());
      if (isInt) sString = String.format(format, iResult.toString());
      else sString = String.format(format, result.toString());

      return new Maybe<String>(sString.replaceAll(" ", HtmlUtil.NBSP.html()));
    }

    if ("tT".indexOf(conversion) >= 0) { // ...date
      Calendar cal = Calendar.getInstance();
      cal.setTimeInMillis(iResult);
      return new Maybe<String>(String.format(format, cal.getTime()));
    }

    if ("eEfgG".indexOf(conversion) >= 0) // ...use the double
    return new Maybe<String>(String.format(format, result));

    return Maybe.nothingBecause("invalid format: " + format);
  }