Coverage report

  %line %branch
net.sf.infrared.base.model.ExecutionTimer
85% 
100% 

 1  
 /* 
 2  
  * Copyright 2005 Tavant Technologies and Contributors
 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  
  *
 17  
  *
 18  
  * Original Author:  binil.thoms (Tavant Technologies)
 19  
  * Contributor(s):   -;
 20  
  *
 21  
  */
 22  
 package net.sf.infrared.base.model;
 23  
 
 24  
 import java.io.Serializable;
 25  
 
 26  
 /**
 27  
  * Timer used to time an execution. Precision is of millisecond order.
 28  
  * 
 29  
  * <p>
 30  
  * ExecutionTimers are created with an ExecutionContext, which represents the execution that 
 31  
  * this timer times. start(), stop() methods are called before and after the execution.
 32  
  * <p>
 33  
  * This timer calculates the time that elapsed between start() and stop() calls. This is the
 34  
  * inclusive time of the execution i.e. time which includes time spend in other executions that
 35  
  * happened as part of this execution. setExclusiveTime() method should be used to set the 
 36  
  * time taken exclusively by an execution i.e. time spend <strong>only</strong> in this execution
 37  
  * ignoring time spend in other executions which happened as part of this one.
 38  
  * 
 39  
  * @author binil.thomas
 40  
  */
 41  10
 public class ExecutionTimer implements Serializable {
 42  
     private ExecutionContext ctx;
 43  
     
 44  45
     private long startTime = -1;
 45  
 
 46  45
     private long endTime = -1;
 47  
     
 48  45
     private long inclusiveTime = 0;
 49  
     
 50  45
     private long exclusiveTime = 0;
 51  
     
 52  
     private String layerName;
 53  
     
 54  
     /**
 55  
      * Creates a new timer to time the execution represented by the given ExecutionContext.
 56  
      * 
 57  
      * @param ctx
 58  
      */
 59  43
     public ExecutionTimer(ExecutionContext ctx) {
 60  43
         this.ctx = ctx;
 61  43
     }
 62  
     
 63  2
     ExecutionTimer() {
 64  2
     }
 65  
 
 66  
     /**
 67  
      * Starts the timer.
 68  
      *
 69  
      * @throws IllegalArgumentException if start() is already called.
 70  
      */
 71  
     public void start() {
 72  12
         assert !isExecuting() : "Cannot start an already started ExecutionTimer";
 73  12
         startTime = getCurrentTime();
 74  12
     }
 75  
 
 76  
     /**
 77  
      * Stops the timer.
 78  
      *
 79  
      * @throws IllegalArgumentException if start() is not called yet, or if a start() & stop() pair 
 80  
      * had been called already
 81  
      */
 82  
     public void stop() {
 83  12
         assert isExecuting(): "Cannot stop an ExecutionTimer which hasnt started";
 84  12
         endTime = getCurrentTime();
 85  12
         inclusiveTime = endTime - startTime;
 86  12
     }
 87  
     
 88  
     /**
 89  
      * Sets the exclusive time.
 90  
      * 
 91  
      * <p>
 92  
      * Exclusive time is the time spend <strong>only</strong> in this execution,
 93  
      * ignoring time spend in other executions which happened as part of this one.
 94  
      * 
 95  
      * @throws IllegalArgumentException if an attempt is made to set exclsuive time 
 96  
      * greater than inclusive time.
 97  
      */
 98  
     public void setExclusiveTime(long time) {
 99  
         assert (getInclusiveTime() < time) : "exclusive time " + time + 
 100  10
                     " cannot be greater than inclusive time " + inclusiveTime;
 101  10
         this.exclusiveTime = time;
 102  10
     }
 103  
     
 104  
     /**
 105  
      * Gets the inclusive time.
 106  
      * 
 107  
      * <p>
 108  
      * Inclusive time is the time that elapses between a pair of start() & stop() calls.
 109  
      */
 110  
     public long getInclusiveTime() {
 111  14
         return inclusiveTime;
 112  
     }
 113  
 
 114  
     /**
 115  
      * Gets the system time when stop() was called.
 116  
      */
 117  
     public long getEndTime() {
 118  0
         return endTime;
 119  
     }
 120  
 
 121  
     /**
 122  
      * Gets the associated ExecutionContext
 123  
      */
 124  
     public ExecutionContext getContext() {
 125  64
         return ctx;
 126  
     }
 127  
 
 128  
     /**
 129  
      * Gets the exclusive time.
 130  
      */
 131  
     public long getExclusiveTime() {
 132  13
         return exclusiveTime;
 133  
     }
 134  
 
 135  
     /**
 136  
      * Gets the system time when start() was called.
 137  
      */
 138  
     public long getStartTime() {
 139  117
         return startTime;
 140  
     }
 141  
 
 142  
     public String toString() {
 143  0
         return "ExecutionTime for " + ctx + 
 144  
             "(inclusive time = " + inclusiveTime + ", " + 
 145  
             "exclusive time = " + exclusiveTime + ")";
 146  
     }
 147  
     
 148  
     public String getLayerName() {
 149  58
         return this.layerName;
 150  
     }
 151  
     
 152  
     public void setLayerName(String layer) {
 153  0
         this.layerName = layer;                
 154  0
     }
 155  
 
 156  
     protected long getCurrentTime() {
 157  4
         return System.currentTimeMillis();
 158  
     }
 159  
     
 160  
     boolean isExecuting() {
 161  3
         return startTime != -1 && endTime == -1;
 162  
     }
 163  
     
 164  
     boolean hasFinishedExecution() {
 165  0
         return endTime != -1;
 166  
     }
 167  
     
 168  
     void setInclusiveTime(long time) {
 169  5
         this.inclusiveTime = time;
 170  5
     }    
 171  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.