source: trunk/src/org/expeditee/simple/Primitives.java@ 1480

Last change on this file since 1480 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 9.0 KB
Line 
1/**
2 * Primitives.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.simple;
20
21import org.expeditee.math.ExpediteeJEP;
22
23public class Primitives extends Variables<SPrimitive<?>> {
24 private static final String[] prefixes = new String[] { SInteger.prefix,
25 SBoolean.prefix, SString.prefix, SReal.prefix, SCharacter.prefix };
26
27 public static boolean isPrimitive(String varName) {
28 for (String s : prefixes) {
29 if (varName.startsWith(s))
30 return true;
31 }
32 return false;
33 }
34
35 public static final int PREFIX_LENGTH = SInteger.prefix.length();
36
37 public Primitives() {
38 super();
39 }
40
41 protected String getType() {
42 return "primitive";
43 }
44
45 public void setValue(String variableName, SPrimitive newValue)
46 throws IncorrectTypeException {
47 try {
48 // if it is an existing variable change the value
49 SPrimitive v = getVariable(variableName);
50 v.setValue(newValue);
51 } catch (Exception e) {
52 try{
53 add(variableName, newValue);
54 }catch(IncorrectTypeException ite){
55 throw ite;
56 }catch(Exception ex){
57 throw new IncorrectTypeException(ex.getClass().getSimpleName());
58 //DO NOTHING... THIS SHOULD NOT HAPPEN
59 //But the ANT builder is complaining
60 //ex.printStackTrace();
61 }
62 }
63 }
64
65 public void setValue(String variableName, String newValue) throws Exception {
66 setValue(variableName, new SString(newValue));
67 }
68
69 /**
70 * Adds a variable to the list of primitives.
71 *
72 * @param name
73 * the variable name
74 * @param value
75 * a primitive to take the new variables value from
76 * @throws Exception
77 * if the variable name is invalid
78 */
79 public void add(String name, SPrimitive value)
80 throws IncorrectTypeException {
81 // figure out the type and add it...
82 SPrimitive newVar;
83 if (name.startsWith(SInteger.prefix))
84 newVar = new SInteger();
85 else if (name.startsWith(SBoolean.prefix))
86 newVar = new SBoolean();
87 else if (name.startsWith(SReal.prefix))
88 newVar = new SReal();
89 else if (name.startsWith(SString.prefix))
90 newVar = new SString();
91 else if (name.startsWith(SCharacter.prefix))
92 newVar = new SCharacter();
93 else
94 throw new IncorrectTypeException(name, "primitive");
95 newVar.setName(name);
96 newVar.setValue(value);
97 list_.add(newVar);
98 }
99
100 public String getStringValue(String name) throws Exception {
101 return getVariable(name).stringValue();
102 }
103
104 public boolean getBooleanValue(String name) throws Exception {
105 return getVariable(name).booleanValue();
106 }
107
108 public long getIntegerValue(String name) throws Exception {
109 return getVariable(name).integerValue();
110 }
111
112 public double getDoubleValue(String name) throws Exception {
113 return getVariable(name).doubleValue();
114 }
115
116 public char getCharacterValue(String name) throws Exception {
117 return getVariable(name).characterValue();
118 }
119
120 /*
121 * public String setStringValue(String name, String value) throws Exception {
122 * return getVariable(name).stringValue(); }
123 *
124 * public boolean setBooleanValue(String name, boolean value) throws
125 * Exception { return getVariable(name).booleanValue(); }
126 *
127 * public long setIntegerValue(String name, long value) throws Exception {
128 * return getVariable(name).integerValue(); }
129 *
130 * public double setDoubleValue(String name, double value) throws Exception {
131 * return getVariable(name).doubleValue(); }
132 *
133 * public double setCharacterValue(String name, char value) throws Exception {
134 * return getVariable(name).characterValue(); }
135 */
136
137 public static boolean isNumeric(String varName) {
138 return varName.startsWith(SInteger.prefix)
139 || varName.startsWith(SReal.prefix);
140 }
141
142 public boolean equalValues(String var1, String var2) throws Exception {
143 return compareValues(var1, var2) == 0;
144 }
145
146 /**
147 * Checks if the string representation of two variables is identical
148 * ignoring case.
149 *
150 * @param var1
151 * @param var2
152 * @return
153 * @throws Exception
154 */
155 public boolean equalValuesNoCase(String var1, String var2) throws Exception {
156 return getStringValue(var1).equalsIgnoreCase(getStringValue(var2));
157 }
158
159 /**
160 *
161 * @param var1
162 * @param var2
163 * @return
164 * @throws Exception
165 */
166 public int compareValues(String var1, String var2) throws Exception {
167 // If one of them is a real and one is an integer then do a numeric
168 // comparison
169 if ((var1.startsWith(SReal.prefix) || var2.startsWith(SReal.prefix))) {
170 double v1 = getDoubleValue(var1);
171 double v2 = getDoubleValue(var2);
172 return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
173 } else if ((var1.startsWith(SInteger.prefix) || var2
174 .startsWith(SInteger.prefix))) {
175 long v1 = getIntegerValue(var1);
176 long v2 = getIntegerValue(var2);
177 return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
178 } else if ((var1.startsWith(SBoolean.prefix) || var2
179 .startsWith(SBoolean.prefix))) {
180 boolean v1 = getBooleanValue(var1);
181 boolean v2 = getBooleanValue(var2);
182 return v1 == v2 ? 0 : (v1 == true ? 1 : -1);
183 }
184 return getStringValue(var1).compareTo(getStringValue(var2));
185 }
186
187 /**
188 * Increments a variable.
189 *
190 * @param var
191 * the name of the variable to increment
192 * @throws Exception
193 */
194 public void add(String var) throws Exception {
195 setValue(var, new SReal(getVariable(var).doubleValue() + 1));
196 }
197
198 /**
199 * Decrements a variable.
200 *
201 * @param var
202 * the name of the variable to decrement
203 * @throws Exception
204 */
205 public void subtract(String var) throws Exception {
206 setValue(var, new SReal(getVariable(var).doubleValue() - 1));
207 }
208
209 /**
210 * Adds two variables to gether returning the value in the first.
211 *
212 * @param toSet
213 * @param amount
214 * @throws Exception
215 */
216 public void add(String toSet, String amount) throws Exception {
217 setValue(toSet, new SReal(getVariable(toSet).doubleValue()
218 + getVariable(amount).doubleValue()));
219 }
220
221 public void subtract(String toSet, String amount) throws Exception {
222 setValue(toSet, new SReal(getVariable(toSet).doubleValue()
223 - getVariable(amount).doubleValue()));
224 }
225
226 public void multiply(String toSet, String amount) throws Exception {
227 setValue(toSet, new SReal(getVariable(toSet).doubleValue()
228 - getVariable(amount).doubleValue()));
229 }
230
231 public void divide(String toSet, String amount) throws Exception {
232 setValue(toSet, new SReal(getVariable(toSet).doubleValue()
233 - getVariable(amount).doubleValue()));
234 }
235
236 public void add(String var1, String var2, String varAnswer)
237 throws Exception {
238 setValue(varAnswer, new SReal(getVariable(var1).doubleValue()
239 + getVariable(var2).doubleValue()));
240 }
241
242 public void subtract(String var1, String var2, String varAnswer)
243 throws Exception {
244 setValue(varAnswer, new SReal(getVariable(var1).doubleValue()
245 - getVariable(var2).doubleValue()));
246 }
247
248 public void divide(String var1, String var2, String varAnswer)
249 throws Exception {
250 setValue(varAnswer, new SReal(getVariable(var1).doubleValue()
251 / getVariable(var2).doubleValue()));
252 }
253
254 public void multiply(String var1, String var2, String varAnswer)
255 throws Exception {
256 setValue(varAnswer, new SReal(getVariable(var1).doubleValue()
257 * getVariable(var2).doubleValue()));
258 }
259
260 public void modulo(String var1, String var2, String varAnswer)
261 throws Exception {
262 setValue(varAnswer, new SReal(getVariable(var1).integerValue()
263 % getVariable(var2).integerValue()));
264 }
265
266 public void power(String var1, String var2, String varAnswer)
267 throws Exception {
268 setValue(varAnswer, new SReal(Math.pow(getVariable(var1).doubleValue(),
269 getVariable(var2).doubleValue())));
270 }
271
272 public void log(String variable, String answer) throws Exception {
273 setValue(answer, new SReal(Math
274 .log(getVariable(variable).doubleValue())));
275 }
276
277 public void log10(String variable, String answer) throws Exception {
278 setValue(answer, new SReal(Math.log10(getVariable(variable)
279 .doubleValue())));
280 }
281
282 public void sqrt(String variable, String answer) throws Exception {
283 setValue(answer, new SReal(Math.sqrt(getVariable(variable)
284 .doubleValue())));
285 }
286
287 public void exp(String variable, String answer) throws Exception {
288 setValue(answer, new SReal(Math
289 .exp(getVariable(variable).doubleValue())));
290 }
291
292 public void not(String variable, String answer) throws Exception {
293 setValue(answer, new SBoolean(!getVariable(variable).booleanValue()));
294 }
295
296 public void addToParser(ExpediteeJEP myParser) {
297 for (SPrimitive var : list_) {
298 try {
299 Double value = var.doubleValue();
300 myParser.addVariable(var.getName().substring(SReal.prefix.length()), value);
301 } catch (Exception e) {
302 }
303 }
304 }
305}
Note: See TracBrowser for help on using the repository browser.