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

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

Introduction

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

Prototype

@Override
public int length() 

Source Link

Document

Gets the length of the string builder.

Usage

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

/**
 * Returns a line of given length.//from  w w w .jav  a  2s .  c om
 * @param length number of in the line
 * @param builder builder to append the line to, new builder will be created if null
 * @return line of given length, empty if length was null or negative
 */
default StrBuilder getLine(int length, StrBuilder builder) {
    StrBuilder append = new StrBuilder();
    if (this.getLineString().length() == 0 || length == 0) {
        return append;
    }
    while (append.length() < (length)) {
        append.append(this.getLineString());
    }
    while (append.length() > length) {
        append.deleteCharAt(append.length() - 1);
    }
    if (builder == null) {
        return append;
    }
    return builder.append(append);
}

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

/**
 * Returns a line of given length.//from   www  .  j av  a 2  s .co m
 * @param length number of characters in the line
 * @param builder builder to append the line to, new builder will be created if null
 * @return line of given length, empty if length was null or negative
 */
default StrBuilder getLine(int length, StrBuilder builder) {
    StrBuilder ret = new StrBuilder();
    if (length < 1) {
        return (builder == null) ? ret : builder;
    }
    ret.appendPadding(length / 2, this.getLeftLineChar());
    while (ret.length() < length) {
        ret.appendPadding(1, this.getRightLineChar());
    }

    return (builder == null) ? ret : builder.append(ret);
}

From source file:net.radai.beanz.codecs.CollectionCodec.java

@Override
public String encode(Object object) {
    if (object == null) {
        return null;
    }//from   w w w . j  a  v  a 2 s  .c o m
    if (!ReflectionUtil.isCollection(object.getClass())) {
        throw new IllegalArgumentException();
    }
    Collection<?> collection = (Collection<?>) object;
    if (collection.isEmpty()) {
        return "[]";
    }
    StrBuilder sb = new StrBuilder();
    sb.append("[");
    for (Object element : collection) {
        sb.append(elementCodec.encode(element)).append(", ");
    }
    sb.delete(sb.length() - 2, sb.length()); //last ", "
    sb.append("]");
    return sb.toString();
}

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
 *//*  ww  w  .j  a va 2  s . c om*/
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.asciithemes.TA_Frame.java

/**
 * Changes the input collection by adding a frame.
 * The return list will have//from  w w  w  .j  a  va  2  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:candr.yoclip.ParseResult.java

@Override
public String toString() {

    final StrBuilder builder = new StrBuilder();

    if (!isParseError()) {
        builder.append("Ok");

    } else {/*from w ww  .  j  a  va2  s  .  c om*/

        for (final ParsedOption<T> parsedOption : errors) {

            if (builder.length() > 0) {
                builder.appendNewLine();
            }

            final ParserOption<T> parserOption = parsedOption.getParserOption();
            if (null != parserOption) {
                builder.append(parserOption).append(": ");

            }
            builder.append(parsedOption.getError());
        }
    }

    return builder.toString();
}

From source file:net.radai.beanz.codecs.ArrayCodec.java

@Override
public String encode(Object object) {
    if (object == null) {
        return null;
    }/*from   w ww  .  j  a  v  a2  s  .c  om*/
    if (!ReflectionUtil.isArray(object.getClass())) {
        throw new IllegalArgumentException();
    }
    int size = Array.getLength(object);
    if (size == 0) {
        return "[]";
    }
    StrBuilder sb = new StrBuilder();
    sb.append("[");
    for (int i = 0; i < size; i++) {
        Object element = Array.get(object, i);
        String encoded = elementCodec.encode(element);
        sb.append(encoded).append(", ");
    }
    sb.delete(sb.length() - 2, sb.length()); //last ", "
    sb.append("]");
    return sb.toString();
}

From source file:net.radai.beanz.codecs.MapCodec.java

@Override
public String encode(Object object) {
    if (object == null) {
        return null;
    }/*from   w w w.j av a 2s.  co  m*/
    if (!ReflectionUtil.isMap(object.getClass())) {
        throw new IllegalArgumentException();
    }
    Map<?, ?> map = (Map) object;
    if (map.isEmpty()) {
        return "{}";
    }
    StrBuilder sb = new StrBuilder();
    sb.append("{");
    for (Map.Entry<?, ?> entry : map.entrySet()) {
        sb.append(keyCodec.encode(entry.getKey())).append("=").append(valueCodec.encode(entry.getValue()))
                .append(", ");
    }
    sb.delete(sb.length() - 2, sb.length()); //last ", "
    sb.append("}");
    return sb.toString();
}

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

/**
 * Prints a help screen for the application, to be used by an executing component.
 *//*from   w  w  w.  ja  v  a  2  s.c o  m*/
default void appHelpScreen() {
    STGroupFile stg = new STGroupFile("de/vandermeer/skb/interfaces/application/help.stg");
    ST st = stg.getInstanceOf("usage");
    st.add("appName", this.getAppName());
    st.add("appDisplayName", this.getAppDisplayName());
    st.add("appVersion", this.getAppVersion());
    st.add("appDescription", Text_To_FormattedText.left(this.getAppDescription(), this.getConsoleWidth()));
    st.add("required", CliOptionList.getRequired(getCLiALlOptions()));
    for (StrBuilder sb : this.getCliParser().usage(this.getConsoleWidth())) {
        st.add("cliOptions", sb);
    }

    if (this.getEnvironmentOptions().size() > 0) {
        TreeMap<String, Apo_TypedE<?>> map = EnvironmentOptionsList.sortedMap(this.getEnvironmentOptions());
        Map<String, String> envMap = new LinkedHashMap<>();
        int length = 0;
        for (Apo_TypedE<?> te : map.values()) {
            if (te.getEnvironmentKey().length() > length) {
                length = te.getEnvironmentKey().length();
            }
            envMap.put(te.getEnvironmentKey(), te.getDescription());
        }
        length += 2;

        for (Entry<String, String> entry : envMap.entrySet()) {
            StrBuilder argLine = new StrBuilder();
            argLine.append(entry.getKey()).appendPadding(length - argLine.length(), ' ').append("  - ");
            StrBuilder padLine = new StrBuilder();
            padLine.appendPadding(length + 4, ' ');

            Collection<StrBuilder> text = Text_To_FormattedText.left(entry.getValue(),
                    this.getConsoleWidth() - length);
            int i = 0;
            for (StrBuilder b : text) {
                if (i == 0) {
                    st.add("envOptions", argLine + b.build());
                } else {
                    st.add("envOptions", padLine + b.build());
                }
                i++;
            }
        }
    }

    if (this.getPropertyOptions().size() > 0) {
        TreeMap<String, Apo_TypedP<?>> map = PropertyOptionsList.sortedMap(this.getPropertyOptions());
        Map<String, String> envMap = new LinkedHashMap<>();
        int length = 0;
        for (Apo_TypedP<?> te : map.values()) {
            if (te.getPropertyKey().length() > length) {
                length = te.getPropertyKey().length();
            }
            envMap.put(te.getPropertyKey(), te.getDescription());
        }
        length += 2;

        for (Entry<String, String> entry : envMap.entrySet()) {
            StrBuilder argLine = new StrBuilder();
            argLine.append(entry.getKey()).appendPadding(length - argLine.length(), ' ').append("  - ");
            StrBuilder padLine = new StrBuilder();
            padLine.appendPadding(length + 4, ' ');

            Collection<StrBuilder> text = Text_To_FormattedText.left(entry.getValue(),
                    this.getConsoleWidth() - length);
            int i = 0;
            for (StrBuilder b : text) {
                if (i == 0) {
                    st.add("propOptions", argLine + b.build());
                } else {
                    st.add("propOptions", padLine + b.build());
                }
                i++;
            }
        }
    }
    System.out.println(st.render());
}

From source file:com.hotpot.commons.pagination.PageHelper.java

/**
 * MappedStatement?????./*from   www  .j  a va 2 s  . com*/
 *
 * @param ms the ms
 * @param newSqlSource the new sql source
 * @return the mapped statement
 */
private MappedStatement newMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
    MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(),
            ms.getId() + "_PageHelper", newSqlSource, ms.getSqlCommandType());
    builder.resource(ms.getResource());
    builder.fetchSize(ms.getFetchSize());
    builder.statementType(ms.getStatementType());
    builder.keyGenerator(ms.getKeyGenerator());
    if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
        StrBuilder keyProperties = new StrBuilder();
        for (String keyProperty : ms.getKeyProperties()) {
            keyProperties.append(keyProperty).append(",");
        }
        keyProperties.delete(keyProperties.length() - 1, keyProperties.length());
        builder.keyProperty(keyProperties.toString());
    }
    builder.timeout(ms.getTimeout());
    builder.parameterMap(ms.getParameterMap());
    //resultMaps?int??resultMap - ?
    List<ResultMap> resultMaps = new ArrayList<ResultMap>();
    ResultMap resultMap = new ResultMap.Builder(ms.getConfiguration(), ms.getId(), int.class,
            EMPTY_RESULTMAPPING).build();
    resultMaps.add(resultMap);
    builder.resultMaps(resultMaps);
    builder.resultSetType(ms.getResultSetType());
    builder.cache(ms.getCache());
    builder.flushCacheRequired(ms.isFlushCacheRequired());
    builder.useCache(ms.isUseCache());

    return builder.build();
}