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

File SoftCache.java

 

Coverage histogram

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

Code metrics

4
14
3
1
101
39
5
0.36
4.67
3
1.67

Classes

Class Line # Actions
SoftCache 38 14 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 590 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.collection;
21   
22    import java.lang.ref.SoftReference;
23    import java.util.WeakHashMap;
24    import java.util.concurrent.locks.ReentrantReadWriteLock;
25   
26    import org.xwiki.stability.Unstable;
27   
28    /**
29    * A concurrent version of {@link WeakHashMap} in which the values are soft references.
30    *
31    * @param <K> the type of keys maintained by this map
32    * @param <V> the type of mapped values
33    * @version $Id: 03ddf918aeab6c35fb3eb1f2f9337e90f9106161 $
34    * @since 10.8RC1
35    * @since 9.11.8
36    */
37    @Unstable
 
38    public class SoftCache<K, V>
39    {
40    private WeakHashMap<K, SoftReference<V>> map = new WeakHashMap<>();
41   
42    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
43   
44    /**
45    * @param key the entry key
46    * @return the value associated to the passed key
47    */
 
48  53454029 toggle public V get(K key)
49    {
50  53458581 this.lock.readLock().lock();
51   
52  53471916 try {
53  53471973 SoftReference<V> reference = this.map.get(key);
54   
55  53468323 return reference != null ? reference.get() : null;
56    } finally {
57  53468068 this.lock.readLock().unlock();
58    }
59    }
60   
61    /**
62    * Get the value associated to the passed key. If no value can be found stored and return the passed default value.
63    *
64    * @param key the entry key
65    * @param defaultValue the default value
66    * @return the value associated to the passed key
67    */
 
68  3639594 toggle public V get(K key, V defaultValue)
69    {
70    // Check if we only know an equal entry
71  3639594 V sharedValue = get(key);
72   
73  3639594 if (sharedValue == null) {
74    // If no entry can be found, store and return the passed one
75  1689104 sharedValue = defaultValue;
76   
77    // Make sure to remember the entry
78  1689104 put(key, defaultValue);
79    }
80   
81    // Return the shared entry
82  3639594 return sharedValue;
83    }
84   
85    /**
86    * Associate passed key to passed value.
87    *
88    * @param key the entry key
89    * @param value the entry value
90    */
 
91  1791346 toggle public void put(K key, V value)
92    {
93  1791350 this.lock.writeLock().lock();
94   
95  1791361 try {
96  1791361 this.map.put(key, new SoftReference<>(value));
97    } finally {
98  1791361 this.lock.writeLock().unlock();
99    }
100    }
101    }