01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.forms.expression;
18:
19: import java.math.BigDecimal;
20: import java.math.BigInteger;
21: import java.util.Collection;
22: import java.util.Iterator;
23:
24: import org.outerj.expression.AbstractExpression;
25: import org.outerj.expression.Expression;
26: import org.outerj.expression.ExpressionContext;
27: import org.outerj.expression.ExpressionException;
28:
29: /**
30: * Sum function. This function returns the sum of all of its argument, but
31: * it accepts Collections or Iterators as arguments. When it finds such an
32: * argument it iterates on all it's values, and try to sum them as well. It
33: * accepts String and any instance of Number.
34: */
35: public class SumFunction extends AbstractExpression {
36:
37: public Object evaluate(ExpressionContext context)
38: throws ExpressionException {
39: BigDecimal result = new BigDecimal("0");
40: for (int i = 0; i < arguments.size(); i++) {
41: Expression function = (Expression) arguments.get(i);
42: Object ret = function.evaluate(context);
43: if (ret instanceof Collection) {
44: ret = ((Collection) ret).iterator();
45: }
46: if (ret instanceof Iterator) {
47: Iterator iter = (Iterator) ret;
48: while (iter.hasNext()) {
49: Object p = iter.next();
50: BigDecimal db = null;
51: if (p instanceof BigDecimal) {
52: db = (BigDecimal) p;
53: } else if (p instanceof Long) {
54: db = new BigDecimal(((Long) p).longValue());
55: } else if (p instanceof Integer) {
56: db = new BigDecimal(((Integer) p).intValue());
57: } else if (p instanceof Double) {
58: db = new BigDecimal(((Double) p).doubleValue());
59: } else if (p instanceof Float) {
60: db = new BigDecimal(((Float) p).floatValue());
61: } else if (p instanceof BigInteger) {
62: db = new BigDecimal((BigInteger) p);
63: } else if (p instanceof Number) {
64: db = new BigDecimal(((Number) p).doubleValue());
65: } else if (p instanceof String) {
66: db = new BigDecimal((String) p);
67: } else {
68: throw new IllegalArgumentException(
69: "Cannot sum an argument of type "
70: + p.getClass().getName());
71: }
72: result = result.add(db);
73: }
74: } else {
75: result = result.add((BigDecimal) function
76: .evaluate(context));
77: }
78: }
79: return result;
80: }
81:
82: public Class getResultType() {
83: return BigDecimal.class;
84: }
85:
86: public String getDescription() {
87: return "Summatory";
88: }
89:
90: }
|