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.HashMap;
20 import java.util.Map;
21
22
23
24
25 public class ArduinoMsgFilter {
26
27
28
29 private static final Integer ZERO = 0;
30
31
32
33
34 public static final int PARSEMSG = 1;
35
36
37
38
39 public static final int BUILDMSG = 2;
40
41
42
43
44 private final Map<String, Integer> theSelection;
45
46
47
48
49 private boolean pubFields;
50
51
52
53
54 public ArduinoMsgFilter() {
55 theSelection = new HashMap<>();
56 }
57
58
59
60
61
62 public int size() {
63 return theSelection.size();
64 }
65
66
67
68
69
70 public boolean isEmpty() {
71 return theSelection.isEmpty();
72 }
73
74
75
76
77
78 public boolean publicFields() {
79 return pubFields;
80 }
81
82
83
84
85 public void togglePublicFields() {
86 pubFields = !pubFields;
87 }
88
89
90
91
92
93 public boolean hasSelected() {
94
95 for (Integer myValue : theSelection.values()) {
96 if (!ZERO.equals(myValue)) {
97 return true;
98 }
99 }
100
101
102 return false;
103 }
104
105
106
107
108
109
110 public boolean isSelected(final String pId) {
111 final Integer myState = theSelection.get(pId);
112 return myState != null && !ZERO.equals(myState);
113 }
114
115
116
117
118
119
120 public boolean isParsed(final String pId) {
121 final Integer myState = theSelection.get(pId);
122 return myState != null && !ZERO.equals(myState & PARSEMSG);
123 }
124
125
126
127
128
129
130 public boolean isBuilt(final String pId) {
131 final Integer myState = theSelection.get(pId);
132 return myState != null && !ZERO.equals(myState & BUILDMSG);
133 }
134
135
136
137
138
139
140 public void toggleFlag(final String pId,
141 final int pFlag) {
142
143 Integer myState = theSelection.computeIfAbsent(pId, i -> ZERO);
144 myState ^= pFlag;
145
146
147 if (ZERO.equals(myState)) {
148 removeMessage(pId);
149 } else {
150 theSelection.put(pId, myState);
151 }
152 }
153
154
155
156
157
158 public void selectMessage(final String pId) {
159 theSelection.put(pId, PARSEMSG | BUILDMSG);
160 }
161
162
163
164
165
166 public void removeMessage(final String pId) {
167 theSelection.remove(pId);
168 }
169
170
171
172
173
174 public void resetSelection(final ArduinoNamedObject pObject) {
175 if (pObject instanceof ArduinoSystem) {
176 resetSelection((ArduinoSystem) pObject);
177 }
178 if (pObject instanceof ArduinoNode) {
179 resetSelection((ArduinoNode) pObject);
180 }
181 }
182
183
184
185
186
187 public void resetSelection(final ArduinoSystem pSystem) {
188
189 theSelection.clear();
190
191
192 for (ArduinoMessage myMessage : pSystem.getMessages()) {
193
194 theSelection.put(myMessage.getId(), PARSEMSG | BUILDMSG);
195 }
196 }
197
198
199
200
201
202 public void resetSelection(final ArduinoNode pNode) {
203
204 theSelection.clear();
205
206
207 for (ArduinoMessage myMessage : pNode.getOwner().getMessages()) {
208
209 int myValue = pNode.getMessages().contains(myMessage) ? BUILDMSG : ZERO;
210
211
212 if (pNode.receivesMessage(myMessage)) {
213 myValue |= PARSEMSG;
214 }
215
216
217 if (!ZERO.equals(myValue)) {
218
219 theSelection.put(myMessage.getId(), myValue);
220 }
221 }
222 }
223 }