1 package edu.psu.geovista.app.spreadsheet.functions;
2
3 import edu.psu.geovista.app.spreadsheet.formula.Node;
4 import edu.psu.geovista.app.spreadsheet.formula.Formula;
5 import edu.psu.geovista.app.spreadsheet.formula.Cell;
6 import edu.psu.geovista.app.spreadsheet.exception.ParserException;
7 import edu.psu.geovista.app.spreadsheet.exception.NoReferenceException;
8
9 import java.util.LinkedList;
10 import java.util.Iterator;
11 import java.util.ArrayList;
12
13 import edu.psu.geovista.app.spreadsheet.util.Debug;
14
15 /*
16 * Description:
17 * Date: Mar 28, 2003
18 * Time: 9:33:26 AM
19 * @author Jin Chen
20 */
21
22 public class FunctionCustomize extends Function {
23 public Number evaluate( Node node) throws ParserException,NoReferenceException {
24 ArrayList params=new ArrayList(); //the parameters to pass to customized function
25 LinkedList exps = node.getParams();//
26 Debug.showLinkedList(exps,"Show params");
27 //Node exp=(Node)exps.getFirst() ;// expresion Node(type=Node.EXP). e.g. a1:a2
28 Iterator iter = exps.iterator();
29 while(iter.hasNext()){
30 Node exp= (Node)iter.next();
31 Debug.showNode(exp, "FunctionCustomize show exp");
32 Number p=this.getOwner().evaluate(exp.getExp() ) ;
33 params.add(p);
34
35 /*float p=Formula.evaluateFun(exp.getExp() ).floatValue();
36 Debug.println("param:"+p); */
37 /***
38 LinkedList param=exp.getParams() ;
39 Debug.println("");
40 Iterator it=param.iterator();
41 while(it.hasNext()){
42 Node p= (Node)it.next(); //type1 Node
43 Cell cell=p.getReference() ;//
44 if(cell.isFormula()){
45 cell.evaluate() ;
46 }
47 if (! (cell.getValue() instanceof Number)){
48 //If not a Number, the error msg is in cell.getValue()
49 throw new ParserException(cell.getValue() );
50 }
51 Debug.println("show param:"+cell.getValue() );
52 params.add(cell.getValue());
53 } */
54 }
55
56 Object o=this.evaluate(params.toArray()) ;
57
58 if (!(o instanceof Number)){
59 throw new ParserException("#NUM?");
60 }
61
62 return (Number)o;
63 }
64
65 private Object evaluate(Object[] param) {
66 float sum=0;
67 for (int i=0;i<param.length ;i++){
68 sum=sum+((Number)param[i]).floatValue() ;
69 }
70 return new Float(sum);
71 }
72
73 public String getUsage() {
74 return "MYFUN(value1,value2,...)";
75 }
76
77 public String getDescription() {
78 return "Returns the average (arithmetric mean) of its arguments.";
79 }
80 }
81
This page was automatically generated by Maven