1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.model.reference; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Collection; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.Comparator; |
26 |
|
import java.util.IdentityHashMap; |
27 |
|
import java.util.List; |
28 |
|
import java.util.Locale; |
29 |
|
import java.util.Map; |
30 |
|
import java.util.TreeMap; |
31 |
|
|
32 |
|
import org.xwiki.model.EntityType; |
33 |
|
|
34 |
|
|
35 |
|
@link |
36 |
|
|
37 |
|
@version |
38 |
|
@since |
39 |
|
|
|
|
| 87.5% |
Uncovered Elements: 16 (128) |
Complexity: 35 |
Complexity Density: 0.48 |
|
40 |
|
public class EntityReferenceTreeNode |
41 |
|
{ |
42 |
|
|
43 |
|
@link |
44 |
|
|
45 |
|
@version |
46 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
47 |
|
static final class LocaleComparator implements Comparator<Locale> |
48 |
|
{ |
49 |
|
static final LocaleComparator INSTANCE = new LocaleComparator(); |
50 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
51 |
2 |
private LocaleComparator()... |
52 |
|
{ |
53 |
|
|
54 |
|
} |
55 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
56 |
8 |
@Override... |
57 |
|
public int compare(Locale locale1, Locale locale2) |
58 |
|
{ |
59 |
8 |
return locale1.toString().compareTo(locale2.toString()); |
60 |
|
} |
61 |
|
} |
62 |
|
|
63 |
|
private final EntityReference reference; |
64 |
|
|
65 |
|
private final int referenceSize; |
66 |
|
|
67 |
|
private final Comparator<String> comparator; |
68 |
|
|
69 |
|
private Map<String, Map<EntityType, EntityReferenceTreeNode>> children; |
70 |
|
|
71 |
|
private Map<Locale, EntityReference> locales; |
72 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
73 |
30 |
protected EntityReferenceTreeNode(Comparator<String> comparator)... |
74 |
|
{ |
75 |
30 |
this.reference = null; |
76 |
30 |
this.referenceSize = 0; |
77 |
30 |
this.comparator = comparator; |
78 |
|
} |
79 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
80 |
104 |
EntityReferenceTreeNode(EntityReference reference, Comparator<String> comparator)... |
81 |
|
{ |
82 |
104 |
this.reference = reference; |
83 |
104 |
this.referenceSize = reference.getReversedReferenceChain().size(); |
84 |
104 |
this.comparator = comparator; |
85 |
|
} |
86 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (26) |
Complexity: 6 |
Complexity Density: 0.38 |
|
87 |
148 |
void addChild(EntityReference childReference)... |
88 |
|
{ |
89 |
148 |
if (this.children == null) { |
90 |
83 |
this.children = new TreeMap<String, Map<EntityType, EntityReferenceTreeNode>>(this.comparator); |
91 |
|
} |
92 |
|
|
93 |
148 |
List<EntityReference> childReferenceList = childReference.getReversedReferenceChain(); |
94 |
|
|
95 |
148 |
EntityReference childNodeReference = childReferenceList.get(this.referenceSize); |
96 |
|
|
97 |
148 |
Map<EntityType, EntityReferenceTreeNode> childrenByType = this.children.get(childNodeReference.getName()); |
98 |
148 |
if (childrenByType == null) { |
99 |
103 |
childrenByType = new IdentityHashMap<>(); |
100 |
103 |
this.children.put(childNodeReference.getName(), childrenByType); |
101 |
|
} |
102 |
|
|
103 |
148 |
EntityReferenceTreeNode childNode = childrenByType.get(childNodeReference.getType()); |
104 |
148 |
if (childNode == null) { |
105 |
104 |
childNode = new EntityReferenceTreeNode(childNodeReference, this.comparator); |
106 |
|
} |
107 |
|
|
108 |
148 |
if (childReferenceList.size() > this.referenceSize + 1) { |
109 |
104 |
childNode.addChild(childReference); |
110 |
|
} |
111 |
|
|
112 |
148 |
if (childNodeReference.getParameter(DocumentReference.LOCALE) != null) { |
113 |
8 |
childNode.addLocale(childReference); |
114 |
|
} |
115 |
|
|
116 |
148 |
childrenByType.put(childNodeReference.getType(), childNode); |
117 |
|
} |
118 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
119 |
8 |
void addLocale(EntityReference childReference)... |
120 |
|
{ |
121 |
8 |
if (this.locales == null) { |
122 |
8 |
this.locales = new TreeMap<Locale, EntityReference>(LocaleComparator.INSTANCE); |
123 |
|
} |
124 |
|
|
125 |
8 |
this.locales.put((Locale) childReference.getParameter(DocumentReference.LOCALE), childReference); |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
@return |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
131 |
268 |
public EntityReference getReference()... |
132 |
|
{ |
133 |
268 |
return this.reference; |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
@param |
140 |
|
@return |
141 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
142 |
11 |
public EntityReferenceTreeNode get(String name)... |
143 |
|
{ |
144 |
11 |
Collection<EntityReferenceTreeNode> childrenWhithName = getChildrenInternal(name); |
145 |
|
|
146 |
11 |
if (childrenWhithName != null && !childrenWhithName.isEmpty()) { |
147 |
11 |
return childrenWhithName.iterator().next(); |
148 |
|
} |
149 |
|
|
150 |
0 |
return null; |
151 |
|
} |
152 |
|
|
153 |
|
|
154 |
|
@param@link |
155 |
|
@return |
156 |
|
@since |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 5 |
Complexity Density: 0.38 |
|
158 |
26 |
public EntityReferenceTreeNode get(EntityReference path)... |
159 |
|
{ |
160 |
26 |
if (path == null) { |
161 |
1 |
return null; |
162 |
|
} |
163 |
|
|
164 |
25 |
EntityReferenceTreeNode descendant = this; |
165 |
25 |
for (EntityReference pathElement : path.getReversedReferenceChain()) { |
166 |
54 |
if (descendant.children == null) { |
167 |
8 |
return null; |
168 |
|
} |
169 |
|
|
170 |
46 |
Map<EntityType, EntityReferenceTreeNode> descendantByType = descendant.children.get(pathElement.getName()); |
171 |
46 |
if (descendantByType == null) { |
172 |
1 |
return null; |
173 |
|
} |
174 |
|
|
175 |
45 |
descendant = descendantByType.get(pathElement.getType()); |
176 |
45 |
if (descendant == null) { |
177 |
1 |
return null; |
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
15 |
return descendant; |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
|
@return |
186 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
187 |
120 |
public Collection<EntityReferenceTreeNode> getChildren()... |
188 |
|
{ |
189 |
120 |
if (this.children != null) { |
190 |
89 |
List<EntityReferenceTreeNode> childrenList = new ArrayList<>(this.children.size() * 2); |
191 |
|
|
192 |
89 |
for (Map<EntityType, EntityReferenceTreeNode> childrenByType : this.children.values()) { |
193 |
115 |
childrenList.addAll(childrenByType.values()); |
194 |
|
} |
195 |
|
|
196 |
89 |
return childrenList; |
197 |
|
} |
198 |
|
|
199 |
31 |
return Collections.emptyList(); |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
|
@param |
204 |
|
@return |
205 |
|
@since |
206 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
207 |
0 |
public Collection<EntityReferenceTreeNode> getChildren(String name)... |
208 |
|
{ |
209 |
0 |
Collection<EntityReferenceTreeNode> childrenWhithName = getChildrenInternal(name); |
210 |
|
|
211 |
0 |
return childrenWhithName != null ? Collections.unmodifiableCollection(childrenWhithName) |
212 |
|
: Collections.<EntityReferenceTreeNode>emptyList(); |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
|
@param |
217 |
|
@return@link |
218 |
|
@since |
219 |
|
|
|
|
| 75% |
Uncovered Elements: 3 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
220 |
4 |
public Collection<EntityReferenceTreeNode> getChildren(EntityType type)... |
221 |
|
{ |
222 |
4 |
if (this.children != null) { |
223 |
4 |
List<EntityReferenceTreeNode> childrenList = new ArrayList<>(this.children.size()); |
224 |
|
|
225 |
4 |
for (Map<EntityType, EntityReferenceTreeNode> childrenByType : this.children.values()) { |
226 |
4 |
EntityReferenceTreeNode child = childrenByType.get(type); |
227 |
4 |
if (child != null) { |
228 |
4 |
childrenList.add(child); |
229 |
|
} |
230 |
|
} |
231 |
|
|
232 |
4 |
return childrenList; |
233 |
|
} |
234 |
|
|
235 |
0 |
return Collections.emptyList(); |
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
|
@param |
240 |
|
@return |
241 |
|
|
|
|
| 66.7% |
Uncovered Elements: 3 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
242 |
11 |
public Collection<EntityReferenceTreeNode> getChildrenInternal(String name)... |
243 |
|
{ |
244 |
11 |
if (this.children != null) { |
245 |
11 |
Map<EntityType, EntityReferenceTreeNode> childrenByType = this.children.get(name); |
246 |
|
|
247 |
11 |
if (childrenByType != null) { |
248 |
11 |
return childrenByType.values(); |
249 |
|
} |
250 |
|
} |
251 |
|
|
252 |
0 |
return null; |
253 |
|
} |
254 |
|
|
255 |
|
|
256 |
|
@return@link |
257 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
258 |
32 |
public Collection<EntityReference> getLocales()... |
259 |
|
{ |
260 |
32 |
return this.locales != null ? this.locales.values() : Collections.<EntityReference>emptyList(); |
261 |
|
} |
262 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
263 |
21 |
@Override... |
264 |
|
public String toString() |
265 |
|
{ |
266 |
21 |
StringBuilder builder = new StringBuilder(); |
267 |
|
|
268 |
21 |
if (getReference() != null) { |
269 |
21 |
builder.append(getReference()); |
270 |
|
} |
271 |
|
|
272 |
21 |
if (!getChildren().isEmpty()) { |
273 |
9 |
if (builder.length() > 0) { |
274 |
9 |
builder.append(" = "); |
275 |
|
} |
276 |
9 |
builder.append(getChildren()); |
277 |
|
} |
278 |
|
|
279 |
21 |
return builder.toString(); |
280 |
|
} |
281 |
|
} |