01: /*
02: * $Id: CalculateImpl.java,v 1.4 2007/03/12 10:46:16 agoubard Exp $
03: */
04: package com.mycompany.toolbox.api;
05:
06: import java.math.BigDecimal;
07:
08: import groovy.lang.Binding;
09: import groovy.lang.GroovyShell;
10:
11: import org.xins.common.BeanUtils;
12:
13: /**
14: * Implementation of the <code>Calculate</code> function.
15: *
16: * <p>Description: Make a mathematical calculation.
17: *
18: * @version $Revision: 1.4 $ $Date: 2007/03/12 10:46:16 $
19: * @author TODO
20: */
21: public final class CalculateImpl extends Calculate {
22:
23: /**
24: * Constructs a new <code>CalculateImpl</code> instance.
25: *
26: * @param api
27: * the API to which this function belongs, guaranteed to be not
28: * <code>null</code>.
29: */
30: public CalculateImpl(APIImpl api) {
31: super (api);
32: }
33:
34: /**
35: * Calls this function. If the function fails, it may throw any kind of
36: * exception. All exceptions will be handled by the caller.
37: *
38: * @param request
39: * the request, never <code>null</code>.
40: *
41: * @return
42: * the result of the function call, should never be <code>null</code>.
43: *
44: * @throws Throwable
45: * if anything went wrong.
46: */
47: public Result call(Request request) throws Throwable {
48: SuccessfulResult result = new SuccessfulResult();
49: GroovyShell shell = new GroovyShell();
50:
51: Object value = shell.evaluate(request.getCalculation());
52: if (value instanceof Integer) {
53: result.setResult(Double.parseDouble(value.toString()));
54: } else if (value instanceof BigDecimal) {
55: result.setResult(((BigDecimal) value).doubleValue());
56: }
57: return result;
58: }
59: }
|