1 package cz.cuni.amis.pogamut.sposh.elements;
2
3 import cz.cuni.amis.pogamut.sposh.executor.IAction;
4 import java.util.AbstractList;
5 import java.util.ArrayList;
6 import java.util.List;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class Arguments extends AbstractList<Arguments.Argument> {
31
32 public static Arguments EMPTY = new Arguments();
33
34 public static abstract class Argument {
35
36 final private String parameterName;
37
38
39
40
41
42
43 protected Argument(String parameterName) {
44 this.parameterName = parameterName;
45 }
46
47
48
49
50
51
52 public abstract Object getValue();
53
54
55
56
57
58
59
60 public abstract String getParameterVariable();
61
62 public String getParameterName() {
63 return parameterName;
64 }
65
66 @Override
67 public abstract String toString();
68
69 @Override
70 protected abstract Argument clone();
71
72
73
74
75
76
77
78
79 public static Argument createValueArgument(String argumentName, Object value) {
80 return new ValueArgument(argumentName, value);
81 }
82
83
84
85
86
87
88
89
90
91
92 public static Argument createVariableArgument(String argumentName, String variableName) {
93 return new VariableArgument(argumentName, variableName);
94 }
95 }
96
97
98
99
100 protected static final class VariableArgument extends Arguments.Argument {
101
102 protected final String variableName;
103
104 protected VariableArgument(int index, String variableName) {
105 super(Integer.toString(index));
106 this.variableName = variableName;
107 }
108
109
110
111
112
113
114
115
116 protected VariableArgument(String parameterName, String variableName) {
117 super(parameterName);
118 this.variableName = variableName;
119 }
120
121 @Override
122 public String getValue() {
123 throw new UnsupportedOperationException("Not yet implemented");
124 }
125
126 @Override
127 public String getParameterVariable() {
128 return variableName;
129 }
130
131 @Override
132 public String toString() {
133 if (getParameterName().startsWith("$")) {
134 return getParameterName() + "=" + variableName;
135 }
136 return variableName;
137 }
138
139 @Override
140 protected VariableArgument clone() {
141 return new VariableArgument(getParameterName(), variableName);
142 }
143 }
144
145
146
147
148 protected static final class ValueArgument extends Arguments.Argument {
149
150 private final Object value;
151
152
153
154
155
156
157
158
159
160
161 protected ValueArgument(int sequenceNumber, Object value) {
162 super(Integer.toString(sequenceNumber));
163 this.value = value;
164 }
165
166 protected ValueArgument(String parameterName, Object value) {
167 super(parameterName);
168 this.value = value;
169 }
170
171 @Override
172 public Object getValue() {
173 return value;
174 }
175
176 @Override
177 public String getParameterVariable() {
178 return null;
179 }
180
181 @Override
182 public String toString() {
183 if (getParameterName().startsWith("$")) {
184 return getParameterName() + "=" + Result.toLap(value);
185 }
186 return Result.toLap(value);
187 }
188
189 @Override
190 protected ValueArgument clone() {
191 return new ValueArgument(getParameterName(), value);
192 }
193 }
194 private List<Arguments.Argument> parameters = new ArrayList<Argument>();
195
196
197
198
199
200 public Arguments() {
201 }
202
203
204
205
206
207
208
209 protected Arguments(Arguments parameters) {
210 for (int i = 0; i < parameters.size(); ++i) {
211 Argument parameter = parameters.get(i);
212 Argument clone = parameter.clone();
213 this.parameters.add(clone);
214 }
215 }
216
217 @Override
218 public synchronized Arguments.Argument get(int index) {
219 return parameters.get(index);
220 }
221
222 @Override
223 public synchronized int size() {
224 return parameters.size();
225 }
226
227 @Override
228 public void add(int index, Argument element) {
229 parameters.add(index, element);
230 }
231
232 public synchronized boolean addFormal(Arguments.Argument element, FormalParameters formalParams) {
233
234 String parameterVariable = element.getParameterVariable();
235 if (parameterVariable != null) {
236 if (!formalParams.containsVariable(parameterVariable)) {
237 throw new IllegalArgumentException("Variable \"" + parameterVariable + "\" is not defined in formal parameters (" + formalParams.toString() + ").");
238 }
239 }
240
241
242 for (int i = 0; i < parameters.size(); ++i) {
243 String parameterName = parameters.get(i).getParameterName();
244
245 if (parameterName != null && parameterName.equals(element.getParameterName())) {
246 throw new IllegalArgumentException("Named parameter \"" + element.getParameterName() + "\" has already been defined.");
247 }
248 }
249 return parameters.add(element);
250 }
251
252
253
254
255
256
257
258
259 @Override
260 public String toString() {
261 StringBuilder sb = new StringBuilder();
262 for (int argumentIndex = 0; argumentIndex < parameters.size(); ++argumentIndex) {
263 if (argumentIndex != 0) {
264 sb.append(',');
265 }
266 Argument argument = parameters.get(argumentIndex);
267 sb.append(argument.toString());
268 }
269 return sb.toString();
270 }
271 }