1 package org.opentrafficsim.base.modelproperties;
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>, 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
61 public final void setReadOnly(final boolean readOnlyValue)
62 {
63 this.readOnly = readOnlyValue;
64 }
65
66
67 @Override
68 public final int getDisplayPriority()
69 {
70 return this.displayPriority;
71 }
72
73
74 @Override
75 public abstract String htmlStateDescription();
76
77
78 @Override
79 public final Iterator<Property<?>> iterator()
80 {
81 return new PropertyIterator(this);
82 }
83
84
85 @Override
86 public final String getShortName()
87 {
88 return this.shortName;
89 }
90
91
92 @Override
93 public final String getDescription()
94 {
95 return this.description;
96 }
97
98
99 @Override
100 public final boolean isReadOnly()
101 {
102 return null != this.readOnly && this.readOnly;
103 }
104
105
106
107
108
109 @Override
110 public final String getKey()
111 {
112 return this.key;
113 }
114
115
116 @Override
117 public final Property<?> findByKey(final String propertyKey)
118 {
119 if (this.key.equals(propertyKey))
120 {
121 return this;
122 }
123 if (this instanceof CompoundProperty)
124 {
125 return ((CompoundProperty) this).getPropertyGroup().get(propertyKey);
126 }
127 if (null == getParent())
128 {
129 return null;
130 }
131 return getParent().getPropertyGroup().get(propertyKey);
132 }
133
134
135
136
137
138 protected final void setParent(final CompoundProperty newParent)
139 {
140 this.parentProperty = newParent;
141 }
142
143
144 @Override
145 public final CompoundProperty getParent()
146 {
147 return this.parentProperty;
148 }
149
150
151 @Override
152 public final String toString()
153 {
154 return this.getShortName();
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168 class PropertyIterator implements Iterator<Property<?>>, Serializable
169 {
170
171 private static final long serialVersionUID = 20150000L;
172
173
174 private int currentIndex;
175
176
177 private final ArrayList<Property<?>> list;
178
179
180
181
182
183 PropertyIterator(final Property<T> ap)
184 {
185 this.currentIndex = 0;
186 this.list = new ArrayList<Property<?>>();
187 addToList(ap);
188 }
189
190
191
192
193
194
195 private void addToList(final Property<?> cp)
196 {
197 this.list.add(cp);
198 if (cp instanceof CompoundProperty)
199 {
200 for (Property<?> ap : ((CompoundProperty) cp).getValue())
201 {
202 addToList(ap);
203 }
204 }
205 }
206
207
208 @Override
209 public boolean hasNext()
210 {
211 return this.currentIndex < this.list.size();
212 }
213
214
215 @Override
216 public Property<?> next()
217 {
218 return this.list.get(this.currentIndex++);
219 }
220
221
222 @Override
223 public void remove()
224 {
225 throw new UnsupportedOperationException();
226 }
227
228
229 @Override
230 public String toString()
231 {
232 return "PropertyIterator [currentIndex=" + this.currentIndex + ", list=" + this.list + "]";
233 }
234
235 }
236
237 }