public TypeValuePair evaluate(final FormulaContext context, final ParameterCallback parameters)
      throws EvaluationException {
    if (parameters.getParameterCount() != 3) {
      throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
    }

    // 0: Configuration-indicator (gives the pattern indirectly) never the pattern itself
    // 1: the report-path
    // 2: the parameter as 2d-array (name value pairs)

    final TypeRegistry typeRegistry = context.getTypeRegistry();
    final String configIndicator =
        typeRegistry.convertToText(parameters.getType(0), parameters.getValue(0));
    String path;
    try {
      path = typeRegistry.convertToText(parameters.getType(1), parameters.getValue(1));
    } catch (EvaluationException ee) {
      if (LibFormulaErrorValue.ERROR_NA_VALUE.equals(ee.getErrorValue())) {
        path = null;
      } else {
        throw ee;
      }
    }
    final ArrayCallback parameter =
        typeRegistry.convertToArray(parameters.getType(2), parameters.getValue(2));

    final LinkCustomizer pattern = createLinkCustomizer(configIndicator);
    return new TypeValuePair(
        TextType.TYPE,
        pattern.format(
            context, configIndicator, path, computeParameterEntries(parameter, typeRegistry)));
  }
  public TypeValuePair evaluate(final FormulaContext context, final ParameterCallback parameters)
      throws EvaluationException {
    final int parameterCount = parameters.getParameterCount();
    if (parameterCount < 1) {
      throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
    }

    final Type type1 = parameters.getType(0);
    final Object value = parameters.getValue(0);

    final TypeRegistry typeRegistry = context.getTypeRegistry();
    final Number number = typeRegistry.convertToNumber(type1, value);

    if (number == null) {
      throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
    }

    int intValue = number.intValue();
    if (intValue < 0) {
      intValue *= -1;
    }

    if (intValue % 2 == 1) {
      return RETURN_TRUE;
    }

    return RETURN_FALSE;
  }
  public TypeValuePair evaluate(final FormulaContext context, final ParameterCallback parameters)
      throws EvaluationException {

    final int parameterCount = parameters.getParameterCount();
    if (parameterCount < 1) {
      throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
    }

    final TypeRegistry typeRegistry = context.getTypeRegistry();
    final double value =
        typeRegistry.convertToNumber(parameters.getType(0), parameters.getValue(0)).doubleValue();

    if (value == 0) {
      return new TypeValuePair(TextType.TYPE, "0 ");
    }

    boolean fixedSize = false;
    int precision = 0;

    if (parameters.getParameterCount() > 1) {
      fixedSize = true;
      precision =
          typeRegistry.convertToNumber(parameters.getType(1), parameters.getValue(1)).intValue();
      if (parameters.getParameterCount() == 3) {
        final Boolean rawFixedSize =
            typeRegistry.convertToLogical(parameters.getType(2), parameters.getValue(2));
        fixedSize = rawFixedSize.booleanValue();
      }
    }

    final int log10 = computeLog10(value);

    // index will allow us to find the the index of the suffix to use
    final int index = (int) (Math.floor(log10 / 3.0) + OFFSET);
    if (index < 0 || index >= SUFFIXES.length) {
      throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_UNEXPECTED_VALUE);
    }

    // Find the adequate precision. % operator behaves badly in negative results, so we need to make
    // it work as expected
    final int roundPrecision =
        fixedSize ? (log10 - precision) : (log10 - (precision + (3 + log10 % 3) % 3));

    // Round the value
    final double roundingScale = Math.pow(10, roundPrecision);
    final double rounded = Math.round(value / roundingScale) * roundingScale;

    // Get it's eng format. Get it as string without trailing 0's
    final double outputValue = rounded / Math.pow(10, Math.floor(log10 / 3.0) * 3);
    final int outputValueDecimalPlaces = Math.max(1, computeLog10(outputValue));

    final Locale locale = context.getLocalizationContext().getLocale();
    final NumberFormat decimalFormat =
        createDecimalFormat(fixedSize, outputValueDecimalPlaces, precision, locale);
    final String result = decimalFormat.format(outputValue) + SUFFIXES[index];
    return new TypeValuePair(TextType.TYPE, result);
  }
Example #4
0
  public TypeValuePair evaluate(final FormulaContext context, final TypeValuePair value)
      throws EvaluationException {
    final TypeRegistry typeRegistry = context.getTypeRegistry();

    Boolean valBool = typeRegistry.convertToLogical(value.getType(), value.getValue());

    if (!valBool.booleanValue()) {
      return RETURN_TRUE;
    } else {
      return RETURN_FALSE;
    }
  }
  private ParameterEntry[] computeParameterEntries(
      final ArrayCallback paraCallback, final TypeRegistry typeRegistry)
      throws EvaluationException {
    if (paraCallback.getColumnCount() == 0) {
      return new ParameterEntry[0];
    }
    if (paraCallback.getColumnCount() != 2) {
      throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
    }

    final int rowCount = paraCallback.getRowCount();
    final ParameterEntry[] retval = new ParameterEntry[rowCount];
    for (int r = 0; r < rowCount; r++) {
      final String name =
          typeRegistry.convertToText(paraCallback.getType(r, 0), paraCallback.getValue(r, 0));
      try {
        final Object value = paraCallback.getValue(r, 1);
        if (value instanceof ArrayCallback) {
          retval[r] = new ParameterEntry(name, TypeUtil.normalize((ArrayCallback) value));
        } else if (value instanceof Sequence) {
          retval[r] = new ParameterEntry(name, TypeUtil.normalize((Sequence) value));
        } else if (value instanceof Collection) {
          final Collection c = (Collection) value;
          retval[r] = new ParameterEntry(name, c.toArray());
        } else {
          retval[r] = new ParameterEntry(name, value);
        }
      } catch (EvaluationException ee) {
        if (ee.getErrorValue() == LibFormulaErrorValue.ERROR_NA_VALUE) {
          retval[r] = new ParameterEntry(name, null);
        } else {
          throw ee;
        }
      }
    }
    return retval;
  }