Coverage Report - org.dozer.cache.DozerCacheManager
 
Classes in this File Line Coverage Branch Coverage Complexity
DozerCacheManager
93%
27/29
100%
8/8
1.5
 
 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.dozer.util.MappingUtils;
 19  
 import org.slf4j.LoggerFactory;
 20  
 import org.slf4j.Logger;
 21  
 
 22  
 import java.util.Collection;
 23  
 import java.util.HashMap;
 24  
 import java.util.HashSet;
 25  
 import java.util.Map;
 26  
 import java.util.Map.Entry;
 27  
 import java.util.Set;
 28  
 
 29  
 /**
 30  
  * Internal class that manages the Dozer caches. Only intended for internal use.
 31  
  * 
 32  
  * @author tierney.matt
 33  
  */
 34  1144
 public final class DozerCacheManager implements CacheManager {
 35  
 
 36  1144
   private final Logger log = LoggerFactory.getLogger(DozerCacheManager.class);
 37  
 
 38  1144
   private final Map<String, Cache> cachesMap = new HashMap<String, Cache>();
 39  
 
 40  
   public Collection<Cache> getCaches() {
 41  1
     return new HashSet<Cache>(cachesMap.values());
 42  
   }
 43  
 
 44  
   public Cache getCache(String name) {
 45  264124
     Cache cache = cachesMap.get(name);
 46  264124
     if (cache == null) {
 47  1
       MappingUtils.throwMappingException("Unable to find cache with name: " + name);
 48  
     }
 49  264123
     return cache;
 50  
   }
 51  
 
 52  
   public void addCache(String name, int maxElementsInMemory) {
 53  2275
     addCache(new DozerCache(name, maxElementsInMemory));
 54  2274
   }
 55  
 
 56  
   public void addCache(Cache cache) {
 57  2278
     synchronized (cachesMap) {
 58  2278
       String name = cache.getName();
 59  2278
       if (cacheExists(name)) {
 60  1
         MappingUtils.throwMappingException("Cache already exists with name: " + name);
 61  
       }
 62  2277
       cachesMap.put(name, cache);
 63  2277
     }
 64  2277
   }
 65  
 
 66  
   public Collection<String> getCacheNames() {
 67  1
     Set<String> results = new HashSet<String>();
 68  1
     for (Entry<String, Cache> entry : cachesMap.entrySet()) {
 69  2
       results.add(entry.getKey());
 70  2
     }
 71  1
     return results;
 72  
   }
 73  
 
 74  
   /*
 75  
    * Dont clear keys in caches map because these are only added 1 time at startup. Only clear cache entries for each cache
 76  
    */
 77  
   public void clearAllEntries() {
 78  1
     for (Cache cache : cachesMap.values()) {
 79  1
       cache.clear();
 80  1
     }
 81  1
   }
 82  
 
 83  
   public boolean cacheExists(String name) {
 84  2282
     return cachesMap.containsKey(name);
 85  
   }
 86  
 
 87  
   public void logCaches() {
 88  0
     log.info(getCaches().toString());
 89  0
   }
 90  
   
 91  
 }