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

File AbstractCache.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

4
17
9
1
149
74
12
0.71
1.89
9
1.33

Classes

Class Line # Actions
AbstractCache 38 17 0% 12 7
0.7666666576.7%
 

Contributing tests

This file is covered by 117 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.cache.util;
21   
22    import javax.swing.event.EventListenerList;
23   
24    import org.slf4j.Logger;
25    import org.slf4j.LoggerFactory;
26    import org.xwiki.cache.Cache;
27    import org.xwiki.cache.DisposableCacheValue;
28    import org.xwiki.cache.config.CacheConfiguration;
29    import org.xwiki.cache.event.CacheEntryEvent;
30    import org.xwiki.cache.event.CacheEntryListener;
31   
32    /**
33    * Base class for {@link Cache} implementations. It provides events {@link DisposableCacheValue} management.
34    *
35    * @param <T> the class of the data stored in the cache.
36    * @version $Id: 24ea2f096e78041b415f2d9d09e4fc176ab8996b $
37    */
 
38    public abstract class AbstractCache<T> implements Cache<T>
39    {
40    /**
41    * The logger to use to log.
42    */
43    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractCache.class);
44   
45    /**
46    * The configuration used to create the cache.
47    */
48    protected final CacheConfiguration configuration;
49   
50    /**
51    * The list of listener to called when events appends on a cache entry.
52    */
53    protected final EventListenerList cacheEntryListeners = new EventListenerList();
54   
55    /**
56    * @deprecated since 8.3RC1, use {@link #AbstractCache(CacheConfiguration)} instead
57    */
 
58  0 toggle @Deprecated
59    public AbstractCache()
60    {
61  0 this(null);
62    }
63   
64    /**
65    * @param configuration the configuration of the cache
66    */
 
67  1346 toggle public AbstractCache(CacheConfiguration configuration)
68    {
69  1346 this.configuration = configuration;
70    }
71   
 
72  10 toggle @Override
73    public void dispose()
74    {
75  10 for (CacheEntryListener<T> listener : this.cacheEntryListeners.getListeners(CacheEntryListener.class)) {
76  4 this.cacheEntryListeners.remove(CacheEntryListener.class, listener);
77    }
78    }
79   
 
80  5 toggle @Override
81    public void addCacheEntryListener(CacheEntryListener<T> listener)
82    {
83  5 this.cacheEntryListeners.add(CacheEntryListener.class, listener);
84    }
85   
 
86  0 toggle @Override
87    public void removeCacheEntryListener(CacheEntryListener<T> listener)
88    {
89  0 cacheEntryListeners.remove(CacheEntryListener.class, listener);
90    }
91   
92    /**
93    * Helper method to send event when a new cache entry is inserted.
94    *
95    * @param event the event to send.
96    */
 
97  38667 toggle protected void sendEntryAddedEvent(CacheEntryEvent<T> event)
98    {
99  38666 for (org.xwiki.cache.event.CacheEntryListener<T> listener : this.cacheEntryListeners
100    .getListeners(org.xwiki.cache.event.CacheEntryListener.class)) {
101  7 listener.cacheEntryAdded(event);
102    }
103    }
104   
105    /**
106    * Helper method to send event when an existing cache entry is removed.
107    *
108    * @param event the event to send.
109    */
 
110  27832 toggle protected void sendEntryRemovedEvent(CacheEntryEvent<T> event)
111    {
112  27832 for (org.xwiki.cache.event.CacheEntryListener<T> listener : this.cacheEntryListeners
113    .getListeners(org.xwiki.cache.event.CacheEntryListener.class)) {
114  6 listener.cacheEntryRemoved(event);
115    }
116   
117  27832 disposeCacheValue(event.getEntry().getValue());
118    }
119   
120    /**
121    * Helper method to send event when a cache entry is modified.
122    *
123    * @param event the event to send.
124    */
 
125  838 toggle protected void sendEntryModifiedEvent(CacheEntryEvent<T> event)
126    {
127  837 for (org.xwiki.cache.event.CacheEntryListener<T> listener : this.cacheEntryListeners
128    .getListeners(org.xwiki.cache.event.CacheEntryListener.class)) {
129  1 listener.cacheEntryModified(event);
130    }
131    }
132   
133    /**
134    * Dispose the value being removed from the cache.
135    *
136    * @param value the value to dispose
137    */
 
138  27913 toggle protected void disposeCacheValue(T value)
139    {
140  27913 if (value instanceof DisposableCacheValue) {
141  5252 try {
142  5252 ((DisposableCacheValue) value).dispose();
143    } catch (Throwable e) {
144  0 LOGGER.warn("Error when trying to dispose a cache object of cache [{}]",
145  0 this.configuration != null ? this.configuration.getConfigurationId() : null, e);
146    }
147    }
148    }
149    }