Coverage Report - net.admin4j.timer.RollingNbrObservationsDataMeasure
 
Classes in this File Line Coverage Branch Coverage Complexity
RollingNbrObservationsDataMeasure
78%
44/56
87%
14/16
1.571
 
 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.ArrayList;
 17  
 import java.util.Date;
 18  
 import java.util.List;
 19  
 
 20  
 import net.admin4j.vo.DataMeasurementSummaryVO;
 21  
 
 22  
 /**
 23  
  * Keeps the last given number of observations of a series of data measurements.
 24  
  * @author D. Ashmore
 25  
  * @since 1.0
 26  
  */
 27  
 public class RollingNbrObservationsDataMeasure implements DataMeasure {
 28  
     
 29  
     private static final long serialVersionUID = -240040580088773258L;
 30  
     public static final int DEFAULT_NUMBER_ROLLING_OBSERVATIONS=100;
 31  198
     private int nbrRollingObservations = DEFAULT_NUMBER_ROLLING_OBSERVATIONS;
 32  
     private Number[] observations;
 33  
     private Long[] observationTime;
 34  198
     private int currentObservationOffset = 0;
 35  
     
 36  
     public RollingNbrObservationsDataMeasure() {
 37  198
         this(DEFAULT_NUMBER_ROLLING_OBSERVATIONS);
 38  198
     }
 39  
     
 40  198
     public RollingNbrObservationsDataMeasure(int nbrRollingObservations) {
 41  198
         this.nbrRollingObservations = nbrRollingObservations;
 42  198
         this.observations = new Number[this.nbrRollingObservations];
 43  198
         this.observationTime = new Long[this.nbrRollingObservations];
 44  198
     }
 45  
 
 46  
     public void addNumber(Number number) {
 47  1500030
         synchronized (observations) {
 48  1500030
             if (this.currentObservationOffset >= this.observations.length) {
 49  14997
                 this.currentObservationOffset = 0;
 50  
             }
 51  1500030
             this.observations[this.currentObservationOffset] = number;
 52  1500030
             this.observationTime[this.currentObservationOffset] = System.currentTimeMillis();
 53  1500030
             this.currentObservationOffset++;
 54  1500030
         }
 55  
 
 56  1500030
     }
 57  
 
 58  
     public DataMeasurementSummaryVO getDataMeasurementSummary() {
 59  6
         List<Number> valueList = new ArrayList<Number>();
 60  6
         long firstObsTime = Long.MAX_VALUE;
 61  6
         long lastObsTime = Long.MIN_VALUE;
 62  
         
 63  6
         synchronized (observations) {
 64  606
             for (int i = 0; i < observations.length; i++) {
 65  600
                 if (observations[i] != null) {
 66  6
                     valueList.add(observations[i]);
 67  6
                     if (this.observationTime[i] < firstObsTime) {
 68  3
                         firstObsTime = this.observationTime[i];
 69  
                     }
 70  6
                     if (this.observationTime[i] > lastObsTime) {
 71  3
                         lastObsTime = this.observationTime[i];
 72  
                     }
 73  
                 }
 74  
             }
 75  6
         }
 76  6
         DataMeasurementSummaryVO summary = DataMeasurementSummaryVO.fromValueList(valueList);
 77  6
         summary.setLabel("Last " + this.nbrRollingObservations + " observations");
 78  6
         summary.setSummaryType(DataMeasurementSummaryVO.SummaryType.ROLLING_NBR_OBS);
 79  
         
 80  6
         if (firstObsTime < Long.MAX_VALUE) {
 81  3
             summary.setFirstObservationDate(new Date(firstObsTime));
 82  
         }
 83  6
         if (lastObsTime > Long.MIN_VALUE) {
 84  3
             summary.setLastObservationDate(new Date(lastObsTime));
 85  
         }
 86  6
         return summary;
 87  
     }
 88  
 
 89  
     public void purgeObsoleteObservations() {
 90  
         // NoOp
 91  
 
 92  3
     }
 93  
 
 94  
     public void reset() {
 95  0
         synchronized (observations) {
 96  0
             this.currentObservationOffset = 0;
 97  0
             for (int i = 0; i < observations.length; i++) {
 98  0
                 observations[i] = null;
 99  
             }
 100  0
         }
 101  
 
 102  0
     }
 103  
 
 104  
     public int getNbrRollingObservations() {
 105  18
         return nbrRollingObservations;
 106  
     }
 107  
 
 108  
     public void setNbrRollingObservations(int nbrRollingObservations) {
 109  0
         this.nbrRollingObservations = nbrRollingObservations;
 110  0
     }
 111  
 
 112  
     public Number[] getObservations() {
 113  189
         return observations;
 114  
     }
 115  
 
 116  
     public void setObservations(Number[] observations) {
 117  0
         this.observations = observations;
 118  0
     }
 119  
 
 120  
     public Long[] getObservationTime() {
 121  189
         return observationTime;
 122  
     }
 123  
 
 124  
     public void setObservationTime(Long[] observationTime) {
 125  0
         this.observationTime = observationTime;
 126  0
     }
 127  
 
 128  
     public int getCurrentObservationOffset() {
 129  18
         return currentObservationOffset;
 130  
     }
 131  
 
 132  
     public void setCurrentObservationOffset(int currentObservationOffset) {
 133  177
         this.currentObservationOffset = currentObservationOffset;
 134  177
     }
 135  
 
 136  
 }