Coverage Report - net.admin4j.timer.BasicTaskTimer
 
Classes in this File Line Coverage Branch Coverage Complexity
BasicTaskTimer
94%
16/17
75%
3/4
1.5
 
 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.Collection;
 17  
 
 18  
 import net.admin4j.deps.commons.lang3.Validate;
 19  
 import net.admin4j.util.Admin4jRuntimeException;
 20  
 
 21  
 /**
 22  
  * Basic TaskTimer implementation.
 23  
  * @author D. Ashmore
 24  
  * @since 1.0
 25  
  */
 26  
 public class BasicTaskTimer implements TaskTimer {
 27  
     
 28  
     private static final long serialVersionUID = -6898638441458870161L;
 29  
     private Collection<DataMeasure> dataMeasures;
 30  237
     private ThreadLocal<Long> beginTime = new ThreadLocal<Long>();
 31  
     private String label;
 32  
     
 33  237
     public BasicTaskTimer(String label, Collection<DataMeasure> dataMeasures) {
 34  
         Validate.notNull(dataMeasures, "Null data measure collection not allowed.");
 35  
         Validate.notEmpty(label, "Null or blank label not allowed.");
 36  237
         this.dataMeasures = dataMeasures;
 37  237
         this.label = label;
 38  237
     }
 39  
 
 40  
     /* (non-Javadoc)
 41  
      * @see net.admin4j.timer.TaskTimer#stop()
 42  
      */
 43  
     public void stop() {
 44  3000030
         Long bTime = this.beginTime.get();
 45  3000030
         if (bTime == null) {
 46  0
             throw new Admin4jRuntimeException("Can't call stop() on a timer that hasn't been started.");            
 47  
         }
 48  
         
 49  3000030
         Long timing = System.currentTimeMillis() - bTime;
 50  3000030
         for (DataMeasure measure: this.dataMeasures) {
 51  4500060
             measure.addNumber(timing);
 52  
         }
 53  
 
 54  3000030
     }
 55  
 
 56  
     /* (non-Javadoc)
 57  
      * @see net.admin4j.timer.TaskTimer#start()
 58  
      */
 59  
     public void start() {
 60  3180171
        this.beginTime.set(System.currentTimeMillis());
 61  
         
 62  3180171
     }
 63  
 
 64  
     /* (non-Javadoc)
 65  
      * @see net.admin4j.timer.TaskTimer#getLabel()
 66  
      */
 67  
     public String getLabel() {
 68  270
         return label;
 69  
     }
 70  
 
 71  
     public Collection<DataMeasure> getDataMeasures() {
 72  180482
         return dataMeasures;
 73  
     }
 74  
     
 75  
     protected Long getBeginTime() {
 76  180138
         return this.beginTime.get();
 77  
     }
 78  
 
 79  
 }