1 package rydeen;
2
3 import java.io.Serializable;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.Map;
8
9
10
11
12
13
14
15
16 public class Arguments implements Iterable<Argument>, Serializable{
17 private static final long serialVersionUID = 529727017881875610L;
18
19 private Map<String, Argument> arguments = new HashMap<String, Argument>();
20
21
22
23
24 public Arguments(){
25 }
26
27
28
29
30
31 public Arguments(Arguments args){
32 for(Argument param: args){
33 arguments.put(param.getName(), param);
34 }
35 }
36
37
38
39
40 public Arguments(Argument[] args){
41 for(Argument param: args){
42 arguments.put(param.getName(), param);
43 }
44 }
45
46
47
48
49 @Override
50 public Iterator<Argument> iterator(){
51 return Collections.unmodifiableMap(arguments).values().iterator();
52 }
53
54
55
56
57
58
59
60
61 public void merge(Arguments args){
62 for(Argument param: args){
63 if(arguments.get(param.getName()) == null){
64 arguments.put(param.getName(), param);
65 }
66 }
67 }
68
69
70
71
72
73
74
75
76 public void mergeOverwrite(Arguments args){
77 for(Argument param: args){
78 arguments.put(param.getName(), param);
79 }
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 public boolean hasArgument(String name){
96 if(name == null){
97 throw new NullPointerException();
98 }
99 return arguments.get(name) != null;
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 public String getValue(String name, String defaultValue){
120 String value = null;
121 if(name == null){
122 throw new NullPointerException();
123 }
124 if(arguments.containsKey(name)){
125 Argument param = arguments.get(name);
126 if(param != null){
127 value = param.getValue();
128 }
129 if(value == null){
130 value = defaultValue;
131 }
132 }
133 return value;
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 public String getDescription(String name){
150 String desc = null;
151 if(name == null){
152 throw new NullPointerException();
153 }
154 if(arguments.containsKey(name)){
155 Argument param = arguments.get(name);
156 if(param != null){
157 desc = param.getDescription();
158 }
159 }
160 return desc;
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175 public String getValue(String name){
176 return getValue(name, null);
177 }
178
179
180
181
182
183
184
185
186 public void setDescription(String name, String description){
187 Argument arg = arguments.get(name);
188 if(name == null){
189 throw new NullPointerException("name is null");
190 }
191 else if(arg == null){
192 throw new NullPointerException(name + " is not contained.");
193 }
194 else{
195 arg = new ArgumentImpl(name, arg.getValue(), description);
196 }
197 arguments.put(name, arg);
198 }
199
200
201
202
203
204
205 public void putValue(Argument arg){
206 arguments.put(arg.getName(), arg);
207 }
208
209
210
211
212
213
214
215
216
217
218
219 public void putValue(String name, String value){
220 if(name == null){
221 throw new NullPointerException();
222 }
223 Argument arg = arguments.get(name);
224 if(arg == null){
225 arg = new ArgumentImpl(name, value);
226 }
227 else{
228 arg = new ArgumentImpl(name, value, arg.getDescription());
229 }
230 arguments.put(name, arg);
231 }
232
233
234
235
236
237
238
239
240
241 public void putValue(String name, String value, String description){
242 if(name == null){
243 throw new NullPointerException();
244 }
245 arguments.put(name, new ArgumentImpl(name, value, description));
246 }
247
248
249
250
251
252
253 public int getArgumentCount(){
254 return arguments.size();
255 }
256
257
258
259
260
261
262
263
264
265
266 public boolean removeArgument(String name){
267 return arguments.remove(name) != null;
268 }
269 }