1 3 package jodd.datetime; 4 5 import java.util.List ; 6 import java.util.ArrayList ; 7 8 12 public class JStopWatch { 13 14 17 protected String name; 18 21 protected long startTime; 22 25 protected long stopTime; 26 29 protected long spanTime; 30 33 protected long totalTime; 34 37 protected boolean running; 38 39 41 44 public JStopWatch() { 45 this(""); 46 } 47 48 51 public JStopWatch(String name) { 52 this.name = name; 53 start(); 54 } 55 56 59 public String getName() { 60 return name; 61 } 62 63 66 public boolean isRunning() { 67 return running; 68 } 69 70 72 76 public long start() { 77 if (running == false) { 78 startTime = System.currentTimeMillis(); 79 running = true; 80 } 81 return startTime; 82 } 83 84 87 public long restart() { 88 startTime = System.currentTimeMillis(); 89 running = true; 90 return startTime; 91 } 92 93 97 public long stop() { 98 if (running == true) { 99 stopTime = System.currentTimeMillis(); 100 if (laps != null) { 101 lap(stopTime); 102 } 103 spanTime = stopTime - startTime; 104 totalTime += stopTime - startTime; 105 running = false; 106 } 107 return spanTime; 108 } 109 110 113 public long elapsed() { 114 return System.currentTimeMillis() - startTime; 115 } 116 117 120 public long total() { 121 stop(); 122 return totalTime; 123 } 124 125 129 public long span() { 130 stop(); 131 return spanTime; 132 } 133 134 135 137 145 protected List laps; 146 147 150 public long lap() { 151 return lap(System.currentTimeMillis()); 152 } 153 154 protected long lap(long lap) { 155 if (running == false) { 156 return 0; 157 } 158 long lapSpanTime = lap - startTime; 159 long lapTime; 160 if (laps == null) { 161 lapTime = lapSpanTime; 162 laps = new ArrayList (); 163 } else { 164 long[] previous = (long[]) laps.get(laps.size() - 1); 165 lapTime = lap - previous[2]; 166 } 167 laps.add(new long[] {lapTime, lapSpanTime, lap}); 168 return lapTime; 169 } 170 171 174 public int totalLaps() { 175 if (laps == null) { 176 return 0; 177 } 178 return laps.size(); 179 } 180 181 185 public long[] getLapTimes(int index) { 186 if (laps == null) { 187 return null; 188 } 189 if ((index <= 0) || (index > laps.size())) { 190 return null; 191 } 192 return (long[]) laps.get(index - 1); 193 } 194 195 197 200 public String toString() { 201 long elapsed = elapsed(); 202 StringBuffer sb = new StringBuffer (); 203 sb.append("JStopWatch ").append(name).append(running ? " is running." : "").append('\n'); 204 if (running == true) { 205 sb.append("elapsed: ").append(formatTimeSpan(elapsed)); 206 } else { 207 if (spanTime != totalTime) { 208 sb.append("span: ").append(formatTimeSpan(spanTime)).append('\n'); 209 } 210 sb.append("total: ").append(formatTimeSpan(totalTime)); 211 } 212 if (laps != null) { 213 if (laps.isEmpty() == false) { 214 sb.append('\n'); 215 } 216 for (int i = 0; i < laps.size(); i++) { 217 long[] longs = (long[]) laps.get(i); 218 sb.append(" lap #").append(i + 1).append(':').append('\t'); 219 sb.append(formatTimeSpan(longs[0])).append('\t'); 220 sb.append(formatTimeSpan(longs[1])).append('\n'); 221 } 222 } 223 return sb.toString(); 224 } 225 226 229 public static String formatTimeSpan(long millis) { 230 long seconds = 0; 231 long minutes = 0; 232 long hours = 0; 233 234 if (millis > 1000) { 235 seconds = millis / 1000; 236 millis %= 1000; 237 } 238 if (seconds > 60) { 239 minutes = seconds / 60; 240 seconds %= 60; 241 } 242 if (minutes > 60) { 243 hours = minutes / 60; 244 minutes %= 60; 245 } 246 247 StringBuffer result = new StringBuffer (20); 248 boolean out = false; 249 if (hours > 0) { 250 result.append(hours).append(':'); 251 out = true; 252 } 253 if ((out == true) || (minutes > 0)) { 254 if (minutes < 10) { 255 result.append('0'); 256 } 257 result.append(minutes).append(':'); 258 } 259 260 if (seconds < 10) { 261 result.append('0'); 262 } 263 result.append(seconds).append('.'); 264 265 if (millis < 10) { 266 result.append('0'); 267 } 268 if (millis < 100) { 269 result.append('0'); 270 } 271 result.append(millis); 272 return result.toString(); 273 } 274 } 275 | Popular Tags |