Clover Coverage Report - Subsonic-Android Coverage Report
Coverage timestamp: ven dic 19 2014 17:57:13 EST
../../../../../img/srcFileCovDistChart6.png 65% of files have more coverage
27   102   13   3
8   61   0,48   4,5
9     1,44  
2    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  LRUCache       Line # 28 22 9 51,4% 0.51428574
  LRUCache.TimestampedValue       Line # 79 5 4 77,8% 0.7777778
 
No Tests
 
1    /*
2    This file is part of Subsonic.
3   
4    Subsonic is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8   
9    Subsonic is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12    GNU General Public License for more details.
13   
14    You should have received a copy of the GNU General Public License
15    along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
16   
17    Copyright 2009 (C) Sindre Mehus
18    */
19    package net.sourceforge.subsonic.androidapp.util;
20   
21    import java.lang.ref.SoftReference;
22    import java.util.HashMap;
23    import java.util.Map;
24   
25    /**
26    * @author Sindre Mehus
27    */
 
28    public class LRUCache<K,V>{
29   
30    private final int capacity;
31    private final Map<K, TimestampedValue> map;
32   
 
33  3 toggle public LRUCache(int capacity) {
34  3 map = new HashMap<K, TimestampedValue>(capacity);
35  3 this.capacity = capacity;
36    }
37   
 
38  502 toggle public synchronized V get(K key) {
39  502 TimestampedValue value = map.get(key);
40   
41  502 V result = null;
42  502 if (value != null) {
43  430 value.updateTimestamp();
44  430 result = value.getValue();
45    }
46   
47  502 return result;
48    }
49   
 
50  90 toggle public synchronized void put(K key, V value) {
51  90 if (map.size() >= capacity) {
52  0 removeOldest();
53    }
54  90 map.put(key, new TimestampedValue(value));
55    }
56   
 
57  2 toggle public void clear() {
58  2 map.clear();
59    }
60   
 
61  0 toggle private void removeOldest() {
62  0 K oldestKey = null;
63  0 long oldestTimestamp = Long.MAX_VALUE;
64   
65  0 for (Map.Entry<K, TimestampedValue> entry : map.entrySet()) {
66  0 K key = entry.getKey();
67  0 TimestampedValue value = entry.getValue();
68  0 if (value.getTimestamp() < oldestTimestamp) {
69  0 oldestTimestamp = value.getTimestamp();
70  0 oldestKey = key;
71    }
72    }
73   
74  0 if (oldestKey != null) {
75  0 map.remove(oldestKey);
76    }
77    }
78   
 
79    private final class TimestampedValue {
80   
81    private final SoftReference<V> value;
82    private long timestamp;
83   
 
84  90 toggle public TimestampedValue(V value) {
85  90 this.value = new SoftReference<V>(value);
86  90 updateTimestamp();
87    }
88   
 
89  430 toggle public V getValue() {
90  430 return value.get();
91    }
92   
 
93  0 toggle public long getTimestamp() {
94  0 return timestamp;
95    }
96   
 
97  520 toggle public void updateTimestamp() {
98  520 timestamp = System.currentTimeMillis();
99    }
100    }
101   
102    }