private static String format()

in gshell-support/gshell-chronos/src/main/java/org/apache/geronimo/gshell/chronos/DurationFormatUtils.java [73:127]


    private static String format(Token[] tokens, int years, int months, int days, int hours, int minutes, int seconds, int milliseconds, boolean padWithZeros) {
        StringBuilder buffer = new StringBuilder();
        boolean lastOutputSeconds = false;
        int sz = tokens.length;
        for (int i = 0; i < sz; i++) {
            Token token = tokens[i];
            Object value = token.getValue();
            int count = token.getCount();
            if (value instanceof StringBuilder) {
                buffer.append(value.toString());
            }
            else {
                if (value == y) {
                    buffer.append(padWithZeros ? leftPad(Integer.toString(years), count, "0") : Integer.toString(years));
                    lastOutputSeconds = false;
                }
                else if (value == M) {
                    buffer.append(padWithZeros ? leftPad(Integer.toString(months), count, "0") : Integer.toString(months));
                    lastOutputSeconds = false;
                }
                else if (value == d) {
                    buffer.append(padWithZeros ? leftPad(Integer.toString(days), count, "0") : Integer.toString(days));
                    lastOutputSeconds = false;
                }
                else if (value == H) {
                    buffer.append(padWithZeros ? leftPad(Integer.toString(hours), count, "0") : Integer.toString(hours));
                    lastOutputSeconds = false;
                }
                else if (value == m) {
                    buffer.append(padWithZeros ? leftPad(Integer.toString(minutes), count, "0") : Integer.toString(minutes));
                    lastOutputSeconds = false;
                }
                else if (value == s) {
                    buffer.append(padWithZeros ? leftPad(Integer.toString(seconds), count, "0") : Integer.toString(seconds));
                    lastOutputSeconds = true;
                }
                else if (value == S) {
                    if (lastOutputSeconds) {
                        milliseconds += 1000;
                        String str = padWithZeros
                                ? leftPad(Integer.toString(milliseconds), count, "0")
                                : Integer.toString(milliseconds);
                        buffer.append(str.substring(1));
                    }
                    else {
                        buffer.append(padWithZeros
                                ? leftPad(Integer.toString(milliseconds), count, "0")
                                : Integer.toString(milliseconds));
                    }
                    lastOutputSeconds = false;
                }
            }
        }
        return buffer.toString();
    }