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

File EntityReferenceFactory.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
14
2
1
82
35
7
0.5
7
2
3.5

Classes

Class Line # Actions
EntityReferenceFactory 37 14 0% 7 1
0.958333395.8%
 

Contributing tests

This file is covered by 171 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.internal.reference;
21   
22    import javax.inject.Singleton;
23   
24    import org.xwiki.collection.SoftCache;
25    import org.xwiki.component.annotation.Component;
26    import org.xwiki.model.reference.EntityReference;
27   
28    /**
29    * Store and return shared soft reference of EntityReference instances of equals reference. This is used to reduce the
30    * memory footprint of caches containing entity references.
31    *
32    * @version $Id: 890b5aec1c234cfed65fd7b40b0baf1459724efa $
33    * @since 10.8RC1
34    */
35    @Component(roles = EntityReferenceFactory.class)
36    @Singleton
 
37    public class EntityReferenceFactory
38    {
39    private SoftCache<EntityReference, EntityReference> cache = new SoftCache<>();
40   
41    /**
42    * Return a cached reference equals to the passed one and with the same or extending class. If none could be found
43    * the passed reference is stored and returned.
44    *
45    * @param <E> the type of the reference
46    * @param reference the entity reference to find
47    * @return the cached entity reference
48    */
 
49  46692365 toggle public <E extends EntityReference> E getReference(E reference)
50    {
51  46701330 if (reference == null) {
52  177916 return null;
53    }
54   
55  46526113 EntityReference entityReference = this.cache.get(reference);
56   
57  46532827 if (entityReference == null || !reference.getClass().isAssignableFrom(entityReference.getClass())) {
58  65215 entityReference = unique(reference);
59   
60  65212 this.cache.put(entityReference, entityReference);
61    }
62   
63  46531881 return (E) entityReference;
64    }
65   
 
66  65215 toggle private <E extends EntityReference> E unique(E reference)
67    {
68  65216 EntityReference parent = reference.getParent();
69   
70  65214 EntityReference cachedParent = getReference(parent);
71   
72  65216 if (cachedParent != parent) {
73  27328 EntityReference uniqueReference = reference.replaceParent(cachedParent);
74   
75  27324 if (reference.getClass().isAssignableFrom(uniqueReference.getClass())) {
76  27325 return (E) uniqueReference;
77    }
78    }
79   
80  37886 return reference;
81    }
82    }