1 | /**
|
---|
2 | * Variables.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 |
|
---|
19 | package org.expeditee.simple;
|
---|
20 |
|
---|
21 | import java.util.ArrayList;
|
---|
22 |
|
---|
23 | import org.expeditee.gui.MessageBay;
|
---|
24 |
|
---|
25 | public abstract class Variables<T extends SVariable<?>> {
|
---|
26 |
|
---|
27 | protected ArrayList<T> list_;
|
---|
28 |
|
---|
29 | public Variables() {
|
---|
30 | list_ = new ArrayList<T>();
|
---|
31 | }
|
---|
32 |
|
---|
33 | public int size() {
|
---|
34 | return list_.size();
|
---|
35 | }
|
---|
36 |
|
---|
37 | public void clear() {
|
---|
38 | list_.clear();
|
---|
39 | }
|
---|
40 |
|
---|
41 | public void display() {
|
---|
42 | for (T v : list_) {
|
---|
43 | MessageBay.displayMessage(v.getName() + ": " + v.stringValue());
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public T getVariable(String name) throws VariableNotFoundException {
|
---|
48 | for (T v : list_) {
|
---|
49 | if (v.getName().equalsIgnoreCase(name)) {
|
---|
50 | return v;
|
---|
51 | }
|
---|
52 | }
|
---|
53 | throw new VariableNotFoundException(name + " is not an existing "
|
---|
54 | + getType());
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Sets the value of variable1 to value of variable2. If variable1 doesnt
|
---|
59 | * exist then it is created.
|
---|
60 | *
|
---|
61 | * @param variableToSet-
|
---|
62 | * the variable whos value will be modified.
|
---|
63 | * @param variableWithNewValue-
|
---|
64 | * the variable to get the new value from.
|
---|
65 | */
|
---|
66 | public void set(String variableToSet, String variableWithNewValue)
|
---|
67 | throws Exception {
|
---|
68 | // If the variables are the same then do nothing
|
---|
69 | if (variableToSet.equalsIgnoreCase(variableWithNewValue))
|
---|
70 | return;
|
---|
71 |
|
---|
72 | T toGetValueFrom = getVariable(variableWithNewValue);
|
---|
73 | T toSet = null;
|
---|
74 | try {
|
---|
75 | toSet = getVariable(variableToSet);
|
---|
76 | } catch (Exception e) {
|
---|
77 | // if the variable to be set doesnt exist and exception is
|
---|
78 | // thrown So it will be added
|
---|
79 | add(variableToSet, toGetValueFrom);
|
---|
80 | return;
|
---|
81 | }
|
---|
82 | //TODO:figure out how to fix the ANT build error
|
---|
83 | ((SVariable)toSet).setValue(toGetValueFrom);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public abstract void add(String name, T value) throws Exception;
|
---|
87 |
|
---|
88 | protected abstract String getType();
|
---|
89 | }
|
---|