Coverage Report - org.dozer.cache.DozerCache
 
Classes in this File Line Coverage Branch Coverage Complexity
DozerCache
93%
31/33
100%
10/10
1.643
DozerCache$LRUMap
100%
5/5
100%
2/2
1.643
 
 1  
 /**
 2  
  * Copyright 2005-2013 Dozer Project
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.dozer.cache;
 17  
 
 18  
 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
 19  
 import org.apache.commons.lang3.builder.ToStringStyle;
 20  
 import org.dozer.stats.GlobalStatistics;
 21  
 import org.dozer.stats.StatisticType;
 22  
 import org.dozer.stats.StatisticsManager;
 23  
 
 24  
 import java.util.Collection;
 25  
 import java.util.LinkedHashMap;
 26  
 import java.util.Map;
 27  
 import java.util.Set;
 28  
 
 29  
 /**
 30  
  * Map backed Cache implementation.
 31  
  *
 32  
  * @author tierney.matt
 33  
  * @author dmitry.buzdin
 34  
  */
 35  
 public class DozerCache<KeyType, ValueType> implements Cache<KeyType, ValueType> {
 36  
 
 37  
   private final String name;
 38  
 
 39  
   private final LRUMap cacheMap;
 40  
 
 41  2293
   StatisticsManager statMgr = GlobalStatistics.getInstance().getStatsMgr();
 42  
 
 43  2293
   public DozerCache(final String name, final int maximumSize) {
 44  2293
     if (maximumSize < 1) {
 45  1
       throw new IllegalArgumentException("Dozer cache max size must be greater than 0");
 46  
     }
 47  2292
     this.name = name;
 48  2292
     this.cacheMap = new LRUMap(maximumSize); // TODO This should be in Collections.synchronizedMap()
 49  2292
   }
 50  
 
 51  
   public void clear() {
 52  2
     cacheMap.clear();
 53  2
   }
 54  
 
 55  
   public synchronized void put(KeyType key, ValueType value) {
 56  3909
     if (key == null) {
 57  1
       throw new IllegalArgumentException("Cache entry key cannot be null");
 58  
     }
 59  3908
     CacheEntry<KeyType, ValueType> cacheEntry = new CacheEntry<KeyType, ValueType>(key, value);
 60  3908
     cacheMap.put(cacheEntry.getKey(), cacheEntry);
 61  3908
   }
 62  
 
 63  
   public ValueType get(KeyType key) {
 64  301722
     if (key == null) {
 65  1
       throw new IllegalArgumentException("Key cannot be null");
 66  
     }
 67  301721
     CacheEntry<KeyType, ValueType> result = cacheMap.get(key);
 68  301721
     if (result != null) {
 69  300422
       statMgr.increment(StatisticType.CACHE_HIT_COUNT, name);
 70  300422
       return result.getValue();
 71  
     } else {
 72  1299
       statMgr.increment(StatisticType.CACHE_MISS_COUNT, name);
 73  1299
       return null;
 74  
     }
 75  
   }
 76  
 
 77  
   public void addEntries(Collection<CacheEntry<KeyType, ValueType>> entries) {
 78  1
     for (CacheEntry<KeyType, ValueType> entry : entries) {
 79  2
       cacheMap.put(entry.getKey(), entry);
 80  2
     }
 81  1
   }
 82  
 
 83  
   public Collection<CacheEntry<KeyType, ValueType>> getEntries() {
 84  3
     return cacheMap.values();
 85  
   }
 86  
 
 87  
   public String getName() {
 88  2281
     return name;
 89  
   }
 90  
 
 91  
   public long getSize() {
 92  9
     return cacheMap.size();
 93  
   }
 94  
 
 95  
   public long getMaxSize() {
 96  1
     return cacheMap.maximumSize;
 97  
   }
 98  
 
 99  
   public boolean containsKey(KeyType key) {
 100  131146
     return cacheMap.containsKey(key);
 101  
   }
 102  
 
 103  
   public Set<KeyType> keySet() {
 104  0
     return cacheMap.keySet();
 105  
   }
 106  
 
 107  
   @Override
 108  
   public String toString() {
 109  0
     return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
 110  
   }
 111  
 
 112  1
   class LRUMap extends LinkedHashMap<KeyType, CacheEntry<KeyType, ValueType>> {
 113  
 
 114  
     private int maximumSize;
 115  
 
 116  2292
     LRUMap(int maximumSize) {
 117  2292
       this.maximumSize = maximumSize;
 118  2292
     }
 119  
 
 120  
     @Override
 121  
     protected boolean removeEldestEntry(Map.Entry<KeyType, CacheEntry<KeyType, ValueType>> eldest) {
 122  3828
       return size() > maximumSize;
 123  
     }
 124  
     
 125  
   }
 126  
 
 127  
 }