1 16 package org.apache.commons.lang.time; 17 18 50 public class StopWatch { 51 52 private static final int STATE_UNSTARTED = 0; 54 private static final int STATE_RUNNING = 1; 55 private static final int STATE_STOPPED = 2; 56 private static final int STATE_SUSPENDED = 3; 57 58 private static final int STATE_UNSPLIT = 10; 60 private static final int STATE_SPLIT = 11; 61 62 65 private int runningState = STATE_UNSTARTED; 66 67 70 private int splitState = STATE_UNSPLIT; 71 72 75 private long startTime = -1; 76 79 private long stopTime = -1; 80 81 84 public StopWatch() { 85 } 86 87 94 public void start() { 95 if(this.runningState == STATE_STOPPED) { 96 throw new IllegalStateException ("Stopwatch must be reset before being restarted. "); 97 } 98 if(this.runningState != STATE_UNSTARTED) { 99 throw new IllegalStateException ("Stopwatch already started. "); 100 } 101 stopTime = -1; 102 startTime = System.currentTimeMillis(); 103 this.runningState = STATE_RUNNING; 104 } 105 106 113 public void stop() { 114 if(this.runningState != STATE_RUNNING && this.runningState != STATE_SUSPENDED) { 115 throw new IllegalStateException ("Stopwatch is not running. "); 116 } 117 stopTime = System.currentTimeMillis(); 118 this.runningState = STATE_STOPPED; 119 } 120 121 126 public void reset() { 127 this.runningState = STATE_UNSTARTED; 128 this.splitState = STATE_UNSPLIT; 129 startTime = -1; 130 stopTime = -1; 131 } 132 133 142 public void split() { 143 if(this.runningState != STATE_RUNNING) { 144 throw new IllegalStateException ("Stopwatch is not running. "); 145 } 146 stopTime = System.currentTimeMillis(); 147 this.splitState = STATE_SPLIT; 148 } 149 150 158 public void unsplit() { 159 if(this.splitState != STATE_SPLIT) { 160 throw new IllegalStateException ("Stopwatch has not been split. "); 161 } 162 stopTime = -1; 163 this.splitState = STATE_UNSPLIT; 164 } 165 166 174 public void suspend() { 175 if(this.runningState != STATE_RUNNING) { 176 throw new IllegalStateException ("Stopwatch must be running to suspend. "); 177 } 178 stopTime = System.currentTimeMillis(); 179 this.runningState = STATE_SUSPENDED; 180 } 181 182 190 public void resume() { 191 if(this.runningState != STATE_SUSPENDED) { 192 throw new IllegalStateException ("Stopwatch must be suspended to resume. "); 193 } 194 startTime += (System.currentTimeMillis() - stopTime); 195 stopTime = -1; 196 this.runningState = STATE_RUNNING; 197 } 198 199 207 public long getTime() { 208 if(this.runningState == STATE_STOPPED || this.runningState == STATE_SUSPENDED) { 209 return this.stopTime - this.startTime; 210 } else 211 if(this.runningState == STATE_UNSTARTED) { 212 return 0; 213 } else 214 if(this.runningState == STATE_RUNNING) { 215 return System.currentTimeMillis() - this.startTime; 216 } 217 throw new RuntimeException ("Illegal running state has occured. "); 218 } 219 220 230 public long getSplitTime() { 231 if(this.splitState != STATE_SPLIT) { 232 throw new IllegalStateException ("Stopwatch must be split to get the split time. "); 233 } 234 return this.stopTime - this.startTime; 235 } 236 237 245 public String toString() { 246 return DurationFormatUtils.formatDurationHMS(getTime()); 247 } 248 249 258 public String toSplitString() { 259 return DurationFormatUtils.formatDurationHMS(getSplitTime()); 260 } 261 262 } 263 | Popular Tags |