KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jep > Variable


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.3.0
4       October 3 2004
5       (c) Copyright 2004, Nathan Funk and Richard Morris
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9 /* @author rich
10  * Created on 18-Nov-2003
11  */

12 package org.nfunk.jep;
13 import java.util.*;
14
15 /**
16  * Information about a variable.
17  * Each variable has a name, a value.
18  * There is a flag to indicate
19  * whether it is a constant or not (constants cannot have their value changed).
20  * There is also a flag to indicate whether the value of the
21  * variable is valid, if the variable is initialised without a value
22  * then its value is said to be invalid.
23  * <p>
24  * @author Rich Morris
25  * Created on 18-Nov-2003
26  * @version 2.3.0 beta 2 Now extends Observable so observers can track if the value has been changed.
27  */

28 public class Variable extends Observable {
29     protected String JavaDoc name;
30     private Object JavaDoc value;
31     private boolean isConstant = false;
32     private boolean validValue = false;
33 // private static final Double ZERO = new Double(0.0);
34

35     /** Constructors are protected. Variables should only
36      * be created through the associated {@link VariableFactory}
37      * which are in turned called by {@link SymbolTable}.
38      */

39     protected Variable(String JavaDoc name)
40     {
41         this.name = name;
42         this.value = null;
43         validValue = false;
44     }
45     /** Constructors are protected. Variables should only
46      * be created through the associated {@link VariableFactory}
47      * which are in turned called by {@link SymbolTable}.
48      */

49     protected Variable(String JavaDoc name,Object JavaDoc value)
50     {
51         this.name = name;
52         this.value = value;
53         validValue = (value!=null);
54     }
55     public String JavaDoc getName() {return name;}
56     //private void setName(String string) {name = string; }
57
public boolean isConstant() { return this.isConstant; }
58     public void setIsConstant(boolean b) { this.isConstant = b; }
59     public Object JavaDoc getValue() { return value; }
60     /** Is the value of this variable valid? **/
61     public boolean hasValidValue() { return validValue; }
62     /** Sets whether the value of variable is valid. **/
63     public void setValidValue(boolean val) { validValue = val; }
64
65     /**
66      * Sets the value of the variable. Constant values cannot be changed.
67      * <p>
68      * This method call java.util.Observable.notifyObservers()
69      * to indicate to anyone interested that the value has been changed.
70      * Note subclasses should override setValueRaw rather than this
71      * method so they do not need to handle the Observable methods.
72      *
73      * @return false if tried to change a constant value.
74      * @since 2.3.0 beta 2 added Observable
75      */

76     public boolean setValue(Object JavaDoc object) {
77         if(!setValueRaw(object)) return false;
78         setChanged();
79         notifyObservers();
80         return true;
81     }
82
83     /**
84      * In general subclasses should override this method rather than
85      * setValue. This is because setValue notifies any observers
86      * and then calls this method.
87      * @param object
88      * @return false if tried to change a constant value.
89      * @since 2.3.0 beta 2
90      */

91     protected boolean setValueRaw(Object JavaDoc object) {
92         if(isConstant) return false;
93         validValue = true;
94         value = object;
95         return true;
96     }
97
98     /**
99      * Returns a string with the variable name followed by it's value.
100      * For example for the variable "a" with the value 10, the following
101      * string is returned:
102      * <pre>a: 10</pre>
103      * If the variable is a constant the string " (Constant" is appended.
104      * @return A string with the variable name and value.
105      */

106     public String JavaDoc toString() {
107         if(!validValue || value == null)
108             return name + ": null";
109         else if(isConstant)
110             return name + ": " + value.toString() + " (Constant)";
111         else
112             return name + ": " + value.toString();
113     }
114 }
115
Popular Tags