20050722 Friday July 22, 2005

Big freaking numbers II

After I finished my blog entry yesterday, I kept staring at the code for a while, thinking if it could be simplified by changing the API of BigNumbers. I already had a convenience class (Function) that didn't feel very convenient at all, so I fixed that. With the new API, the snippet given yesterday:

/** 
 * Calculates (a / 20) * (b + 123.90) 
 */ 
public void calculateIt(BigDecimal a, BigDecimal b) { 
  ExpressionBuilder builder = ....;
  Expression expression = builder.create("(a / 20) * (b + 123.90)");
  VariableMap map = new VariableMap();
  return expression.evaluate(map.assign(a, "a").assign(b, "b"));
}

can be rewritten as:

/** 
 * Calculates (a / 20) * (b + 123.90) 
 */ 
public void calculateIt(BigDecimal a, BigDecimal b) { 
  ExpressionBuilder builder = ....;
  Expression expression = builder.create("(a / 20) * (b + 123.90)");
  Function function = new Function(expression, new String[] { "a", "b" });
  return function.evaluate(a, b);
}

Basically, Function is a convenience wrapper of Expression. The constructor accepts both the Expression, as well as an array of variable names. The order of the variable names is the order in which the Function will expect the arguments to arrive.

( Jul 22 2005, 02:58:29 AM CEST ) Permalink Comments [0]
Trackback URL: http://blogs.sun.com/wilfred/entry/big_freaking_numbers_ii
Comments:

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed