Coverage Report - org.dozer.stats.Statistic
 
Classes in this File Line Coverage Branch Coverage Complexity
Statistic
93%
27/29
91%
11/12
1.9
 
 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.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  
  * Internal class that provides an interface to a single statistic. Holds all of the statistic entries for the
 29  
  * statistic. Only intended for internal use.
 30  
  *
 31  
  * @author tierney.matt
 32  
  * @author dmitry.buzdin
 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  
     // Get the StatisticEntry object which contains the actual value.
 53  
     // If it doesn't already exist, create it so that it can be incremented
 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  
     // Increment the actual value
 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  
 }