001: /*
002: * This is free software, licensed under the Gnu Public License (GPL)
003: * get a copy from <http://www.gnu.org/licenses/gpl.html>
004: * $Id: TimeRenderer.java,v 1.8 2005/11/27 16:20:28 hzeller Exp $
005: * author: Henner Zeller <H.Zeller@acm.org>
006: */
007: package henplus.commands;
008:
009: import henplus.OutputDevice;
010: import henplus.AbstractOutputDevice;
011:
012: /**
013: * document me.
014: */
015: public class TimeRenderer {
016: private final static long SECOND_MILLIS = 1000;
017: private final static long MINUTE_MILLIS = 60 * SECOND_MILLIS;
018: private final static long HOUR_MILLIS = 60 * MINUTE_MILLIS;
019:
020: public static void printFraction(long execTime, long number,
021: OutputDevice out) {
022: if (number == 0) {
023: out.print(" -- ");
024: return;
025: }
026: long milli = execTime / number;
027: long micro = (execTime - number * milli) * 1000 / number;
028: printTime(milli, micro, out);
029: }
030:
031: /** render time as string */
032: public static String renderTime(long execTimeInMs) {
033: return renderTime(execTimeInMs, 0);
034: }
035:
036: /** render time as string */
037: public static String renderTime(long execTimeInMs, long usec) {
038: final StringBuffer result = new StringBuffer();
039: printTime(execTimeInMs, usec, new AbstractOutputDevice() {
040: public void print(String s) {
041: result.append(s);
042: }
043: });
044: return result.toString();
045: }
046:
047: /** print time to output device */
048: public static void printTime(long execTimeInMs, OutputDevice out) {
049: printTime(execTimeInMs, 0, out);
050: }
051:
052: /** print time to output device */
053: public static void printTime(long execTimeInMs, long usec,
054: OutputDevice out) {
055: final long totalTime = execTimeInMs;
056:
057: boolean hourPrinted = false;
058: boolean minutePrinted = false;
059:
060: if (execTimeInMs > HOUR_MILLIS) {
061: out.print(String.valueOf(execTimeInMs / HOUR_MILLIS));
062: out.print("h ");
063: execTimeInMs %= HOUR_MILLIS;
064: hourPrinted = true;
065: }
066:
067: if (hourPrinted || execTimeInMs > MINUTE_MILLIS) {
068: long minute = execTimeInMs / 60000;
069: if (hourPrinted && minute < 10) {
070: out.print("0"); // need padding.
071: }
072: out.print(String.valueOf(minute));
073: out.print("m ");
074: execTimeInMs %= MINUTE_MILLIS;
075: minutePrinted = true;
076: }
077:
078: if (minutePrinted || execTimeInMs >= SECOND_MILLIS) {
079: long seconds = execTimeInMs / SECOND_MILLIS;
080: if (minutePrinted && seconds < 10) {
081: out.print("0"); // need padding.
082: }
083: out.print(String.valueOf(seconds));
084: out.print(".");
085: execTimeInMs %= SECOND_MILLIS;
086: // milliseconds
087: if (execTimeInMs < 100)
088: out.print("0");
089: if (execTimeInMs < 10)
090: out.print("0");
091: out.print(String.valueOf(execTimeInMs));
092: } else if (execTimeInMs > 0) {
093: out.print(String.valueOf(execTimeInMs));
094: }
095:
096: if (usec > 0) {
097: if (totalTime > 0) { // need delimiter and padding.
098: out.print(".");
099: if (usec < 100)
100: out.print("0");
101: if (usec < 10)
102: out.print("0");
103: }
104: out.print(String.valueOf(usec));
105: } else if (execTimeInMs == 0) {
106: out.print("0 ");
107: }
108:
109: if (totalTime > MINUTE_MILLIS) {
110: out.print("s");
111: return;
112: } else if (totalTime >= SECOND_MILLIS)
113: out.print(" ");
114: else if (totalTime > 0 && totalTime < SECOND_MILLIS)
115: out.print(" m");
116: else if (totalTime == 0 && usec > 0)
117: out.print(" µ");
118: out.print("sec");
119: }
120: }
121:
122: /*
123: * Local variables:
124: * c-basic-offset: 4
125: * compile-command: "ant -emacs -find build.xml"
126: * End:
127: */
|