1 package com.protomatter.util; 2 3 52 53 75 public class OpTimer 76 implements java.io.Serializable  77 { 78 private String name = null; 79 private long start = 0; 80 private long stop = 0; 81 private boolean showThread = true; 82 private String threadName = null; 83 84 88 public OpTimer(String name) 89 { 90 this(name, true); 91 } 92 93 97 public OpTimer(String name, boolean showThread) 98 { 99 this.name = name; 100 this.showThread = showThread; 101 start(); 102 } 103 104 109 public OpTimer start() 110 { 111 this.start = System.currentTimeMillis(); 112 this.stop = 0; 113 if (showThread) 114 this.threadName = Thread.currentThread().getName(); 115 return this; 116 } 117 118 121 public long elapsed() 122 { 123 return (this.stop != 0) 124 ? (this.stop - this.start) 125 : (System.currentTimeMillis() - this.start); 126 } 127 128 131 public String getName() 132 { 133 return this.name; } 134 135 138 public void setName(String name) 139 { 140 this.name = name; 141 } 142 143 146 public long startTime() 147 { 148 return this.start; 149 } 150 151 156 public long stopTime() 157 { 158 return this.stop; 159 } 160 161 164 public OpTimer stop() 165 { 166 this.stop = System.currentTimeMillis(); 167 return this; 168 } 169 170 public String toString() 171 { 172 long now = System.currentTimeMillis(); 173 return "OpTimer[" + this.name + ", " + 174 (showThread ? ("thread=" + threadName + ", ") : "") + 175 ((stop == 0) ? "still running, " : "") + 176 "took " + 177 ((this.stop != 0) 178 ? (this.stop - this.start) 179 : (now - this.start)) + 180 "ms]"; 181 } 182 } 183
| Popular Tags
|