1 | package org.expeditee.simple;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 |
|
---|
5 | import org.expeditee.gui.FrameGraphics;
|
---|
6 |
|
---|
7 | public abstract class Variables<T extends SVariable<?>> {
|
---|
8 |
|
---|
9 | protected ArrayList<T> list_;
|
---|
10 |
|
---|
11 | public Variables() {
|
---|
12 | list_ = new ArrayList<T>();
|
---|
13 | }
|
---|
14 |
|
---|
15 | public int size() {
|
---|
16 | return list_.size();
|
---|
17 | }
|
---|
18 |
|
---|
19 | public void clear() {
|
---|
20 | list_.clear();
|
---|
21 | }
|
---|
22 |
|
---|
23 | public void display() {
|
---|
24 | for (T v : list_) {
|
---|
25 | FrameGraphics.DisplayMessage(v.getName() + ": " + v.stringValue());
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | public T getVariable(String name) throws Exception {
|
---|
30 | for (T v : list_) {
|
---|
31 | if (v.getName().equalsIgnoreCase(name)) {
|
---|
32 | return v;
|
---|
33 | }
|
---|
34 | }
|
---|
35 | throw new VariableNotFoundException(name + " is not an existing "
|
---|
36 | + getType());
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Sets the value of variable1 to value of variable2. If variable1 doesnt
|
---|
41 | * exist then it is created.
|
---|
42 | *
|
---|
43 | * @param variableToSet-
|
---|
44 | * the variable whos value will be modified.
|
---|
45 | * @param variableWithNewValue-
|
---|
46 | * the variable to get the new value from.
|
---|
47 | */
|
---|
48 | public void set(String variableToSet, String variableWithNewValue)
|
---|
49 | throws Exception {
|
---|
50 | // If the variables are the same then do nothing
|
---|
51 | if (variableToSet.equalsIgnoreCase(variableWithNewValue))
|
---|
52 | return;
|
---|
53 |
|
---|
54 | T toGetValueFrom = getVariable(variableWithNewValue);
|
---|
55 | T toSet = null;
|
---|
56 | try {
|
---|
57 | toSet = getVariable(variableToSet);
|
---|
58 | } catch (Exception e) {
|
---|
59 | // if the variable to be set doesnt exist and exception is
|
---|
60 | // thrown So it will be added
|
---|
61 | add(variableToSet, toGetValueFrom);
|
---|
62 | return;
|
---|
63 | }
|
---|
64 | toSet.setValue(toGetValueFrom);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public abstract void add(String name, T value) throws Exception;
|
---|
68 |
|
---|
69 | protected abstract String getType();
|
---|
70 | }
|
---|