1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sourceforge.jarduino.message;
18
19 import java.util.Objects;
20
21 import net.sourceforge.jarduino.ArduinoException;
22
23
24
25
26 public class ArduinoAttribute {
27
28
29
30 public interface ArduinoAttrObject {
31
32
33
34
35
36 Object getAttrValue(ArduinoAttribute pAttr);
37 }
38
39
40
41
42 static final String REL_MARKER = "REL_";
43
44
45
46
47 static final String NODE2MSG_MARKER = ArduinoNode.MARKER + ArduinoMessage.MARKER + REL_MARKER;
48
49
50
51
52 static final String NODE2SIG_MARKER = ArduinoNode.MARKER + ArduinoSignal.MARKER + REL_MARKER;
53
54
55
56
57 private final String theName;
58
59
60
61
62 private final ArduinoAttrClass theClass;
63
64
65
66
67 private final ArduinoAttrType theType;
68
69
70
71
72 private ArduinoAttrConstraints theConstraints;
73
74
75
76
77 private Object theDefault;
78
79
80
81
82
83
84
85 ArduinoAttribute(final String pName,
86 final ArduinoAttrClass pClass,
87 final ArduinoAttrType pType) {
88 theName = pName;
89 theClass = pClass;
90 theType = pType;
91 }
92
93
94
95
96
97 public String getName() {
98 return theName;
99 }
100
101
102
103
104
105 public ArduinoAttrClass getAttrClass() {
106 return theClass;
107 }
108
109
110
111
112
113 public ArduinoAttrType getAttrType() {
114 return theType;
115 }
116
117
118
119
120
121 public ArduinoAttrConstraints getConstraints() {
122 return theConstraints;
123 }
124
125
126
127
128
129 void setConstraints(final ArduinoAttrConstraints pConstraints) {
130 theConstraints = pConstraints;
131 }
132
133
134
135
136
137 public Object getDefault() {
138 return theDefault;
139 }
140
141
142
143
144
145 void setDefault(final Object pDefault) {
146 theDefault = pDefault;
147 }
148
149 @Override
150 public boolean equals(final Object pThat) {
151
152 if (pThat == this) {
153 return true;
154 } else if (!(pThat instanceof ArduinoAttribute)) {
155 return false;
156 }
157
158
159 final ArduinoAttribute./net/sourceforge/jarduino/message/ArduinoAttribute.html#ArduinoAttribute">ArduinoAttribute myThat = (ArduinoAttribute) pThat;
160
161
162 return theName.equals(myThat.getName());
163 }
164
165 @Override
166 public int hashCode() {
167 return Objects.hashCode(theName);
168 }
169
170 @Override
171 public String toString() {
172 String myName = theName + ":" + theClass + ":" + theType;
173 if (theDefault != null) {
174 myName += "=" + theDefault;
175 }
176 return myName;
177 }
178
179
180
181
182 public enum ArduinoAttrClass {
183
184
185
186 SYSTEM,
187
188
189
190
191 NODE,
192
193
194
195
196 MESSAGE,
197
198
199
200
201 SIGNAL,
202
203
204
205
206 NODE2MSG,
207
208
209
210
211 NODE2SIGNAL;
212
213
214
215
216
217 public boolean isRelation() {
218 switch (this) {
219 case NODE2SIGNAL:
220 case NODE2MSG:
221 return true;
222 default:
223 return false;
224 }
225 }
226
227
228
229
230
231
232
233 static ArduinoAttrClass parseAttrClass(final String pAttrClass) throws ArduinoException {
234 switch (pAttrClass) {
235 case ArduinoNode.MARKER:
236 return NODE;
237 case ArduinoMessage.MARKER:
238 return MESSAGE;
239 case ArduinoSignal.MARKER:
240 return SIGNAL;
241 case NODE2MSG_MARKER:
242 return NODE2MSG;
243 case NODE2SIG_MARKER:
244 return NODE2SIGNAL;
245 default:
246 if (pAttrClass.length() == 0
247 || pAttrClass.charAt(0) != ArduinoChar.QUOTE) {
248 throw new ArduinoException("Invalid Attribute class", pAttrClass);
249 }
250 return SYSTEM;
251 }
252 }
253 }
254
255
256
257
258 public enum ArduinoAttrType {
259
260
261
262 INT,
263
264
265
266
267 FLOAT,
268
269
270
271
272 HEX,
273
274
275
276
277 STRING,
278
279
280
281
282 ENUM;
283
284
285
286
287
288
289
290 static ArduinoAttrType parseAttrType(final String pAttrType) throws ArduinoException {
291
292 for (ArduinoAttrType myType : values()) {
293 if (pAttrType.equals(myType.name())) {
294 return myType;
295 }
296 }
297 throw new ArduinoException("Invalid AttrType", pAttrType);
298 }
299 }
300 }