1 61 62 63 package org.jaxen.function; 64 65 import java.util.Iterator ; 66 import java.util.List ; 67 68 import org.jaxen.Context; 69 import org.jaxen.Function; 70 import org.jaxen.FunctionCallException; 71 import org.jaxen.Navigator; 72 73 84 public class SumFunction implements Function 85 { 86 87 90 public SumFunction() {} 91 92 103 public Object call(Context context, 104 List args) throws FunctionCallException 105 { 106 107 if (args.size() == 1) 108 { 109 return evaluate( args.get(0), 110 context.getNavigator() ); 111 } 112 113 throw new FunctionCallException( "sum() requires one argument." ); 114 } 115 116 128 public static Double evaluate(Object obj, 129 Navigator nav) throws FunctionCallException 130 { 131 double sum = 0; 132 133 if (obj instanceof List ) 134 { 135 Iterator nodeIter = ((List )obj).iterator(); 136 while ( nodeIter.hasNext() ) 137 { 138 double term = NumberFunction.evaluate( nodeIter.next(), 139 nav ).doubleValue(); 140 sum += term; 141 } 142 } 143 else 144 { 145 throw new FunctionCallException("The argument to the sum function must be a node-set"); 146 } 147 148 return new Double (sum); 149 } 150 151 } 152 | Popular Tags |