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