1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.model.reference

File EntityReferenceTreeNode.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

42
74
15
2
281
167
37
0.5
4.93
7.5
2.47

Classes

Class Line # Actions
EntityReferenceTreeNode 40 73 0% 35 13
0.898437589.8%
EntityReferenceTreeNode.LocaleComparator 47 1 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 20 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * A node in a {@link EntityReferenceTree}.
36    *
37    * @version $Id: e4444f7d822a663f2f28b462aee1d0b03ddfaaef $
38    * @since 5.4RC1
39    */
 
40    public class EntityReferenceTreeNode
41    {
42    /**
43    * USed to order {@link Locale}s.
44    *
45    * @version $Id: e4444f7d822a663f2f28b462aee1d0b03ddfaaef $
46    */
 
47    static final class LocaleComparator implements Comparator<Locale>
48    {
49    static final LocaleComparator INSTANCE = new LocaleComparator();
50   
 
51  4 toggle private LocaleComparator()
52    {
53    // Stateless
54    }
55   
 
56  158 toggle @Override
57    public int compare(Locale locale1, Locale locale2)
58    {
59  158 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   
 
73  124 toggle protected EntityReferenceTreeNode(Comparator<String> comparator)
74    {
75  124 this.reference = null;
76  124 this.referenceSize = 0;
77  124 this.comparator = comparator;
78    }
79   
 
80  707 toggle EntityReferenceTreeNode(EntityReference reference, Comparator<String> comparator)
81    {
82  707 this.reference = reference;
83  707 this.referenceSize = reference.getReversedReferenceChain().size();
84  707 this.comparator = comparator;
85    }
86   
 
87  1781 toggle void addChild(EntityReference childReference)
88    {
89  1781 if (this.children == null) {
90  295 this.children = new TreeMap<String, Map<EntityType, EntityReferenceTreeNode>>(this.comparator);
91    }
92   
93  1781 List<EntityReference> childReferenceList = childReference.getReversedReferenceChain();
94   
95  1781 EntityReference childNodeReference = childReferenceList.get(this.referenceSize);
96   
97  1781 Map<EntityType, EntityReferenceTreeNode> childrenByType = this.children.get(childNodeReference.getName());
98  1781 if (childrenByType == null) {
99  705 childrenByType = new IdentityHashMap<>();
100  705 this.children.put(childNodeReference.getName(), childrenByType);
101    }
102   
103  1781 EntityReferenceTreeNode childNode = childrenByType.get(childNodeReference.getType());
104  1781 if (childNode == null) {
105  707 childNode = new EntityReferenceTreeNode(childNodeReference, this.comparator);
106    }
107   
108  1781 if (childReferenceList.size() > this.referenceSize + 1) {
109  1233 childNode.addChild(childReference);
110    }
111   
112  1781 if (childNodeReference.getParameter(DocumentReference.LOCALE) != null) {
113  66 childNode.addLocale(childReference);
114    }
115   
116  1781 childrenByType.put(childNodeReference.getType(), childNode);
117    }
118   
 
119  66 toggle void addLocale(EntityReference childReference)
120    {
121  66 if (this.locales == null) {
122  27 this.locales = new TreeMap<Locale, EntityReference>(LocaleComparator.INSTANCE);
123    }
124   
125  66 this.locales.put((Locale) childReference.getParameter(DocumentReference.LOCALE), childReference);
126    }
127   
128    /**
129    * @return the reference associated to this node
130    */
 
131  1553 toggle public EntityReference getReference()
132    {
133  1553 return this.reference;
134    }
135   
136    /**
137    * Return the first found direct child with passed name.
138    *
139    * @param name the name of the child node
140    * @return the node associated to the passed name
141    */
 
142  11 toggle 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 path a path in the tree starting from this node, specified as an {@link EntityReference}
155    * @return the node associated to the specified path
156    * @since 7.2M1
157    */
 
158  114 toggle public EntityReferenceTreeNode get(EntityReference path)
159    {
160  114 if (path == null) {
161  1 return null;
162    }
163   
164  113 EntityReferenceTreeNode descendant = this;
165  113 for (EntityReference pathElement : path.getReversedReferenceChain()) {
166  187 if (descendant.children == null) {
167  58 return null;
168    }
169   
170  129 Map<EntityType, EntityReferenceTreeNode> descendantByType = descendant.children.get(pathElement.getName());
171  129 if (descendantByType == null) {
172  1 return null;
173    }
174   
175  128 descendant = descendantByType.get(pathElement.getType());
176  128 if (descendant == null) {
177  1 return null;
178    }
179    }
180   
181  53 return descendant;
182    }
183   
184    /**
185    * @return the child reference nodes
186    */
 
187  651 toggle public Collection<EntityReferenceTreeNode> getChildren()
188    {
189  651 if (this.children != null) {
190  305 List<EntityReferenceTreeNode> childrenList = new ArrayList<>(this.children.size() * 2);
191   
192  305 for (Map<EntityType, EntityReferenceTreeNode> childrenByType : this.children.values()) {
193  819 childrenList.addAll(childrenByType.values());
194    }
195   
196  305 return childrenList;
197    }
198   
199  346 return Collections.emptyList();
200    }
201   
202    /**
203    * @param name the name of the children to return
204    * @return the children with the passed name
205    * @since 7.4M1
206    */
 
207  0 toggle 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 type the type of the children to return
217    * @return the children with the passed {@link EntityType}
218    * @since 7.4M1
219    */
 
220  4 toggle 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 name the name of the children to return
240    * @return the children with the passed name
241    */
 
242  11 toggle 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 the child references containing a {@link Locale}
257    */
 
258  127 toggle public Collection<EntityReference> getLocales()
259    {
260  127 return this.locales != null ? this.locales.values() : Collections.<EntityReference>emptyList();
261    }
262   
 
263  137 toggle @Override
264    public String toString()
265    {
266  137 StringBuilder builder = new StringBuilder();
267   
268  137 if (getReference() != null) {
269  136 builder.append(getReference());
270    }
271   
272  137 if (!getChildren().isEmpty()) {
273  55 if (builder.length() > 0) {
274  54 builder.append(" = ");
275    }
276  55 builder.append(getChildren());
277    }
278   
279  137 return builder.toString();
280    }
281    }