Coverage Report - org.dozer.stats.StatisticsManagerImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
StatisticsManagerImpl
94%
37/39
85%
12/14
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.stats;
 17  
 
 18  
 import org.dozer.config.GlobalSettings;
 19  
 import org.slf4j.LoggerFactory;
 20  
 import org.slf4j.Logger;
 21  
 
 22  
 import java.util.Collections;
 23  
 import java.util.HashSet;
 24  
 import java.util.Map.Entry;
 25  
 import java.util.Set;
 26  
 import java.util.concurrent.ConcurrentHashMap;
 27  
 import java.util.concurrent.ConcurrentMap;
 28  
 
 29  
 /**
 30  
  * Internal class that manages the Dozer runtime statistics. Only intended for internal use.
 31  
  *
 32  
  * @author tierney.matt
 33  
  */
 34  12
 public final class StatisticsManagerImpl implements StatisticsManager {
 35  
 
 36  12
   private final Logger log = LoggerFactory.getLogger(StatisticsManagerImpl.class);
 37  
 
 38  12
   private final ConcurrentMap<StatisticType, Statistic> statisticsMap = new ConcurrentHashMap<StatisticType, Statistic>();
 39  12
   private boolean isStatisticsEnabled = GlobalSettings.getInstance().isStatisticsEnabled();
 40  
 
 41  
   public void clearAll() {
 42  3
     statisticsMap.clear();
 43  3
   }
 44  
 
 45  
   @SuppressWarnings("unchecked")
 46  
   public Set<StatisticEntry> getStatisticEntries(StatisticType statisticType) {
 47  1
     Statistic statistic = statisticsMap.get(statisticType);
 48  1
     return statistic != null ? statistic.getEntries() : Collections.EMPTY_SET;
 49  
   }
 50  
 
 51  
   public Set<Statistic> getStatistics() {
 52  3
     return new HashSet<Statistic>(statisticsMap.values());
 53  
   }
 54  
 
 55  
   public boolean isStatisticsEnabled() {
 56  655316
     return isStatisticsEnabled;
 57  
   }
 58  
 
 59  
   public void setStatisticsEnabled(boolean statisticsEnabled) {
 60  12
     this.isStatisticsEnabled = statisticsEnabled;
 61  12
     GlobalSettings.getInstance().setStatisticsEnabled(statisticsEnabled);
 62  12
   }
 63  
 
 64  
   public Set<StatisticType> getStatisticTypes() {
 65  1
     Set<StatisticType> results = new HashSet<StatisticType>();
 66  1
     for (Entry<StatisticType, Statistic> entry : statisticsMap.entrySet()) {
 67  2
       results.add(entry.getKey());
 68  2
     }
 69  1
     return results;
 70  
   }
 71  
 
 72  
   /*
 73  
    * Convenience method that should only be used for statistic types that will only ever have 1 statistic entry(value).
 74  
    * For stats that only have one entry, it is assumed that the single entry's key is the same as the stat type name
 75  
    */
 76  
 
 77  
   public Statistic increment(StatisticType statisticType) {
 78  181388
     return increment(statisticType, 1);
 79  
   }
 80  
 
 81  
   public Statistic increment(StatisticType statisticType, long value) {
 82  181529
     return increment(statisticType, statisticType, value);
 83  
   }
 84  
 
 85  
   public Statistic increment(StatisticType statisticType, Object statisticEntryKey) {
 86  341721
     return increment(statisticType, statisticEntryKey, 1);
 87  
   }
 88  
 
 89  
   protected Statistic increment(StatisticType statisticType, Object statisticEntryKey, long value) {
 90  
     // If statistics are not enabled, just return and do nothing.
 91  523255
     if (!isStatisticsEnabled()) {
 92  483242
       return null;
 93  
     }
 94  
 
 95  40013
     if (statisticType == null) {
 96  1
       throw new IllegalArgumentException("statistic type must be specified");
 97  
     }
 98  
 
 99  
     // Get Statistic object for the specified type. If it doesn't already exist, create it
 100  40012
     Statistic statistic = statisticsMap.get(statisticType);
 101  40012
     if (statistic == null) {
 102  11
       Statistic newStatistic = new Statistic(statisticType);
 103  11
       statistic = statisticsMap.putIfAbsent(statisticType, newStatistic);
 104  11
       if (statistic == null) {
 105  11
         statistic = newStatistic;
 106  
       }
 107  
     }
 108  
 
 109  
     // increment the statistic
 110  40012
     statistic.increment(statisticEntryKey, value);
 111  40011
     return statistic;
 112  
   }
 113  
 
 114  
   protected Statistic getStatistic(StatisticType statisticType) {
 115  2
     return statisticsMap.get(statisticType);
 116  
   }
 117  
 
 118  
   /*
 119  
    * Convenience method that should only be used for statistic types that will only ever have 1 statistic entry(value).
 120  
    * getStatisticEntries() should be used for statistic types that have more than 1 statistic entry(value)
 121  
    */
 122  
 
 123  
   public long getStatisticValue(StatisticType statisticType) {
 124  9
     return getStatisticValue(statisticType, statisticType);
 125  
   }
 126  
 
 127  
   public long getStatisticValue(StatisticType statisticType, Object entryKey) {
 128  213
     Statistic statistic = statisticsMap.get(statisticType);
 129  213
     return statistic != null ? statistic.getStatisticValue(entryKey) : 0;
 130  
   }
 131  
 
 132  
   public void logStatistics() {
 133  0
     log.info(getStatistics().toString());
 134  0
   }
 135  
 
 136  
 }