Example usage for org.apache.commons.lang3.text StrBuilder appendFixedWidthPadRight

List of usage examples for org.apache.commons.lang3.text StrBuilder appendFixedWidthPadRight

Introduction

In this page you can find the example usage for org.apache.commons.lang3.text StrBuilder appendFixedWidthPadRight.

Prototype

public StrBuilder appendFixedWidthPadRight(final int value, final int width, final char padChar) 

Source Link

Document

Appends an object to the builder padding on the right to a fixed length.

Usage

From source file:de.vandermeer.skb.interfaces.transformers.textformat.String_To_LeftPadded.java

@Override
default StrBuilder transform(String s) {
    IsTransformer.super.transform(s);
    StrBuilder ret = (this.getBuilderForAppend() == null) ? new StrBuilder(this.getLength())
            : this.getBuilderForAppend();

    String left = (s == null) ? "" : s;
    left = left.replace(' ', this.getInnerWsChar());
    ret.appendFixedWidthPadRight(left, this.getLength(), this.getPaddingChar());
    return ret;// ww w  . j  av  a2s.c o  m
}

From source file:de.vandermeer.asciitable.v2.render.V2_AsciiTableRenderer.java

private void appendWithAlignment(char alignment, StrBuilder sb, String str, int width, char paddingChar,
        int padding, boolean isLastLine) {
    if (padding > 0) {
        width = width - padding * 2;//from   ww  w .  j  av a 2s.  c o  m
    }
    for (int i = 0; i < padding; i++) {
        sb.append(paddingChar);
    }

    if ('l' == alignment) {
        sb.appendFixedWidthPadRight(str, width, paddingChar);
    } else if ('r' == alignment) {
        sb.appendFixedWidthPadLeft(str, width, paddingChar);
    } else if ('c' == alignment) {
        sb.append(StringUtils.center(str, width, paddingChar));
    } else if ('j' == alignment || 't' == alignment) {
        if (isLastLine) {
            //nothing needed if this is the last line of a wrapped text
            if ('j' == alignment) {
                sb.appendFixedWidthPadRight(str, width, paddingChar);
            } else {
                //mist be 't' now
                sb.appendFixedWidthPadLeft(str, width, paddingChar);
            }
        } else {
            String[] ar = StringUtils.split(str);
            int length = 0;
            for (String s : ar) {
                length += s.length();
            }

            //first spaces to distributed (even)
            int first = ((width - length) / (ar.length - 1)) * (ar.length - 1);
            for (int i = 0; i < ar.length - 1; i++) {
                if (first != 0) {
                    ar[i] += " ";
                    first--;
                }
            }

            //second space to distributed (leftovers, as many as there are)
            int second = (width - length) % (ar.length - 1);
            for (int i = 0; i < ar.length - 1; i++) {
                if (second != 0) {
                    ar[i] += " ";
                    second--;
                }
            }
            sb.append(StringUtils.join(ar));
        }
    } else {
        System.err.println("ERROR RENDER ALIGNMENT");//TODO
    }

    for (int i = 0; i < padding; i++) {
        sb.append(paddingChar);
    }
}

From source file:com.mgmtp.perfload.core.console.status.FileStatusTransformer.java

private void processStatusInfo(final Map<StatusInfoKey, StatusInfo> statusInfoMap,
        final Map<StatusInfoKey, Deque<ThreadActivity>> threadActivitiesMap) {
    PrintWriter pr = null;/*from  w  w w . j a  v a2 s . c o  m*/
    try {
        pr = new PrintWriter(statusFile, encoding);
        pr.println(HR);
        pr.println(HEADER);
        pr.println(HR);

        Set<StatusInfoKey> sortedKeys = newTreeSet(statusInfoMap.keySet());

        int threadsFinished = 0;
        for (StatusInfoKey key : sortedKeys) {
            StatusInfo statusInfo = statusInfoMap.get(key);

            if (statusInfo.getFinished() != null && statusInfo.getFinished()) {
                threadsFinished++;
            }

            StrBuilder sb = new StrBuilder(200);

            sb.append(LINE_START);
            sb.appendFixedWidthPadLeft(statusInfo.getDaemonId(), DAEMON_ID.length(), ' ');
            sb.append(DELIMITER);
            sb.appendFixedWidthPadLeft(statusInfo.getProcessId(), PROCESS_ID.length(), ' ');
            sb.append(DELIMITER);
            sb.appendFixedWidthPadLeft(statusInfo.getThreadId(), THREAD_ID.length(), ' ');
            sb.append(DELIMITER);
            sb.appendFixedWidthPadRight(statusInfo.getOperation(), OPERATION.length(), ' ');
            sb.append(DELIMITER);
            sb.appendFixedWidthPadRight(statusInfo.getTarget(), TARGET.length(), ' ');
            sb.append(DELIMITER);
            sb.appendFixedWidthPadRight(statusInfo.getFinished(), FINISHED.length(), ' ');
            sb.append(DELIMITER);
            sb.appendFixedWidthPadRight(statusInfo.getStackTrace() != null, ERROR.length(), ' ');
            sb.append(DELIMITER);
            sb.appendFixedWidthPadRight(
                    statusInfo.getError() != null ? statusInfo.getError() ? "ERROR" : "SUCCESS" : "",
                    RESULT.length(), ' ');
            sb.append(LINE_END);

            pr.println(sb);
        }

        pr.println(HR);

        StrBuilder sb = new StrBuilder(HR.length);
        sb.append(LINE_START);

        int activeThreads = 0;
        for (Deque<ThreadActivity> ta : threadActivitiesMap.values()) {
            activeThreads += ta.peekLast().getActiveThreads();
        }
        sb.appendFixedWidthPadRight("Currently active client threads: " + activeThreads,
                HR.length - LINE_START.length() - LINE_END.length(), ' ');
        sb.append(LINE_END);
        pr.println(sb);

        sb = new StrBuilder(HR.length);
        sb.append(LINE_START);
        maxConcurrentThreads = max(maxConcurrentThreads, activeThreads);
        sb.appendFixedWidthPadRight("Maximum concurrent client threads: " + maxConcurrentThreads,
                HR.length - LINE_START.length() - LINE_END.length(), ' ');
        sb.append(LINE_END);
        pr.println(sb);

        sb = new StrBuilder(HR.length);
        sb.append(LINE_START);
        sb.appendFixedWidthPadRight("Total threads finished: " + threadsFinished + "/" + totalThreadCount,
                HR.length - LINE_START.length() - LINE_END.length(), ' ');
        sb.append(LINE_END);
        pr.println(sb);

        pr.println(HR);
    } catch (IOException ex) {
        log.error("Error writing load test status to file: {}", statusFile, ex);
    } finally {
        IOUtils.closeQuietly(pr);
    }
}

From source file:de.vandermeer.asciitable.v1.V1_AsciiTable.java

/**
 * Renders rows of content with correct separators.
 * @param row array with all lines for all columns of a single row
 * @param padChar padding character//  ww w .  j ava2  s.c om
 * @param theme row theme
 * @return rendered lines for the row, empty if nothing was to be rendered
 */
protected final StrBuilder renderRow(String[][] row, char padChar, char[] theme) {
    StrBuilder ret = new StrBuilder(100);
    ret.setNullText("");
    for (int k = 0; k < row.length; k++) {
        String[] ar = row[k];
        ret.append(theme[V1_TableTheme.VERTICAL]);
        //if null is in array we need to render column spans
        if (ArrayUtils.contains(ar, null)) {
            int span = 0;
            for (int i = 0; i < ar.length; i++) {
                String content = ar[i];
                if (content == null) {
                    if (i == ar.length - 1) {
                        //a null in last column, so calculate the span)
                        int width = 0;
                        //add the span column width
                        for (k = 0; k < span; k++) {
                            width += this.columns[k + 1];
                        }
                        //add the separator characters (span) plus the one for this column
                        width += span;
                        //add the current column width
                        width += this.columns[i + 1];
                        //centre content in the new column
                        ret.appendFixedWidthPadRight("", width, padChar);
                    } else {
                        span += 1;
                        continue;
                    }
                } else if ("".equals(content)) {
                    //we have an empty column, so
                    //first finish the spans
                    for (k = 0; k < span; k++) {
                        ret.appendFixedWidthPadRight("", this.columns[k + 1], padChar);
                    }
                    ret.appendFixedWidthPadRight("", span, padChar);
                    ret.append(theme[V1_TableTheme.VERTICAL]);
                    span = 0;
                    //now add the empty column
                    ret.appendFixedWidthPadRight(content, this.columns[i + 1], padChar);
                    if (i < ar.length - 1) {
                        ret.append(theme[V1_TableTheme.VERTICAL]);
                    }
                } else {
                    int width = 0;
                    //add the span column width
                    for (k = 0; k < span; k++) {
                        width += this.columns[k + 1];
                    }
                    //add the separator characters (span) plus the one for this column
                    width += span;
                    //add the current column width
                    width += this.columns[i + 1];
                    //centre content in the new column
                    ret.append(StringUtils.center(content, width, padChar));
                    if (i < ar.length - 1) {
                        ret.append(theme[V1_TableTheme.VERTICAL]);
                    }
                    span = 0;
                }
            }
        } else {
            for (int i = 0; i < ar.length; i++) {
                String content = ar[i];
                ret.appendFixedWidthPadRight(content, this.columns[i + 1], padChar);
                if (i < ar.length - 1) {
                    ret.append(theme[V1_TableTheme.VERTICAL]);
                }
            }
        }
        ret.append(theme[V1_TableTheme.VERTICAL]);
        if (k < row.length - 1) {
            ret.append('\n');
        }
    }

    return ret;
}

From source file:de.vandermeer.asciitable.v1.AsciiTable.java

/**
 * Renders rows of content with correct separators.
 * @param row array with all lines for all columns of a single row
 * @param padChar padding character//from   w w w  .j  a v  a 2 s. c o  m
 * @param theme row theme
 * @return rendered lines for the row, empty if nothing was to be rendered
 */
protected final StrBuilder renderRow(String[][] row, char padChar, char[] theme) {
    StrBuilder ret = new StrBuilder(100);
    ret.setNullText("");
    for (int k = 0; k < row.length; k++) {
        String[] ar = row[k];
        ret.append(theme[TableTheme.VERTICAL]);
        //if null is in array we need to render column spans
        if (ArrayUtils.contains(ar, null)) {
            int span = 0;
            for (int i = 0; i < ar.length; i++) {
                String content = ar[i];
                if (content == null) {
                    if (i == ar.length - 1) {
                        //a null in last column, so calculate the span)
                        int width = 0;
                        //add the span column width
                        for (k = 0; k < span; k++) {
                            width += this.columns[k + 1];
                        }
                        //add the separator characters (span) plus the one for this column
                        width += span;
                        //add the current column width
                        width += this.columns[i + 1];
                        //centre content in the new column
                        ret.appendFixedWidthPadRight("", width, padChar);
                    } else {
                        span += 1;
                        continue;
                    }
                } else if ("".equals(content)) {
                    //we have an empty column, so
                    //first finish the spans
                    for (k = 0; k < span; k++) {
                        ret.appendFixedWidthPadRight("", this.columns[k + 1], padChar);
                    }
                    ret.appendFixedWidthPadRight("", span, padChar);
                    ret.append(theme[TableTheme.VERTICAL]);
                    span = 0;
                    //now add the empty column
                    ret.appendFixedWidthPadRight(content, this.columns[i + 1], padChar);
                    if (i < ar.length - 1) {
                        ret.append(theme[TableTheme.VERTICAL]);
                    }
                } else {
                    int width = 0;
                    //add the span column width
                    for (k = 0; k < span; k++) {
                        width += this.columns[k + 1];
                    }
                    //add the separator characters (span) plus the one for this column
                    width += span;
                    //add the current column width
                    width += this.columns[i + 1];
                    //centre content in the new column
                    ret.append(StringUtils.center(content, width, padChar));
                    if (i < ar.length - 1) {
                        ret.append(theme[TableTheme.VERTICAL]);
                    }
                    span = 0;
                }
            }
        } else {
            for (int i = 0; i < ar.length; i++) {
                String content = ar[i];
                ret.appendFixedWidthPadRight(content, this.columns[i + 1], padChar);
                if (i < ar.length - 1) {
                    ret.append(theme[TableTheme.VERTICAL]);
                }
            }
        }
        ret.append(theme[TableTheme.VERTICAL]);
        if (k < row.length - 1) {
            ret.append('\n');
        }
    }

    return ret;
}

From source file:de.vandermeer.asciitable.v2.render.V2_AsciiTableRenderer.java

/**
 * Renders a content row.//from  w  w w  .jav a 2s .  com
 * @param row processed row to render
 * @param cols columns calculated by {@link V2_Width}
 * @return a string builder with the rendered strings (if lines are wrapped) of the rendered row
 */
protected final StrBuilder renderContentRow(ProcessedRow row, int[] cols) {
    StrBuilder ret = new StrBuilder(100);

    V2_RowTheme rt = null;
    String[][] columns = row.getProcessedColumns();
    BorderType[] borders = row.getBorderTypes();
    char[] alignment = ((ContentRow) row.getOriginalRow()).getAlignment();
    int[] padding = ((ContentRow) row.getOriginalRow()).getPadding();

    for (int i = 0; i < columns.length; i++) {
        rt = this.theme.getContent();
        if (i != 0) {
            ret.appendNewLine();
        }
        int span = 0;
        for (int k = 0; k < borders.length; k++) {
            if (borders[k] != BorderType.NONE) {
                if (k == 0) {
                    ret.append(RenderUtilities.getChar(BorderPosition.LEFT, borders[k], rt));
                } else if (k == borders.length - 1) {
                    ret.append(RenderUtilities.getChar(BorderPosition.RIGHT, borders[k], rt));
                } else {
                    ret.append(RenderUtilities.getChar(BorderPosition.MIDDLE, borders[k], rt));
                }
            }

            if (k < columns[i].length) {
                if (ArrayUtils.contains(columns[i], null)) {
                    if (columns[i][k] == null) {
                        if (k == columns[i].length - 1) {
                            //a null in last column, so calculate the span
                            int width = 0;
                            //add the span column width
                            for (int s = 0; s < span; s++) {
                                width += cols[s];
                            }
                            //add the separator characters (span) plus the one for this column
                            width += span;
                            //add the current column width
                            width += cols[k];
                            //Center content in the new column
                            ret.appendFixedWidthPadRight("", width, this.paddingChar);
                        } else {
                            span += 1;
                        }
                    } else if ("".equals(columns[i][k])) {
                        //we have an empty column, so
                        //first finish the spans
                        for (int s = 0; s < span; s++) {
                            ret.appendFixedWidthPadRight("", cols[s], this.paddingChar);
                        }
                        ret.appendFixedWidthPadRight("", span, this.paddingChar);
                        span = 0;
                        //now add the empty column
                        ret.appendFixedWidthPadRight(columns[i][k], cols[k], this.paddingChar);
                    } else {
                        int width = 0;
                        //add the span column width
                        for (int s = 0; s < span; s++) {
                            width += cols[s];
                        }
                        //add the separator characters (span) plus the one for this column
                        width += span;
                        //add the current column width
                        width += cols[k];
                        //add row with proper alignment
                        this.appendWithAlignment(alignment[k], ret, columns[i][k], width, this.paddingChar,
                                padding[k], (i == (columns.length)));
                        span = 0;
                    }
                } else {
                    this.appendWithAlignment(alignment[k], ret, columns[i][k], cols[k], this.paddingChar,
                            padding[k], (i == (columns.length - 1)));
                }
            }
        }
    }
    return ret;
}