Coverage Report - net.admin4j.timer.SummaryDataMeasure
 
Classes in this File Line Coverage Branch Coverage Complexity
SummaryDataMeasure
89%
53/59
100%
6/6
1.15
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.timer;
 15  
 
 16  
 import java.util.Date;
 17  
 
 18  
 import net.admin4j.deps.commons.lang3.Validate;
 19  
 import net.admin4j.vo.DataMeasurementSummaryVO;
 20  
 
 21  
 /**
 22  
  * Responsible for keeping numerical time series data.
 23  
  * 
 24  
  * <p>Note:  This implementation is intended to be threadsafe.</p>
 25  
  * @author D. Ashmore
 26  
  * @since 1.0
 27  
  */
 28  
 public class SummaryDataMeasure implements DataMeasure {
 29  
     
 30  
     private static final long serialVersionUID = 1054824969202050369L;
 31  1500324
     private double total = 0.0D;
 32  1500324
     private double sumOfSquares = 0.0D;
 33  1500324
     private long nbrDataItems = 0L;
 34  1500324
     private double minimum = Double.MAX_VALUE;
 35  1500324
     private double maximum = Double.MIN_VALUE;
 36  
     private long firstObservationTime;
 37  
     private long lastObservationTime;
 38  
     
 39  
     /**
 40  
      * @deprecated - Only provided so reading of earlier version performance stats works.
 41  
      */
 42  
     public SummaryDataMeasure() {
 43  1500165
         this(System.currentTimeMillis());
 44  1500165
     }
 45  
     
 46  1500324
     public SummaryDataMeasure(long firstObservationTime) {
 47  1500324
         this.firstObservationTime = firstObservationTime;
 48  1500324
     }
 49  
     
 50  
     /* (non-Javadoc)
 51  
      * @see net.admin4j.timer.DataMeasure#reset()
 52  
      */
 53  
     public synchronized void  reset() {
 54  0
         total = 0.0D;
 55  0
         sumOfSquares = 0.0D;
 56  0
         nbrDataItems = 0L;
 57  0
         minimum = Double.MAX_VALUE;
 58  0
         maximum = Double.MIN_VALUE;
 59  0
     }
 60  
     
 61  
     /* (non-Javadoc)
 62  
      * @see net.admin4j.timer.DataMeasure#addNumber(java.lang.Number)
 63  
      */
 64  
     public synchronized void addNumber(Number number) {
 65  
         Validate.notNull(number, "Null number not allowed");
 66  3180258
         this.lastObservationTime = System.currentTimeMillis();
 67  
         
 68  3180258
         total += number.doubleValue();
 69  3180258
         sumOfSquares += Math.pow(number.doubleValue(), 2);
 70  3180258
         nbrDataItems++;
 71  3180258
         if (number.doubleValue() < minimum)  minimum = number.doubleValue();
 72  3180258
         if (number.doubleValue() > maximum)  maximum = number.doubleValue();
 73  3180258
     }
 74  
  
 75  
 
 76  
     /* (non-Javadoc)
 77  
      * @see net.admin4j.timer.DataMeasure#purgeObsoleteObservations()
 78  
      */
 79  
     public void purgeObsoleteObservations() {
 80  
         // No Op
 81  
         
 82  3
     }
 83  
 
 84  
     /* (non-Javadoc)
 85  
      * @see net.admin4j.timer.DataMeasure#getDataMeasurementSummary()
 86  
      */
 87  
     public synchronized DataMeasurementSummaryVO getDataMeasurementSummary() {
 88  55
         DataMeasurementSummaryVO summary = new DataMeasurementSummaryVO();
 89  55
         summary.setLabel("Summary for all observations");
 90  55
         summary.setSummaryType(DataMeasurementSummaryVO.SummaryType.SUMMARY);        
 91  55
         summary.setNbrDataItems(this.nbrDataItems);
 92  55
         summary.setTotal(this.total);
 93  55
         if (nbrDataItems > 0) {
 94  52
             summary.setMinimum(this.minimum);
 95  52
             summary.setMaximum(this.maximum);
 96  52
             summary.setAverage(total / (double)nbrDataItems);
 97  52
             summary.setVariance( (this.sumOfSquares / (double)this.nbrDataItems) - Math.pow(summary.getAverage(), 2));
 98  52
             summary.setStandardDeviation(Math.sqrt(summary.getVariance()));
 99  52
             summary.setFirstObservationDate(new Date(this.firstObservationTime));
 100  52
             summary.setLastObservationDate(new Date(this.lastObservationTime));
 101  
         }
 102  
        
 103  55
         return summary;
 104  
     }
 105  
 
 106  
     public double getTotal() {
 107  358
         return total;
 108  
     }
 109  
 
 110  
     public void setTotal(double total) {
 111  171
         this.total = total;
 112  171
     }
 113  
 
 114  
     public double getSumOfSquares() {
 115  18
         return sumOfSquares;
 116  
     }
 117  
 
 118  
     public void setSumOfSquares(double sumOfSquares) {
 119  171
         this.sumOfSquares = sumOfSquares;
 120  171
     }
 121  
 
 122  
     public long getNbrDataItems() {
 123  18
         return nbrDataItems;
 124  
     }
 125  
 
 126  
     public void setNbrDataItems(long nbrDataItems) {
 127  177
         this.nbrDataItems = nbrDataItems;
 128  177
     }
 129  
 
 130  
     public double getMinimum() {
 131  510
         return minimum;
 132  
     }
 133  
 
 134  
     public void setMinimum(double minimum) {
 135  177
         this.minimum = minimum;
 136  177
     }
 137  
 
 138  
     public double getMaximum() {
 139  18
         return maximum;
 140  
     }
 141  
 
 142  
     public void setMaximum(double maximum) {
 143  171
         this.maximum = maximum;
 144  171
     }
 145  
 
 146  
     public long getFirstObservationTime() {
 147  240
         return firstObservationTime;
 148  
     }
 149  
 
 150  
     public void setFirstObservationTime(long firstObservationTime) {
 151  165
         this.firstObservationTime = firstObservationTime;
 152  165
     }
 153  
 
 154  
     public long getLastObservationTime() {
 155  82
         return lastObservationTime;
 156  
     }
 157  
 
 158  
     public void setLastObservationTime(long lastObservationTime) {
 159  177
         this.lastObservationTime = lastObservationTime;
 160  177
     }
 161  
 
 162  
 }