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

File AbstractCache.java

 

Coverage histogram

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

Code metrics

4
17
9
1
152
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 158 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: 0215579da91f5c992e51213a0604d939f3483c40 $
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  1900 toggle public AbstractCache(CacheConfiguration configuration)
68    {
69  1900 this.configuration = configuration;
70    }
71   
 
72  12 toggle @Override
73    public void dispose()
74    {
75  12 for (CacheEntryListener<T> listener : this.cacheEntryListeners.getListeners(CacheEntryListener.class)) {
76  4 this.cacheEntryListeners.remove(CacheEntryListener.class, listener);
77    }
78    }
79   
 
80  153 toggle @Override
81    public void addCacheEntryListener(CacheEntryListener<T> listener)
82    {
83  153 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  273764 toggle protected void sendEntryAddedEvent(CacheEntryEvent<T> event)
98    {
99  273762 for (org.xwiki.cache.event.CacheEntryListener<T> listener : this.cacheEntryListeners
100    .getListeners(org.xwiki.cache.event.CacheEntryListener.class)) {
101  13563 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  187026 toggle protected void sendEntryRemovedEvent(CacheEntryEvent<T> event)
111    {
112  187027 for (org.xwiki.cache.event.CacheEntryListener<T> listener : this.cacheEntryListeners
113    .getListeners(org.xwiki.cache.event.CacheEntryListener.class)) {
114  13094 listener.cacheEntryRemoved(event);
115    }
116   
117  187029 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  12633 toggle protected void sendEntryModifiedEvent(CacheEntryEvent<T> event)
126    {
127  12633 for (org.xwiki.cache.event.CacheEntryListener<T> listener : this.cacheEntryListeners
128    .getListeners(org.xwiki.cache.event.CacheEntryListener.class)) {
129  2 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  187539 toggle protected void disposeCacheValue(T value)
139    {
140  187542 if (value instanceof DisposableCacheValue) {
141  89748 try {
142  89748 ((DisposableCacheValue) value).dispose();
143    } catch (Throwable e) {
144    // We catch Throwable because this method is usually automatically called by an event send by the cache
145    // implementation and there is no reason to crash the whole cache because of some badly implemented
146    // dispose() we don't control.
147  0 LOGGER.warn("Error when trying to dispose a cache object of cache [{}]",
148  0 this.configuration != null ? this.configuration.getConfigurationId() : null, e);
149    }
150    }
151    }
152    }