Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#271 MillisecondsSpendNumberFormat patch

open
nobody
None
5
2012-11-27
2010-06-04
Geoffrey De Smet
No

I wrote this MillisecondsSpendNumberFormat and used it like this:

NumberAxis xAxis = new NumberAxis("Time millis spend");
xAxis.setAutoRangeIncludesZero(false);
xAxis.setNumberFormatOverride(new MillisecondsSpendNumberFormat());

but I think other can use it too if its part of the jfreechart distribution.

Without it, I get this:
http://2.bp.blogspot.com/_DkCPgdrtcek/S4lj4X6BS5I/AAAAAAAAAGI/VvrkRnwQ2gI/s1600-h/exam_comp_set5Statistic.png
With it, it says 50s instead 50000
and 1m20s instead of 100000

package org.drools.planner.benchmark.statistic;

import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;

/**
* @author Geoffrey De Smet
*/
class MillisecondsSpendNumberFormat extends NumberFormat {

private final static long DAY_MILLIS = 3600000L * 24L;
private final static long HOUR_MILLIS = 3600000L;
private final static long MINUTE_MILLIS = 60000L;
private final static long SECOND_MILLIS = 1000L;

public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
return format((long) number, toAppendTo, pos);
}

public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {
if (number == 0L) {
toAppendTo.append("0");
}
long rest = number;
long days = rest / DAY_MILLIS;
if (days > 0) {
toAppendTo.append(days).append("d");
rest %= DAY_MILLIS;
}
long hours = rest / HOUR_MILLIS;
if (hours > 0) {
toAppendTo.append(hours).append("h");
rest %= HOUR_MILLIS;
}
long minutes = rest / MINUTE_MILLIS;
if (minutes > 0) {
toAppendTo.append(minutes).append("m");
rest %= MINUTE_MILLIS;
}
long seconds = rest / SECOND_MILLIS;
if (seconds > 0) {
toAppendTo.append(seconds).append("s");
rest %= SECOND_MILLIS;
}
if (rest > 0) {
toAppendTo.append(rest).append("ms");
}
return toAppendTo;
}

public Number parse(String source, ParsePosition parsePosition) {
throw new UnsupportedOperationException();
}

}

Discussion