| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 31 | |
|
| 32 | |
|
| 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 | |
|
| 74 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 120 | |
|
| 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 | |
} |