1 |
|
package org.kuali.student.common.messagebuilder.impl; |
2 |
|
|
3 |
|
import java.util.ArrayList; |
4 |
|
import java.util.List; |
5 |
|
|
6 |
|
import org.kuali.student.common.messagebuilder.MessageTreeBuilder; |
7 |
|
import org.kuali.student.common.messagebuilder.booleanmessage.ast.BooleanNode; |
8 |
|
import org.kuali.student.common.messagebuilder.impl.exceptions.MessageBuilderException; |
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
|
|
| 99% |
Uncovered Elements: 1 (100) |
Complexity: 45 |
Complexity Density: 0.8 |
|
33 |
|
public class SimpleBooleanMessageBuilder implements MessageTreeBuilder { |
34 |
|
|
35 |
|
private final static char INDENT_CHAR = 0; |
36 |
|
|
37 |
|
private final static int INDENT_NUMBER_OF_SPACES = 0; |
38 |
|
|
39 |
|
private final static String INDENT_STRING = getString(INDENT_NUMBER_OF_SPACES, INDENT_CHAR); |
40 |
|
|
41 |
|
private final static String BOOLEAN_OPERATOR_PREFIX = " "; |
42 |
|
|
43 |
|
private final static String BOOLEAN_OPERATOR_SUFFIX = " "; |
44 |
|
|
45 |
|
|
46 |
|
private int indentCharacter = INDENT_CHAR; |
47 |
|
|
48 |
|
private int indentNumberOfSpaces = INDENT_NUMBER_OF_SPACES; |
49 |
|
|
50 |
|
private String indentString = INDENT_STRING; |
51 |
|
|
52 |
|
private String booleanOperatorPrefix = BOOLEAN_OPERATOR_PREFIX; |
53 |
|
|
54 |
|
private String booleanOperatorSuffix = BOOLEAN_OPERATOR_SUFFIX; |
55 |
|
|
56 |
|
|
57 |
|
private BooleanOperators booleanOperators; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
@param |
64 |
|
@param |
65 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
66 |
2
|
public SimpleBooleanMessageBuilder(String andOperator, String orOperator) {... |
67 |
2
|
this.booleanOperators = new BooleanOperators(andOperator, orOperator); |
68 |
|
} |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@param |
75 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
76 |
14
|
public SimpleBooleanMessageBuilder(BooleanOperators bo) {... |
77 |
14
|
this.booleanOperators = bo; |
78 |
|
} |
79 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
80 |
8
|
public void setIndentCharacter(int indentChar) {... |
81 |
8
|
this.indentCharacter = indentChar; |
82 |
|
} |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
84 |
8
|
public void setIndentNumberOfSpaces(int indentNumberOfSpaces) {... |
85 |
8
|
this.indentNumberOfSpaces = indentNumberOfSpaces; |
86 |
|
} |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
88 |
8
|
public void setIndentString(String indentString) {... |
89 |
8
|
this.indentString = indentString; |
90 |
|
} |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
92 |
8
|
public void setBooleanOperatorPrefix(String booleanOperatorPrefix) {... |
93 |
8
|
this.booleanOperatorPrefix = booleanOperatorPrefix; |
94 |
|
} |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
96 |
8
|
public void setBooleanOperatorSuffix(String booleanOperatorSuffix) {... |
97 |
8
|
this.booleanOperatorSuffix = booleanOperatorSuffix; |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
@param |
105 |
|
@return |
106 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
107 |
6
|
public String buildMessage(List<BooleanNode> nodeList) throws MessageBuilderException {... |
108 |
|
|
109 |
6
|
List<BooleanNode> rootNodeList = new ArrayList<BooleanNode>(); |
110 |
6
|
for(BooleanNode node : nodeList) { |
111 |
29
|
if(node.getParent() == null) { |
112 |
7
|
rootNodeList.add(node); |
113 |
|
} |
114 |
29
|
buildMessage(node); |
115 |
|
} |
116 |
6
|
if(rootNodeList.size() > 1) { |
117 |
1
|
throw new MessageBuilderException("Node list contains more than one root node: " + rootNodeList); |
118 |
|
} |
119 |
5
|
return rootNodeList.get(0).getNodeMessage(); |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
@param |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
128 |
32
|
public String buildMessage(final BooleanNode node) {... |
129 |
|
|
130 |
32
|
if(node.getLabel().equals("*")) { |
131 |
7
|
buildAndNodeSuccessMessage(node); |
132 |
|
} |
133 |
|
|
134 |
25
|
else if(node.getLabel().equals("+")) { |
135 |
9
|
buildOrNodeSuccessMessage(node); |
136 |
|
} |
137 |
32
|
return node.getNodeMessage(); |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
@param |
145 |
|
|
|
|
| 95% |
Uncovered Elements: 1 (20) |
Complexity: 13 |
Complexity Density: 1.08 |
|
146 |
7
|
private void buildAndNodeSuccessMessage(BooleanNode node) {... |
147 |
7
|
if(node.getLabel().equals("*") && |
148 |
|
node.getLeftNode() != null && |
149 |
|
node.getRightNode() != null && |
150 |
|
node.getLeftNode().getNodeMessage() != null && |
151 |
|
node.getRightNode().getNodeMessage() != null) { |
152 |
6
|
String preIndent = ""; |
153 |
6
|
if(node.getLeftNode().getChildCount() == 0) { |
154 |
6
|
preIndent = getIndent(node, 0); |
155 |
|
} |
156 |
6
|
String postIndent = ""; |
157 |
6
|
if(node.getRightNode().getChildCount() == 0) { |
158 |
3
|
postIndent = getIndent(node, 0); |
159 |
|
} |
160 |
|
|
161 |
6
|
String logMessage = this.indentString + node.getLeftNode().getNodeMessage() + |
162 |
|
this.booleanOperatorPrefix + |
163 |
|
preIndent + this.booleanOperators.getAndOperator() + |
164 |
|
this.booleanOperatorSuffix + |
165 |
|
postIndent + this.indentString + node.getRightNode().getNodeMessage(); |
166 |
|
|
167 |
6
|
if(node.getParent() != null && |
168 |
|
((node.getLabel().equals("+") && |
169 |
|
node.getParent().getLabel().equals("*")) || |
170 |
|
(node.getLabel().equals("*") && |
171 |
|
node.getParent().getLabel().equals("+")))) { |
172 |
3
|
logMessage = node.getLeftNode().getNodeMessage() + |
173 |
|
this.booleanOperatorPrefix + |
174 |
|
preIndent + this.booleanOperators.getAndOperator() + |
175 |
|
this.booleanOperatorSuffix + |
176 |
|
postIndent + this.indentString + node.getRightNode().getNodeMessage(); |
177 |
3
|
logMessage = preIndent + "(" + logMessage + ")"; |
178 |
|
} |
179 |
6
|
node.setNodeMessage(logMessage); |
180 |
|
} |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
@param |
188 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 13 |
Complexity Density: 1.08 |
|
189 |
9
|
private void buildOrNodeSuccessMessage(BooleanNode node) {... |
190 |
|
|
191 |
9
|
if(node.getLabel().equals("+") && |
192 |
|
node.getLeftNode() != null && |
193 |
|
node.getRightNode() != null && |
194 |
|
node.getLeftNode().getNodeMessage() != null && |
195 |
|
node.getRightNode().getNodeMessage() != null) { |
196 |
8
|
String preIndent = ""; |
197 |
8
|
if(node.getLeftNode().getChildCount() == 0) { |
198 |
7
|
preIndent = getIndent(node, 0); |
199 |
|
} |
200 |
8
|
String postIndent = ""; |
201 |
8
|
if(node.getRightNode().getChildCount() == 0) { |
202 |
6
|
postIndent = getIndent(node, 0); |
203 |
|
} |
204 |
|
|
205 |
8
|
String logMessage = this.indentString + node.getLeftNode().getNodeMessage() + |
206 |
|
this.booleanOperatorPrefix + |
207 |
|
preIndent + this.booleanOperators.getOrOperator() + |
208 |
|
this.booleanOperatorSuffix + |
209 |
|
postIndent + this.indentString + node.getRightNode().getNodeMessage(); |
210 |
|
|
211 |
8
|
if(node.getParent() != null && |
212 |
|
((node.getLabel().equals("+") && |
213 |
|
node.getParent().getLabel().equals("*")) || |
214 |
|
(node.getLabel().equals("*") && |
215 |
|
node.getParent().getLabel().equals("+")))) { |
216 |
3
|
logMessage = node.getLeftNode().getNodeMessage() + |
217 |
|
this.booleanOperatorPrefix + |
218 |
|
preIndent + this.booleanOperators.getOrOperator() + |
219 |
|
this.booleanOperatorSuffix + |
220 |
|
postIndent + this.indentString + node.getRightNode().getNodeMessage(); |
221 |
3
|
logMessage = preIndent + "(" + logMessage + ")"; |
222 |
|
} |
223 |
8
|
node.setNodeMessage(logMessage); |
224 |
|
} |
225 |
|
} |
226 |
|
|
227 |
|
private int parentCount; |
228 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
229 |
36
|
private void countParents(BooleanNode node) {... |
230 |
36
|
if(node.getParent() != null) { |
231 |
14
|
this.parentCount++; |
232 |
14
|
countParents(node.getParent()); |
233 |
|
} |
234 |
|
} |
235 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
236 |
22
|
private String getIndent(BooleanNode node, int multiplier) {... |
237 |
22
|
this.parentCount = 0; |
238 |
22
|
countParents(node); |
239 |
22
|
if(this.parentCount > 0) { |
240 |
10
|
return getString(this.parentCount * this.indentNumberOfSpaces + multiplier, (char) this.indentCharacter); |
241 |
|
} |
242 |
12
|
return ""; |
243 |
|
} |
244 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
245 |
12
|
private static String getString(int charCount, char indentChr) {... |
246 |
12
|
char[] chr = new char[charCount]; |
247 |
60
|
for(int i=0; i<chr.length; i++) { |
248 |
48
|
chr[i] = indentChr; |
249 |
|
} |
250 |
12
|
return String.valueOf(chr); |
251 |
|
} |
252 |
|
} |