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 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<Property<?>> 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 @Override
115 public final Property<?> findByKey(final String propertyKey)
116 {
117 if (this.key.equals(propertyKey))
118 {
119 return this;
120 }
121 if (this instanceof CompoundProperty)
122 {
123 return ((CompoundProperty) this).getPropertyGroup().get(propertyKey);
124 }
125 if (null == getParent())
126 {
127 return null;
128 }
129 return getParent().getPropertyGroup().get(propertyKey);
130 }
131
132
133
134
135
136 protected final void setParent(final CompoundProperty newParent)
137 {
138 this.parentProperty = newParent;
139 }
140
141
142 @Override
143 public final CompoundProperty getParent()
144 {
145 return this.parentProperty;
146 }
147
148
149 @Override
150 public final String toString()
151 {
152 return this.getShortName();
153 }
154
155
156
157
158
159
160
161
162
163
164
165 class PropertyIterator implements Iterator<Property<?>>, Serializable
166 {
167
168 private static final long serialVersionUID = 20150000L;
169
170
171 private int currentIndex;
172
173
174 private final ArrayList<Property<?>> list;
175
176
177
178
179
180 PropertyIterator(final Property<T> ap)
181 {
182 this.currentIndex = 0;
183 this.list = new ArrayList<Property<?>>();
184 addToList(ap);
185 }
186
187
188
189
190
191
192 private void addToList(final Property<?> cp)
193 {
194 this.list.add(cp);
195 if (cp instanceof CompoundProperty)
196 {
197 for (Property<?> ap : ((CompoundProperty) cp).getValue())
198 {
199 addToList(ap);
200 }
201 }
202 }
203
204
205 @Override
206 public boolean hasNext()
207 {
208 return this.currentIndex < this.list.size();
209 }
210
211
212 @Override
213 public Property<?> next()
214 {
215 return this.list.get(this.currentIndex++);
216 }
217
218
219 @Override
220 public void remove()
221 {
222 throw new UnsupportedOperationException();
223 }
224
225
226 @Override
227 public String toString()
228 {
229 return "PropertyIterator [currentIndex=" + this.currentIndex + ", list=" + this.list + "]";
230 }
231
232 }
233
234 }