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

File SoftCache.java

 

Coverage histogram

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

Code metrics

4
14
3
1
98
37
5
0.36
4.67
3
1.67

Classes

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

Contributing tests

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