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

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

Introduction

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

Prototype

public StrBuilder insert(final int index, final double value) 

Source Link

Document

Inserts the value into this builder.

Usage

From source file:de.vandermeer.skb.interfaces.application.App_CliParser.java

/**
 * Prints usage information for the CLI parser including all CLI options.
 * @param width the console columns or width of each output line
 * @return list of lines with usage information
 *//* w ww .j a v  a  2  s .co m*/
default ArrayList<StrBuilder> usage(int width) {
    TreeMap<String, ApoBaseC> map = CliOptionList.sortedMap(this.getAllOptions(), this.numberShort(),
            this.numberLong());
    Map<String, String> helpMap = new LinkedHashMap<>();
    int length = 0;
    STGroupFile stg = new STGroupFile("de/vandermeer/skb/interfaces/application/help.stg");
    for (Object option : map.values()) {
        ST sto = stg.getInstanceOf("option");
        String description = null;
        if (ClassUtils.isAssignable(option.getClass(), Apo_SimpleC.class)) {
            sto.add("cliShort", ((Apo_SimpleC) option).getCliShort());
            sto.add("cliLong", ((Apo_SimpleC) option).getCliLong());
            description = ((Apo_SimpleC) option).getDescription();
        }
        if (ClassUtils.isAssignable(option.getClass(), Apo_TypedC.class)) {
            sto.add("cliShort", ((Apo_TypedC<?>) option).getCliShort());
            sto.add("cliLong", ((Apo_TypedC<?>) option).getCliLong());
            sto.add("argName", ((Apo_TypedC<?>) option).getCliArgumentName());
            sto.add("argOptional", ((Apo_TypedC<?>) option).cliArgIsOptional());
            description = ((Apo_TypedC<?>) option).getDescription();
        }
        String line = sto.render();
        if (line.length() > length) {
            length = line.length();
        }
        helpMap.put(line, description);
    }
    length += 4;

    ArrayList<StrBuilder> ret = new ArrayList<>();
    for (Entry<String, String> entry : helpMap.entrySet()) {
        StrBuilder argLine = new StrBuilder();
        argLine.append(entry.getKey()).appendPadding(length - argLine.length(), ' ');
        StrBuilder padLine = new StrBuilder();
        padLine.appendPadding(length, ' ');

        Collection<StrBuilder> text = Text_To_FormattedText.left(entry.getValue(), width - length);
        int i = 0;
        for (StrBuilder b : text) {
            if (i == 0) {
                b.insert(0, argLine);
            } else {
                b.insert(0, padLine);
            }
            ret.add(b);
            i++;
        }
    }
    return ret;
}

From source file:de.vandermeer.asciitable.AT_Renderer.java

/**
 * Renders an {@link AsciiTable}.//from  ww  w .j ava  2 s.  co  m
 * @param rows table rows to render, cannot be null
 * @param colNumbers number of columns in the table
 * @param ctx context of the original table with relevant settings, cannot be null
 * @param width maximum line width, excluding any extra padding
 * @return collection of lines, each as a {@link StrBuilder}
 * @throws {@link NullPointerException} if rows or context where null
 */
default Collection<StrBuilder> renderAsCollection(LinkedList<AT_Row> rows, int colNumbers, AT_Context ctx,
        int width) {
    Validate.notNull(rows);
    Validate.notNull(ctx);

    ArrayList<Object> table = new ArrayList<>();
    int[] colWidth = this.getCWC().calculateColumnWidths(rows, colNumbers, ctx.getTextWidth(width));

    for (AT_Row row : rows) {
        int ruleset = 0;
        switch (row.getStyle()) {
        case NORMAL:
            ruleset = TA_GridConfig.RULESET_NORMAL;
            break;
        case STRONG:
            ruleset = TA_GridConfig.RULESET_STRONG;
            break;
        case LIGHT:
            ruleset = TA_GridConfig.RULESET_LIGHT;
            break;
        case HEAVY:
            ruleset = TA_GridConfig.RULESET_HEAVY;
            break;
        case UNKNOWN:
            throw new AsciiTableException("AT_Renderer: cannot render unknown row style",
                    "table row style set to 'unknown'");
        default:
            throw new AsciiTableException("AT_Renderer: cannot render unknown row style",
                    "table row style not specified or type not processed");
        }

        switch (row.getType()) {
        case RULE:
            table.add(ruleset);
            break;
        case CONTENT:
            String[][] cAr = new String[colNumbers][];
            LinkedList<AT_Cell> cells = row.getCells();
            if (cells == null) {
                throw new AsciiTableException("cannot render table", "row content (cells) was null");
            }

            int length = 0;
            for (int i = 0; i < cells.size(); i++) {
                length += colWidth[i];

                Object content = cells.get(i).getContent();
                if (content == null) {
                    length++;
                    continue;
                }

                int realWidth = length;
                length -= cells.get(i).getContext().getPaddingLeft();
                length -= cells.get(i).getContext().getPaddingRight();

                if (content instanceof RendersToClusterWidth) {
                    cAr[i] = ((RendersToClusterWidth) content).renderAsArray(length);
                }
                if (content instanceof DoesRenderToWidth) {
                    cAr[i] = new StrTokenizer(((DoesRenderToWidth) content).render(length))
                            .setDelimiterChar('\n').setIgnoreEmptyTokens(false).getTokenArray();
                } else {
                    //create text from cell object
                    String text = Object_To_StrBuilder.convert(content).toString().replaceAll("\\s+", " ");

                    //check for translators, use what is available
                    if (cells.get(i).getContext().getTargetTranslator() != null) {
                        if (cells.get(i).getContext().getTargetTranslator().getCombinedTranslator() != null) {
                            text = cells.get(i).getContext().getTargetTranslator().getCombinedTranslator()
                                    .translate(text);
                        }
                    } else if (cells.get(i).getContext().getHtmlElementTranslator() != null) {
                        text = cells.get(i).getContext().getHtmlElementTranslator().translateHtmlElements(text);
                    } else if (cells.get(i).getContext().getCharTranslator() != null) {
                        text = cells.get(i).getContext().getCharTranslator().translateCharacters(text);
                    }

                    Collection<StrBuilder> csb = Text_To_FormattedText
                            .create(length, cells.get(i).getContext().getTextAlignment().getMapping(),
                                    TextFormat.NONE.getMapping(), null, null, null, 0, 0, null, 0, 0, null)
                            .transform(text);
                    for (StrBuilder sb : csb) {
                        sb.insert(0, new StrBuilder().appendPadding(cells.get(i).getContext().getPaddingLeft(),
                                cells.get(i).getContext().getPaddingLeftChar()));
                        sb.appendPadding(cells.get(i).getContext().getPaddingRight(),
                                cells.get(i).getContext().getPaddingRightChar());
                    }
                    for (int k = 0; k < cells.get(i).getContext().getPaddingTop(); k++) {
                        ((ArrayList<StrBuilder>) csb).add(0, new StrBuilder().appendPadding(realWidth,
                                cells.get(i).getContext().getPaddingTopChar()));
                    }
                    for (int k = 0; k < cells.get(i).getContext().getPaddingBottom(); k++) {
                        ((ArrayList<StrBuilder>) csb).add(new StrBuilder().appendPadding(realWidth,
                                cells.get(i).getContext().getPaddingBottomChar()));
                    }

                    cAr[i] = ClusterElementTransformer.create()
                            .transform(csb, StrBuilder_To_String.create(), ArrayListStrategy.create())
                            .toArray(new String[0]);
                }
                length = 0;
            }
            cAr = Array2D_To_NormalizedArray.create(colNumbers).transform(cAr);
            cAr = Array2D_To_FlipArray.create().transform(cAr);
            table.add(Pair.of(ruleset, cAr));
            break;
        case UNKNOWN:
            throw new AsciiTableException("AT_Renderer: cannot render unknown row type",
                    "table row type set to 'unknown'");
        default:
            throw new AsciiTableException("AT_Renderer: cannot render unknown row type",
                    "table row type not specified or type not processed");
        }
    }

    ArrayList<StrBuilder> ret = ctx.getGrid().addGrid(table, ctx.getGridTheme() | ctx.getGridThemeOptions());
    int max = ret.get(0).length() + ctx.getFrameLeftMargin() + ctx.getFrameRightMargin();
    for (StrBuilder sb : ret) {
        sb.insert(0, new StrBuilder().appendPadding(ctx.getFrameLeftMargin(), ctx.getFrameLeftChar()));
        sb.appendPadding(ctx.getFrameRightMargin(), ctx.getFrameRightChar());
    }
    for (int k = 0; k < ctx.getFrameTopMargin(); k++) {
        ret.add(0, new StrBuilder().appendPadding(max, ctx.getFrameTopChar()));
    }
    for (int k = 0; k < ctx.getFrameBottomMargin(); k++) {
        ret.add(new StrBuilder().appendPadding(max, ctx.getFrameBottomChar()));
    }

    return ret;
}

From source file:de.vandermeer.asciithemes.TA_Frame.java

/**
 * Changes the input collection by adding a frame.
 * The return list will have/* w w  w . ja v  a2  s. c o  m*/
 * - one line added as first element with the frame top line,
 * - one line added at the end with the frame bottom line, and
 * - for each original element border characters will be added left and right.
 * 
 * The methods assumes that all line (members of the collection) have the same length.
 * 
 * The following exceptions apply:
 * 
 * - top line will not be added if the frame does not define a top line nor top corners
 * - bottom line will not be added if the frame does not define a bottom line nor bottom corners
 * - left borders will only be added if the frame defines a border with left border character
 * - right borders will only be added if the frame defines a border with right border character
 * 
 * The method does guarantee that all lines (member of the final collection) have the same length.
 * 
 * @param coll the collection to add a frame to
 * @param mode options for the frame
 * @return new list with frame
 */
default ArrayList<StrBuilder> addFrame(Collection<StrBuilder> coll, int mode) {
    Validate.notNull(coll);
    Validate.noNullElements(coll);
    int width = 0;
    for (StrBuilder sb : coll) {
        width = sb.length();
    }

    ArrayList<StrBuilder> ret = new ArrayList<>();
    if (TA_FrameOptions.someTopCharacters(mode)) {
        // first, all clear to add topline
        StrBuilder top = this.renderTopLine(mode, width);
        if (top.length() > 0) {
            ret.add(top);
        }
    }

    for (StrBuilder sb : coll) {
        // add border
        if (TA_FrameOptions.borderLeft(mode)) {
            sb.insert(0, this.getBorder().getBorder(TA_Border.MODE_LEFT));
        } else if (TA_FrameOptions.borderLeftNeeded(mode)) {
            sb.insert(0, new StrBuilder()
                    .appendPadding(this.getBorder().getBorder(TA_Border.MODE_LEFT).length(), ' '));
        }
        if (TA_FrameOptions.borderRight(mode)) {
            sb.append(this.getBorder().getBorder(TA_Border.MODE_RIGHT));
        } else if (TA_FrameOptions.borderRightNeeded(mode)) {
            sb.appendPadding(this.getBorder().getBorder(TA_Border.MODE_RIGHT).length(), ' ');
        }
        ret.add(sb);
    }

    // add bottom line
    if (TA_FrameOptions.someBottomCharacters(mode)) {
        StrBuilder bottom = this.renderBottomLine(mode, width);
        if (bottom.length() > 0) {
            ret.add(bottom);
        }
    }

    return ret;
}

From source file:nih.quack.jythonpygments.Token.java

public String repr() {
    if (repr == null) {
        StrBuilder b = new StrBuilder();
        Token token = this;
        while (token != null) {
            if (!b.startsWith(token.name())) {
                b.insert(0, token.name());
            }/*w w w .  ja v  a 2  s  .c  o  m*/
            token = token.parent();
        }
        repr = b.toString();
    }
    return repr;
}