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.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Objects;
24
25 import net.sourceforge.jarduino.ArduinoException;
26 import net.sourceforge.jarduino.message.ArduinoAttribute.ArduinoAttrObject;
27 import net.sourceforge.jarduino.message.ArduinoParser.ArduinoParserException;
28
29
30
31
32 public final class ArduinoSystem
33 implements ArduinoNamedObject, ArduinoAttrObject {
34
35
36
37 private final String theName;
38
39
40
41
42 private final String theCName;
43
44
45
46
47 private final List<ArduinoNode> theNodes;
48
49
50
51
52 private final ArduinoAttributes theCoreAttributes;
53
54
55
56
57 private final Map<ArduinoAttribute, Object> theAttributes;
58
59
60
61
62 private final ArduinoComments theComments;
63
64
65
66
67
68 private ArduinoSystem(final String pName) {
69
70 theName = pName;
71
72
73 theCName = theName.replace(ArduinoChar.BLANK, ArduinoChar.UNDERSCORE)
74 .replace(ArduinoChar.DEC, ArduinoChar.UNDERSCORE);
75
76
77 theNodes = new ArrayList<>();
78 theAttributes = new HashMap<>();
79 theCoreAttributes = new ArduinoAttributes();
80 theComments = new ArduinoComments();
81 }
82
83
84
85
86
87 private void parseNodes(final String pNodes) {
88
89 String myNodes = pNodes;
90 while (myNodes.length() > 0) {
91 final String myNode = ArduinoParser.nextToken(myNodes);
92 myNodes = ArduinoParser.stripToken(myNodes, myNode);
93 theNodes.add(new ArduinoNode(this, myNode));
94 }
95 }
96
97 @Override
98 public String getName() {
99 return theName;
100 }
101
102
103
104
105
106 public String getCName() {
107 return theCName;
108 }
109
110
111
112
113
114 public List<ArduinoNode> getNodes() {
115 return theNodes;
116 }
117
118
119
120
121
122 public List<ArduinoMessage> getMessages() {
123
124 final List<ArduinoMessage> myList = new ArrayList<>();
125
126
127 for (ArduinoNode myNode : theNodes) {
128
129 myList.addAll(myNode.getMessages());
130 }
131
132
133 return myList;
134 }
135
136
137
138
139
140 public List<ArduinoAttribute> getAttributes() {
141
142 return theCoreAttributes.getAttributes();
143 }
144
145
146
147
148
149
150
151 void setAttrValue(final ArduinoAttribute pAttr,
152 final Object pValue) throws ArduinoParserException {
153
154 if (theAttributes.containsKey(pAttr)) {
155 throw new ArduinoParserException("Duplicate Attribute", pAttr.getName());
156 }
157
158
159 theAttributes.put(pAttr, pValue);
160 }
161
162 @Override
163 public Object getAttrValue(final ArduinoAttribute pAttr) {
164 final Object myValue = theAttributes.get(pAttr);
165 return myValue == null ? pAttr.getDefault() : myValue;
166 }
167
168
169
170
171
172
173
174 public ArduinoNode findNodeByName(final String pName) throws ArduinoParserException {
175
176 for (ArduinoNode myNode : theNodes) {
177 if (pName.equals(myNode.getName())) {
178 return myNode;
179 }
180 }
181
182
183 if (ArduinoNode.NULL_NODE.equals(pName)) {
184
185 final ArduinoNodeArduinoNode.html#ArduinoNode">ArduinoNode myNode = new ArduinoNode(this, pName);
186 theNodes.add(myNode);
187 return myNode;
188 }
189
190
191 throw new ArduinoParserException("Unknown node", pName);
192 }
193
194
195
196
197
198
199
200 ArduinoMessage findMessageById(final String pId) throws ArduinoParserException {
201
202 for (ArduinoNode myNode : theNodes) {
203 final ArduinoMessage myMessage = myNode.findMessageById(pId);
204 if (myMessage != null) {
205 return myMessage;
206 }
207 }
208
209
210 throw new ArduinoParserException("Unknown id: ", pId);
211 }
212
213
214
215
216
217
218
219
220 ArduinoSignal findSignalByIdAndName(final String pId,
221 final String pName) throws ArduinoParserException {
222
223 final ArduinoMessage myMessage = findMessageById(pId);
224
225
226 return myMessage.findSignalByName(pName);
227 }
228
229
230
231
232
233
234
235 ArduinoAttribute findAttributeByName(final String pName) throws ArduinoParserException {
236
237 final ArduinoAttribute myAttr = theCoreAttributes.getAttributeForName(pName);
238 if (myAttr == null) {
239 throw new ArduinoParserException("Unknown attribute: ", pName);
240 }
241 return myAttr;
242 }
243
244
245
246
247
248
249 public String getCommentForObject(final ArduinoNamedObject pObject) {
250 return theComments.getCommentForObject(pObject);
251 }
252
253
254
255
256
257
258 void storeCommentForObject(final ArduinoNamedObject pObject,
259 final String pComment) {
260 theComments.storeCommentForObject(pObject, pComment);
261 }
262
263
264
265
266
267
268 void storeAttribute(final ArduinoAttribute pAttr) throws ArduinoException {
269 theCoreAttributes.storeAttribute(pAttr);
270 }
271
272
273
274
275
276
277
278
279 static ArduinoSystem parseSystem(final String pName,
280 final String pSystemDef) throws ArduinoException {
281
282 final String myMarker = ArduinoParser.nextToken(pSystemDef);
283 final String myNodes = ArduinoParser.stripToken(pSystemDef, myMarker);
284 if (!myMarker.equals(ArduinoNode.MARKER + ArduinoChar.COLON)) {
285 throw new ArduinoException("Invalid marker", pSystemDef);
286 }
287
288
289 final ArduinoSysteminoSystem.html#ArduinoSystem">ArduinoSystem mySystem = new ArduinoSystem(pName);
290
291
292 mySystem.parseNodes(myNodes);
293
294
295 return mySystem;
296 }
297
298 @Override
299 public boolean equals(final Object pThat) {
300
301 if (pThat == this) {
302 return true;
303 } else if (!(pThat instanceof ArduinoSystem)) {
304 return false;
305 }
306
307
308 final ArduinoSystem./../net/sourceforge/jarduino/message/ArduinoSystem.html#ArduinoSystem">ArduinoSystem myThat = (ArduinoSystem) pThat;
309
310
311 return theName.equals(myThat.getName());
312 }
313
314 @Override
315 public int hashCode() {
316 return Objects.hashCode(theName);
317 }
318
319 @Override
320 public String toString() {
321 return theName;
322 }
323 }