| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 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 | |
|
| 23 | |
|
| 24 | |
|
| 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 | |
|
| 41 | |
|
| 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 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public void start() { |
| 60 | 3180171 | this.beginTime.set(System.currentTimeMillis()); |
| 61 | |
|
| 62 | 3180171 | } |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 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 | |
} |