source: trunk/org/expeditee/simple/Primitives.java@ 4

Last change on this file since 4 was 4, checked in by davidb, 16 years ago

Starting source code to Expeditee

File size: 7.3 KB
Line 
1package org.expeditee.simple;
2
3public class Primitives extends Variables<SPrimitive<?>> {
4 private static final String[] prefixes = new String[] { SInteger.prefix,
5 SBoolean.prefix, SString.prefix, SReal.prefix, SCharacter.prefix };
6
7 public static boolean isPrimitive(String varName) {
8 for (String s : prefixes) {
9 if (varName.startsWith(s))
10 return true;
11 }
12 return false;
13 }
14
15 public static final int PREFIX_LENGTH = SInteger.prefix.length();
16
17 public Primitives() {
18 super();
19 }
20
21 protected String getType() {
22 return "primitive";
23 }
24
25 public void setValue(String variableName, SPrimitive newValue)
26 throws Exception {
27 try {
28 // if it is an existing variable change the value
29 SPrimitive v = getVariable(variableName);
30 v.setValue(newValue);
31 } catch (Exception e) {
32 // If the first variable doesnt exist then add it
33 add(variableName, newValue);
34 }
35 }
36
37 public void setValue(String variableName, String newValue) throws Exception {
38 setValue(variableName, new SString(newValue));
39 }
40
41 /**
42 * Adds a variable to the list of primitives.
43 *
44 * @param name
45 * the variable name
46 * @param value
47 * a primitive to take the new variables value from
48 * @throws Exception
49 * if the variable name is invalid
50 */
51 public void add(String name, SPrimitive value) throws Exception {
52 // figure out the type and add it...
53 SPrimitive newVar;
54 if (name.startsWith(SInteger.prefix))
55 newVar = new SInteger();
56 else if (name.startsWith(SBoolean.prefix))
57 newVar = new SBoolean();
58 else if (name.startsWith(SReal.prefix))
59 newVar = new SReal();
60 else if (name.startsWith(SString.prefix))
61 newVar = new SString();
62 else if (name.startsWith(SCharacter.prefix))
63 newVar = new SCharacter();
64 else
65 throw new IncorrectTypeException(name, "primitive");
66 newVar.setName(name);
67 newVar.setValue(value);
68 list_.add(newVar);
69 }
70
71 public String getStringValue(String name) throws Exception {
72 return getVariable(name).stringValue();
73 }
74
75 public boolean getBooleanValue(String name) throws Exception {
76 return getVariable(name).booleanValue();
77 }
78
79 public long getIntegerValue(String name) throws Exception {
80 return getVariable(name).integerValue();
81 }
82
83 public double getDoubleValue(String name) throws Exception {
84 return getVariable(name).doubleValue();
85 }
86
87 public char getCharacterValue(String name) throws Exception {
88 return getVariable(name).characterValue();
89 }
90
91 /*
92 * public String setStringValue(String name, String value) throws Exception {
93 * return getVariable(name).stringValue(); }
94 *
95 * public boolean setBooleanValue(String name, boolean value) throws
96 * Exception { return getVariable(name).booleanValue(); }
97 *
98 * public long setIntegerValue(String name, long value) throws Exception {
99 * return getVariable(name).integerValue(); }
100 *
101 * public double setDoubleValue(String name, double value) throws Exception {
102 * return getVariable(name).doubleValue(); }
103 *
104 * public double setCharacterValue(String name, char value) throws Exception {
105 * return getVariable(name).characterValue(); }
106 */
107
108 public static boolean isNumeric(String varName) {
109 return varName.startsWith(SInteger.prefix)
110 || varName.startsWith(SReal.prefix);
111 }
112
113 public boolean equalValues(String var1, String var2) throws Exception {
114 return compareValues(var1, var2) == 0;
115 }
116
117 /**
118 * Checks if the string representation of two variables is identical
119 * ignoring case.
120 *
121 * @param var1
122 * @param var2
123 * @return
124 * @throws Exception
125 */
126 public boolean equalValuesNoCase(String var1, String var2) throws Exception {
127 return getStringValue(var1).equalsIgnoreCase(getStringValue(var2));
128 }
129
130 /**
131 *
132 * @param var1
133 * @param var2
134 * @return
135 * @throws Exception
136 */
137 public int compareValues(String var1, String var2) throws Exception {
138 // If one of them is a real and one is an integer then do a numeric
139 // comparison
140 if ((var1.startsWith(SInteger.prefix) || var2
141 .startsWith(SInteger.prefix))) {
142 long v1 = getIntegerValue(var1);
143 long v2 = getIntegerValue(var2);
144 return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
145 } else if ((var1.startsWith(SReal.prefix) || var2
146 .startsWith(SReal.prefix))) {
147 double v1 = getDoubleValue(var1);
148 double v2 = getDoubleValue(var2);
149 return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
150 } else if ((var1.startsWith(SBoolean.prefix) || var2
151 .startsWith(SBoolean.prefix))) {
152 boolean v1 = getBooleanValue(var1);
153 boolean v2 = getBooleanValue(var2);
154 return v1 == v2 ? 0 : (v1 == true ? 1 : -1);
155 }
156 return getStringValue(var1).compareTo(getStringValue(var2));
157 }
158
159 public void add(String var) throws Exception {
160 setValue(var, new SReal(getVariable(var).doubleValue() + 1));
161 }
162
163 public void subtract(String var) throws Exception {
164 setValue(var, new SReal(getVariable(var).doubleValue() - 1));
165 }
166
167 public void add(String toSet, String amount) throws Exception {
168 setValue(toSet, new SReal(getVariable(toSet).doubleValue()
169 + getVariable(amount).doubleValue()));
170 }
171
172 public void subtract(String toSet, String amount) throws Exception {
173 setValue(toSet, new SReal(getVariable(toSet).doubleValue()
174 - getVariable(amount).doubleValue()));
175 }
176
177 public void multiply(String toSet, String amount) throws Exception {
178 setValue(toSet, new SReal(getVariable(toSet).doubleValue()
179 - getVariable(amount).doubleValue()));
180 }
181
182 public void divide(String toSet, String amount) throws Exception {
183 setValue(toSet, new SReal(getVariable(toSet).doubleValue()
184 - getVariable(amount).doubleValue()));
185 }
186
187 public void add(String var1, String var2, String varAnswer)
188 throws Exception {
189 setValue(varAnswer, new SReal(getVariable(var1).doubleValue()
190 + getVariable(var2).doubleValue()));
191 }
192
193 public void subtract(String var1, String var2, String varAnswer)
194 throws Exception {
195 setValue(varAnswer, new SReal(getVariable(var1).doubleValue()
196 - getVariable(var2).doubleValue()));
197 }
198
199 public void divide(String var1, String var2, String varAnswer)
200 throws Exception {
201 setValue(varAnswer, new SReal(getVariable(var1).doubleValue()
202 / getVariable(var2).doubleValue()));
203 }
204
205 public void multiply(String var1, String var2, String varAnswer)
206 throws Exception {
207 setValue(varAnswer, new SReal(getVariable(var1).doubleValue()
208 * getVariable(var2).doubleValue()));
209 }
210
211 public void modulo(String var1, String var2, String varAnswer)
212 throws Exception {
213 setValue(varAnswer, new SReal(getVariable(var1).integerValue()
214 % getVariable(var2).integerValue()));
215 }
216
217 public void power(String var1, String var2, String varAnswer)
218 throws Exception {
219 setValue(varAnswer, new SReal(Math.pow(getVariable(var1).doubleValue(),
220 getVariable(var2).doubleValue())));
221 }
222
223 public void log(String variable, String answer) throws Exception {
224 setValue(answer, new SReal(Math
225 .log(getVariable(variable).doubleValue())));
226 }
227
228 public void log10(String variable, String answer) throws Exception {
229 setValue(answer, new SReal(Math.log10(getVariable(variable)
230 .doubleValue())));
231 }
232
233 public void sqrt(String variable, String answer) throws Exception {
234 setValue(answer, new SReal(Math.sqrt(getVariable(variable)
235 .doubleValue())));
236 }
237
238 public void exp(String variable, String answer) throws Exception {
239 setValue(answer, new SReal(Math
240 .exp(getVariable(variable).doubleValue())));
241 }
242
243 public void not(String variable, String answer) throws Exception {
244 setValue(answer, new SBoolean(!getVariable(variable).booleanValue()));
245 }
246}
Note: See TracBrowser for help on using the repository browser.