CheckPointing.java [src/csip/utils] Revision: Date:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package csip.utils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Timed check pointing.
*
* @author od
*/
public class CheckPointing {
static final String ROW = "[%1$tH:%1$tM:%1$tS.%1$tL] %2$5d ms (total: %3$5d ms) - %4$s\n";
List<CP> cps = new ArrayList<>();
static class CP {
Date time;
String msg;
CP(Date time, String msg) {
this.time = time;
this.msg = msg;
}
}
public String check(String msg) {
CP cp = new CP(new Date(), msg);
cps.add(cp);
return fmt(cp, cps.size() - 1);
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
for (int i = 0; i < cps.size(); i++) {
s.append(fmt(cps.get(i), i));
}
return s.toString();
}
private String fmt(CP cp, int i) {
long diff = (i > 0) ? (cp.time.getTime() - cps.get(i - 1).time.getTime()) : 0;
long abs = (i > 0) ? (cp.time.getTime() - cps.get(0).time.getTime()) : 0;
return String.format(ROW, cp.time, diff, abs, cp.msg);
}
}