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

File MapCache.java

 

Coverage histogram

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

Code metrics

2
21
13
2
142
91
14
0.67
1.62
6.5
1.08

Classes

Class Line # Actions
MapCache 38 16 0% 10 7
0.740740774.1%
MapCache.MapCacheEntry 42 5 0% 4 2
0.777777877.8%
 

Contributing tests

This file is covered by 27 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.internal;
21   
22    import java.util.HashMap;
23    import java.util.Map;
24   
25    import org.xwiki.cache.Cache;
26    import org.xwiki.cache.CacheEntry;
27    import org.xwiki.cache.event.CacheEntryEvent;
28    import org.xwiki.cache.util.AbstractCache;
29   
30    /**
31    * Implementation of {@link Cache} based on a simple Map.
32    * <p>
33    * Does not support eviction.
34    *
35    * @param <T> the class of the data stored in the cache.
36    * @version $Id: bcdeaceb2e5003b48e6e205b3c01def8145c87d9 $
37    */
 
38    public class MapCache<T> extends AbstractCache<T>
39    {
40    private Map<String, T> map = new HashMap<>();
41   
 
42    class MapCacheEntry implements CacheEntry<T>
43    {
44    private final String key;
45   
46    private final T value;
47   
 
48  40 toggle MapCacheEntry(String key, T value)
49    {
50  40 this.key = key;
51  40 this.value = value;
52    }
53   
 
54  0 toggle @Override
55    public Cache<T> getCache()
56    {
57  0 return MapCache.this;
58    }
59   
 
60  20 toggle @Override
61    public String getKey()
62    {
63  20 return this.key;
64    }
65   
 
66  34 toggle @Override
67    public T getValue()
68    {
69  34 return this.value;
70    }
71    }
72   
73    /**
74    * Default constructor.
75    */
 
76  27 toggle public MapCache()
77    {
78  27 super(null);
79    }
80   
 
81  99 toggle private CacheEntryEvent<T> toEvent(final String key, final T value)
82    {
83  99 return new CacheEntryEvent<T>()
84    {
 
85  40 toggle @Override
86    public CacheEntry<T> getEntry()
87    {
88  40 return new MapCacheEntry(key, value);
89    }
90   
 
91  0 toggle @Override
92    public Cache<T> getCache()
93    {
94  0 return MapCache.this;
95    }
96    };
97    }
98   
 
99  85 toggle @Override
100    public void set(String key, T value)
101    {
102  85 this.map.put(key, value);
103   
104  85 sendEntryAddedEvent(toEvent(key, value));
105    }
106   
 
107  238 toggle @Override
108    public T get(String key)
109    {
110  238 return this.map.get(key);
111    }
112   
 
113  14 toggle @Override
114    public void remove(String key)
115    {
116  14 if (this.map.containsKey(key)) {
117  14 T value = this.map.remove(key);
118   
119  14 sendEntryRemovedEvent(toEvent(key, value));
120    } else {
121  0 this.map.remove(key);
122    }
123    }
124   
 
125  23 toggle @Override
126    public void removeAll()
127    {
128  23 this.map.clear();
129   
130  23 Map<String, T> eventMap = new HashMap<>(this.map);
131   
132  23 for (Map.Entry<String, T> entry : eventMap.entrySet()) {
133  0 sendEntryRemovedEvent(toEvent(entry.getKey(), entry.getValue()));
134    }
135    }
136   
 
137  0 toggle @Override
138    public String toString()
139    {
140  0 return this.map.toString();
141    }
142    }