KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > xjep > function > MaxArg


1 /* @author rich
2  * Created on 10-Sep-2004
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.xjep.function;
9
10 import java.util.Stack JavaDoc;
11
12 import org.nfunk.jep.*;
13 import org.nfunk.jep.function.*;
14
15 /**
16  * @author Rich Morris
17  * Created on 10-Sep-2004
18  */

19 public class MaxArg extends SumType {
20
21     static Comparative comp = new Comparative(Comparative.LE);
22
23     public MaxArg() {
24         super("MaxArg");
25     }
26
27     public Object JavaDoc evaluate(Object JavaDoc[] elements) throws ParseException {
28         throw new ParseException("MaxArg: call to evaluate(Object[] elements) should not have happened.");
29     }
30
31     public Object JavaDoc evaluate(
32         Node node,
33         Variable var,
34         double min,
35         double max,
36         double inc,
37         Object JavaDoc data,
38         ParserVisitor pv,
39         Stack JavaDoc stack)
40         throws ParseException {
41
42             int i=0;
43             double val;
44             Object JavaDoc maxVal=null;
45             Object JavaDoc maxArg=null;
46             for(i=0,val=min;val<=max;++i,val=min+i*inc)
47             {
48                 Object JavaDoc curArg = new Double JavaDoc(val);
49                 var.setValue(curArg);
50                 
51                 node.jjtGetChild(0).jjtAccept(pv,data);
52                 checkStack(stack); // check the stack
53
Object JavaDoc res = stack.pop();
54                 if(i==0)
55                 {
56                     maxVal = res;
57                     maxArg = curArg;
58                 }
59                 else if(comp.gt(res,maxVal))
60                 {
61                     maxVal = res;
62                     maxArg = curArg;
63                 }
64             }
65             stack.push(maxArg);
66             return maxArg;
67     }
68
69 }
70
Popular Tags