1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.icon.internal

File DefaultIconSetCache.java

 

Coverage histogram

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

Code metrics

0
18
14
1
149
102
15
0.83
1.29
14
1.07

Classes

Class Line # Actions
DefaultIconSetCache 47 18 0% 15 0
1.0100%
 

Contributing tests

This file is covered by 11 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.icon.internal;
21   
22    import javax.inject.Inject;
23    import javax.inject.Singleton;
24   
25    import org.xwiki.cache.Cache;
26    import org.xwiki.cache.CacheException;
27    import org.xwiki.cache.CacheFactory;
28    import org.xwiki.cache.CacheManager;
29    import org.xwiki.cache.config.CacheConfiguration;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.component.manager.ComponentLookupException;
32    import org.xwiki.component.phase.Initializable;
33    import org.xwiki.component.phase.InitializationException;
34    import org.xwiki.icon.IconSet;
35    import org.xwiki.icon.IconSetCache;
36    import org.xwiki.model.reference.DocumentReference;
37    import org.xwiki.model.reference.EntityReferenceSerializer;
38   
39    /**
40    * Default implementation of {@link org.xwiki.icon.IconSetCache}.
41    *
42    * @since 6.2M1
43    * @version $Id: c6ba56574305cbd47d70ac08206b7b5768380a02 $
44    */
45    @Component
46    @Singleton
 
47    public class DefaultIconSetCache implements IconSetCache, Initializable
48    {
49    private static final String ICON_SET_CACHE_ID = "iconset";
50   
51    private static final String NAME_SUFFIX = "NAMED:";
52   
53    private static final String DOCUMENT_SUFFIX = "DOC:";
54   
55    @Inject
56    private CacheManager cacheManager;
57   
58    @Inject
59    private EntityReferenceSerializer<String> entityReferenceSerializer;
60   
61    private Cache<IconSet> cache;
62   
 
63  72 toggle @Override
64    public void initialize() throws InitializationException
65    {
66  72 try {
67  72 CacheConfiguration configuration = new CacheConfiguration(ICON_SET_CACHE_ID);
68  72 CacheFactory cacheFactory = cacheManager.getCacheFactory();
69  72 this.cache = cacheFactory.newCache(configuration);
70    } catch (ComponentLookupException | CacheException e) {
71  1 throw new InitializationException("Failed to initialize the IconSet Cache.", e);
72    }
73    }
74   
 
75  23 toggle private String getCacheKey(String name, String wikiId)
76    {
77  23 return wikiId.length() + wikiId + '_' + name;
78    }
79   
 
80  2674 toggle @Override
81    public IconSet get(String name)
82    {
83  2674 return cache.get(getKeyFromName(name));
84    }
85   
 
86  19 toggle @Override
87    public IconSet get(String name, String wikiId)
88    {
89  19 return get(getCacheKey(name, wikiId));
90    }
91   
 
92  1 toggle @Override
93    public IconSet get(DocumentReference documentReference)
94    {
95  1 return cache.get(getKeyFromDocument(documentReference));
96    }
97   
 
98  32 toggle @Override
99    public void put(String name, IconSet iconSet)
100    {
101  32 cache.set(getKeyFromName(name), iconSet);
102    }
103   
 
104  3 toggle @Override
105    public void put(String name, String wikiId, IconSet iconSet)
106    {
107  3 put(getCacheKey(name, wikiId), iconSet);
108    }
109   
 
110  3 toggle @Override
111    public void put(DocumentReference documentReference, IconSet iconSet)
112    {
113  3 cache.set(getKeyFromDocument(documentReference), iconSet);
114    }
115   
 
116  1 toggle @Override
117    public void clear()
118    {
119  1 cache.removeAll();
120    }
121   
 
122  1 toggle @Override
123    public void clear(DocumentReference documentReference)
124    {
125  1 cache.remove(getKeyFromDocument(documentReference));
126    }
127   
 
128  2 toggle @Override
129    public void clear(String name)
130    {
131  2 cache.remove(getKeyFromName(name));
132    }
133   
 
134  1 toggle @Override
135    public void clear(String name, String wikiId)
136    {
137  1 clear(getCacheKey(name, wikiId));
138    }
139   
 
140  2707 toggle private String getKeyFromName(String name)
141    {
142  2707 return NAME_SUFFIX + name;
143    }
144   
 
145  5 toggle private String getKeyFromDocument(DocumentReference docRef)
146    {
147  5 return DOCUMENT_SUFFIX + entityReferenceSerializer.serialize(docRef);
148    }
149    }