| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.dozer.stats; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang3.builder.EqualsBuilder; |
| 19 | |
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; |
| 20 | |
import org.apache.commons.lang3.builder.ToStringStyle; |
| 21 | |
|
| 22 | |
import java.util.HashSet; |
| 23 | |
import java.util.Set; |
| 24 | |
import java.util.concurrent.ConcurrentHashMap; |
| 25 | |
import java.util.concurrent.ConcurrentMap; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public class Statistic { |
| 35 | |
|
| 36 | |
private final StatisticType type; |
| 37 | 19 | private final ConcurrentMap<Object, StatisticEntry> entriesMap = new ConcurrentHashMap<Object, StatisticEntry>(); |
| 38 | |
|
| 39 | 19 | public Statistic(StatisticType type) { |
| 40 | 19 | this.type = type; |
| 41 | 19 | } |
| 42 | |
|
| 43 | |
public StatisticType getType() { |
| 44 | 3 | return type; |
| 45 | |
} |
| 46 | |
|
| 47 | |
public StatisticEntry increment(Object statisticEntryKey, long value) { |
| 48 | 40021 | if (statisticEntryKey == null) { |
| 49 | 2 | throw new IllegalArgumentException("statistic entry key must be specified"); |
| 50 | |
} |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 40019 | StatisticEntry statisticEntry = entriesMap.get(statisticEntryKey); |
| 55 | 40019 | if (statisticEntry == null) { |
| 56 | 493 | StatisticEntry newStatisticEntry = new StatisticEntry(statisticEntryKey); |
| 57 | 493 | statisticEntry = entriesMap.putIfAbsent(statisticEntryKey, newStatisticEntry); |
| 58 | 493 | if (statisticEntry == null) { |
| 59 | 218 | statisticEntry = newStatisticEntry; |
| 60 | |
} |
| 61 | |
} |
| 62 | |
|
| 63 | 40019 | statisticEntry.increment(value); |
| 64 | 40019 | return statisticEntry; |
| 65 | |
} |
| 66 | |
|
| 67 | |
public void clear() { |
| 68 | 1 | entriesMap.clear(); |
| 69 | 1 | } |
| 70 | |
|
| 71 | |
public Set<StatisticEntry> getEntries() { |
| 72 | 8 | return new HashSet<StatisticEntry>(entriesMap.values()); |
| 73 | |
} |
| 74 | |
|
| 75 | |
public StatisticEntry getEntry(Object entryKey) { |
| 76 | 7 | return entriesMap.get(entryKey); |
| 77 | |
} |
| 78 | |
|
| 79 | |
public long getStatisticValue(Object entryKey) { |
| 80 | 206 | StatisticEntry statisticEntry = entriesMap.get(entryKey); |
| 81 | 206 | return statisticEntry != null ? statisticEntry.getValue() : 0; |
| 82 | |
} |
| 83 | |
|
| 84 | |
@Override |
| 85 | |
public boolean equals(Object object) { |
| 86 | 2 | if ((this == object)) { |
| 87 | 1 | return true; |
| 88 | |
} |
| 89 | 1 | if (!(object instanceof Statistic)) { |
| 90 | 0 | return false; |
| 91 | |
} |
| 92 | 1 | Statistic entry = (Statistic) object; |
| 93 | 1 | return new EqualsBuilder().append(this.getType(), entry.getType()).isEquals(); |
| 94 | |
} |
| 95 | |
|
| 96 | |
@Override |
| 97 | |
public int hashCode() { |
| 98 | 5 | return type.hashCode(); |
| 99 | |
} |
| 100 | |
|
| 101 | |
@Override |
| 102 | |
public String toString() { |
| 103 | 0 | return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE); |
| 104 | |
} |
| 105 | |
} |