KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > xjep > XVariable


1 /* @author rich
2  * Created on 28-Feb-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;
9
10 import org.nfunk.jep.*;
11
12 /**
13  * Variables which have their equations stored.
14  *
15  * @author Rich Morris
16  * Created on 28-Feb-2004
17  */

18 public class XVariable extends Variable {
19     private Node equation=null;
20
21     public XVariable(String JavaDoc name) {
22         super(name);
23     }
24
25     public XVariable(String JavaDoc name, Object JavaDoc value) {
26         super(name, value);
27     }
28
29     /** Does this variable has an associated equation? **/
30     public boolean hasEquation() { return equation != null; }
31     /** sets the equation */
32     public void setEquation(Node eqn)
33     {
34         equation = eqn;
35         this.setValidValue(false);
36     }
37     /** get the equation */
38     public Node getEquation() { return equation; }
39     
40     /**
41      * Calculates the value for the variables equation and returns that value.
42      *
43      */

44     public Object JavaDoc calcValue(XJep jep) throws Exception JavaDoc
45     {
46         if(equation == null ) return getValue();
47         Object JavaDoc val = jep.evaluate(equation);
48         setValue(val);
49         return val;
50     }
51
52     /** Returns a string rep of variable with its equation and value. */
53     public String JavaDoc toString(PrintVisitor pv)
54     {
55         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(name);
56         sb.append(": val "+getValue() );
57         if(!hasValidValue()) sb.append("(invalid)");
58         sb.append(" ");
59         if(getEquation()!=null) sb.append(pv.toString(getEquation()));
60         else sb.append("no equation");
61         return sb.toString();
62     }
63 }
64
Popular Tags