1 package org.opentrafficsim.simulationengine.properties;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.Iterator;
7
8
9
10
11
12
13
14
15
16
17
18 public class CompoundProperty extends AbstractProperty<ArrayList<AbstractProperty<?>>>
19 {
20
21 private ArrayList<AbstractProperty<?>> value;
22
23
24 private String shortName;
25
26
27 private String description;
28
29
30 private final Boolean readOnly;
31
32
33
34
35
36
37
38
39
40 public CompoundProperty(final String shortName, final String description,
41 final ArrayList<AbstractProperty<?>> initialValue, final boolean readOnly, final int displayPriority)
42 {
43 super(displayPriority);
44 this.shortName = shortName;
45 this.description = description;
46 this.value = null == initialValue ? new ArrayList<AbstractProperty<?>>() : initialValue;
47 this.readOnly = readOnly;
48 }
49
50
51 @Override
52 public final ArrayList<AbstractProperty<?>> getValue()
53 {
54 return new ArrayList<AbstractProperty<?>>(this.value);
55 }
56
57
58 @Override
59 public final String getShortName()
60 {
61 return this.shortName;
62 }
63
64
65 @Override
66 public final String getDescription()
67 {
68 return this.description;
69 }
70
71
72 @Override
73 public final void setValue(final ArrayList<AbstractProperty<?>> newValue) throws PropertyException
74 {
75 if (this.readOnly)
76 {
77 throw new PropertyException("Cannot modify a read-only CompoundProperty");
78 }
79 this.value = newValue;
80 }
81
82
83 @Override
84 public final boolean isReadOnly()
85 {
86 return this.readOnly;
87 }
88
89
90
91
92
93
94
95
96 public final AbstractProperty<?> findByShortName(final String name)
97 {
98
99 Iterator<AbstractProperty<ArrayList<AbstractProperty<?>>>> i = this.iterator();
100 while (i.hasNext())
101 {
102 AbstractProperty<?> ap = i.next();
103
104 if (ap.getShortName().equals(name))
105 {
106 return ap;
107 }
108 }
109 return null;
110 }
111
112
113
114
115
116
117
118 public final void add(final int index, final AbstractProperty<?> ap) throws PropertyException
119 {
120 if (this.readOnly)
121 {
122 throw new PropertyException("Cannot modify a read-only CompoundProperty");
123 }
124 if (index < 0 || index > this.value.size())
125 {
126 throw new PropertyException("index is out of range");
127 }
128 this.value.add(index, ap);
129 }
130
131
132
133
134
135
136 public final void add(final AbstractProperty<?> ap) throws PropertyException
137 {
138 add(this.value.size(), ap);
139 }
140
141
142
143
144
145
146 public final void remove(final int index) throws PropertyException
147 {
148 if (this.readOnly)
149 {
150 throw new PropertyException("Cannot modify a read-only CompoundProperty");
151 }
152 if (index < 0 || index >= this.value.size())
153 {
154 throw new PropertyException("index is out of range");
155 }
156 this.value.remove(index);
157 }
158
159
160
161
162
163 public final int size()
164 {
165 return this.value.size();
166 }
167
168
169
170
171
172
173
174 public final AbstractProperty<?> get(final int index) throws PropertyException
175 {
176 if (index < 0 || index >= this.value.size())
177 {
178 throw new PropertyException("index is out of range");
179 }
180 return this.value.get(index);
181 }
182
183
184
185
186
187 public final ArrayList<AbstractProperty<?>> displayOrderedValue()
188 {
189 ArrayList<AbstractProperty<?>> result = new ArrayList<AbstractProperty<?>>(this.value);
190 final ArrayList<AbstractProperty<?>> list = this.value;
191 Collections.sort(result, new Comparator<AbstractProperty<?>>()
192 {
193
194 @Override
195 public int compare(final AbstractProperty<?> arg0, final AbstractProperty<?> arg1)
196 {
197 int dp0 = arg0.getDisplayPriority();
198 int dp1 = arg1.getDisplayPriority();
199 if (dp0 < dp1)
200 {
201 return -1;
202 }
203 else if (dp0 > dp1)
204 {
205 return 1;
206 }
207 int i0 = list.indexOf(arg0);
208 int i1 = list.indexOf(arg1);
209 if (i0 < i1)
210 {
211 return -1;
212 }
213 else if (i0 > i1)
214 {
215 return 1;
216 }
217 return 0;
218 }
219
220 });
221
222
223
224
225
226
227
228
229 return result;
230 }
231
232
233 @Override
234 public final String htmlStateDescription()
235 {
236 StringBuilder result = new StringBuilder();
237 result.append("<table border=\"1\">");
238 result.append("<tr><th align=\"left\">" + getShortName() + "</th></tr>\n");
239 for (AbstractProperty<?> ap : displayOrderedValue())
240 {
241 result.append("<tr><td> " + ap.htmlStateDescription() + "</td></tr>\n");
242 }
243 result.append("</table>\n");
244 return result.toString();
245 }
246
247
248
249
250
251
252
253 public final void remove(final AbstractProperty<?> removeMe) throws PropertyException
254 {
255 if (!this.value.remove(removeMe))
256 {
257 throw new PropertyException("Cannot remove property " + removeMe);
258 }
259 }
260
261
262 @Override
263 public final AbstractProperty<ArrayList<AbstractProperty<?>>> deepCopy()
264 {
265 ArrayList<AbstractProperty<?>> copyOfValue = new ArrayList<AbstractProperty<?>>();
266 for (AbstractProperty<?> ap : this.value)
267 {
268 copyOfValue.add(ap.deepCopy());
269 }
270 return new CompoundProperty(this.shortName, this.description, copyOfValue, this.readOnly, getDisplayPriority());
271 }
272
273 }