1 package org.opentrafficsim.simulationengine.properties;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.Iterator;
6
7
8
9
10
11
12
13
14
15
16
17
18 public abstract class AbstractProperty<T> implements Property<T>, Iterable<AbstractProperty<T>>, Serializable
19 {
20
21 private static final long serialVersionUID = 20150000L;
22
23
24 private final String key;
25
26
27 private final int displayPriority;
28
29
30 private String shortName;
31
32
33 private String description;
34
35
36 private Boolean readOnly = null;
37
38
39 private CompoundProperty parentProperty = null;
40
41
42
43
44
45
46
47
48 public AbstractProperty(final String key, final int displayPriority, final String shortName, final String description)
49 {
50 this.key = key;
51 this.displayPriority = displayPriority;
52 this.shortName = shortName;
53 this.description = description;
54 }
55
56
57
58
59
60 protected final void setReadOnly(final boolean readOnlyValue)
61 {
62 this.readOnly = readOnlyValue;
63 }
64
65
66 @Override
67 public final int getDisplayPriority()
68 {
69 return this.displayPriority;
70 }
71
72
73 @Override
74 public abstract String htmlStateDescription();
75
76
77 @Override
78 public final Iterator<AbstractProperty<T>> iterator()
79 {
80 return new PropertyIterator(this);
81 }
82
83
84 @Override
85 public final String getShortName()
86 {
87 return this.shortName;
88 }
89
90
91 @Override
92 public final String getDescription()
93 {
94 return this.description;
95 }
96
97
98 @Override
99 public final boolean isReadOnly()
100 {
101 return null != this.readOnly && this.readOnly;
102 }
103
104
105
106
107
108 public final String getKey()
109 {
110 return this.key;
111 }
112
113
114
115
116
117
118
119 public final AbstractProperty<?> findByKey(final String propertyKey)
120 {
121 if (this.key.equals(propertyKey))
122 {
123 return this;
124 }
125 if (this instanceof CompoundProperty)
126 {
127 return ((CompoundProperty) this).getPropertyGroup().get(propertyKey);
128 }
129 if (null == getParent())
130 {
131 return null;
132 }
133 return getParent().getPropertyGroup().get(propertyKey);
134 }
135
136
137
138
139
140 protected final void setParent(final CompoundProperty newParent)
141 {
142 this.parentProperty = newParent;
143 }
144
145
146
147
148
149 protected final CompoundProperty getParent()
150 {
151 return this.parentProperty;
152 }
153
154
155 public final String toString()
156 {
157 return this.getShortName();
158 }
159
160
161
162
163
164
165
166
167
168
169
170 class PropertyIterator implements Iterator<AbstractProperty<T>>, Serializable
171 {
172
173 private static final long serialVersionUID = 20150000L;
174
175
176 private int currentIndex;
177
178
179 private final ArrayList<AbstractProperty<T>> list;
180
181
182
183
184
185 PropertyIterator(final AbstractProperty<T> ap)
186 {
187 this.currentIndex = 0;
188 this.list = new ArrayList<AbstractProperty<T>>();
189 addToList(ap);
190 }
191
192
193
194
195
196
197 @SuppressWarnings("unchecked")
198 private void addToList(final AbstractProperty<T> cp)
199 {
200 this.list.add(cp);
201 if (cp instanceof CompoundProperty)
202 {
203 for (AbstractProperty<?> ap : ((CompoundProperty) cp).getValue())
204 {
205 addToList((AbstractProperty<T>) ap);
206 }
207 }
208 }
209
210
211 @Override
212 public boolean hasNext()
213 {
214 return this.currentIndex < this.list.size();
215 }
216
217
218 @Override
219 public AbstractProperty<T> next()
220 {
221 return this.list.get(this.currentIndex++);
222 }
223
224
225 @Override
226 public void remove()
227 {
228 throw new UnsupportedOperationException();
229 }
230
231
232 @Override
233 public String toString()
234 {
235 return "PropertyIterator [currentIndex=" + this.currentIndex + ", list=" + this.list + "]";
236 }
237
238 }
239
240 }