Java Code Examples for org.apache.commons.lang.text.StrBuilder#append()

The following examples show how to use org.apache.commons.lang.text.StrBuilder#append() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClassUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Converts a class name to a JLS style class name.
 *
 * @param className  the class name
 * @return the converted name
 */
private static String toCanonicalName(String className) {
    className = StringUtils.deleteWhitespace(className);
    if (className == null) {
        throw new NullArgumentException("className");
    } else if (className.endsWith("[]")) {
        StrBuilder classNameBuffer = new StrBuilder();
        while (className.endsWith("[]")) {
            className = className.substring(0, className.length() - 2);
            classNameBuffer.append("[");
        }
        String abbreviation = (String) abbreviationMap.get(className);
        if (abbreviation != null) {
            classNameBuffer.append(abbreviation);
        } else {
            classNameBuffer.append("L").append(className).append(";");
        }
        className = classNameBuffer.toString();
    }
    return className;
}
 
Example 2
Source File: NumberRange.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Returns the string representation of this range.</p>
 *
 * <p>This string is the string representation of the minimum and
 * maximum numbers in the range, separated by a hyphen. If a number
 * is negative, then it is enclosed in parentheses.</p>
 *
 * @return the string representation of this range
 */
public String toString() {
    StrBuilder sb = new StrBuilder();

    if (min.doubleValue() < 0) {
        sb.append('(')
            .append(min)
            .append(')');
    } else {
        sb.append(min);
    }

    sb.append('-');

    if (max.doubleValue() < 0) {
        sb.append('(')
            .append(max)
            .append(')');
    } else {
        sb.append(max);
    }

    return sb.toString();
}
 
Example 3
Source File: CharSetUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Squeezes any repetitions of a character that is mentioned in the
 * supplied set.</p>
 *
 * <p>An example is:</p>
 * <ul>
 *   <li>squeeze(&quot;hello&quot;, {&quot;el&quot;}) => &quot;helo&quot;</li>
 * </ul>
 * 
 * @see CharSet#getInstance(java.lang.String) for set-syntax.
 * @param str  the string to squeeze, may be null
 * @param set  the character set to use for manipulation, may be null
 * @return modified String, <code>null</code> if null string input
 */
public static String squeeze(String str, String[] set) {
    if (StringUtils.isEmpty(str) || ArrayUtils.isEmpty(set)) {
        return str;
    }
    CharSet chars = CharSet.getInstance(set);
    StrBuilder buffer = new StrBuilder(str.length());
    char[] chrs = str.toCharArray();
    int sz = chrs.length;
    char lastChar = ' ';
    char ch = ' ';
    for (int i = 0; i < sz; i++) {
        ch = chrs[i];
        if (chars.contains(ch)) {
            if ((ch == lastChar) && (i != 0)) {
                continue;
            }
        }
        buffer.append(ch);
        lastChar = ch;
    }
    return buffer.toString();
}
 
Example 4
Source File: CharRange.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Gets a string representation of the character range.</p>
 * 
 * @return string representation of this range
 */
public String toString() {
    if (iToString == null) {
        StrBuilder buf = new StrBuilder(4);
        if (isNegated()) {
            buf.append('^');
        }
        buf.append(start);
        if (start != end) {
            buf.append('-');
            buf.append(end);
        }
        iToString = buf.toString();
    }
    return iToString;
}
 
Example 5
Source File: IntRange.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Gets the range as a <code>String</code>.</p>
 *
 * <p>The format of the String is 'Range[<i>min</i>,<i>max</i>]'.</p>
 *
 * @return the <code>String</code> representation of this range
 */
public String toString() {
    if (toString == null) {
        StrBuilder buf = new StrBuilder(32);
        buf.append("Range[");
        buf.append(min);
        buf.append(',');
        buf.append(max);
        buf.append(']');
        toString = buf.toString();
    }
    return toString;
}
 
Example 6
Source File: RecursiveFsObjectComparer.java    From celos with Apache License 2.0 5 votes vote down vote up
private void appendMessages(Collection<String> messages, StrBuilder strBuilder, String header) {
    if (messages.size() == 0) {
        return;
    }
    if (!strBuilder.isEmpty()) {
        strBuilder.appendNewLine();
    }
    strBuilder.append(header);
    List<String> sortedMessages = Lists.newArrayList(messages);
    Collections.sort(sortedMessages);
    strBuilder.appendWithSeparators(sortedMessages, ", ");
}
 
Example 7
Source File: ClassUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Converts a given name of class into canonical format.
 * If name of class is not a name of array class it returns
 * unchanged name.</p>
 * <p>Example:
 * <ul>
 * <li><code>getCanonicalName("[I") = "int[]"</code></li>
 * <li><code>getCanonicalName("[Ljava.lang.String;") = "java.lang.String[]"</code></li>
 * <li><code>getCanonicalName("java.lang.String") = "java.lang.String"</code></li>
 * </ul>
 * </p>
 *
 * @param className the name of class
 * @return canonical form of class name
 * @since 2.4
 */
private static String getCanonicalName(String className) {
    className = StringUtils.deleteWhitespace(className);
    if (className == null) {
        return null;
    } else {
        int dim = 0;
        while (className.startsWith("[")) {
            dim++;
            className = className.substring(1);
        }
        if (dim < 1) {
            return className;
        } else {
            if (className.startsWith("L")) {
                className = className.substring(
                    1,
                    className.endsWith(";")
                        ? className.length() - 1
                        : className.length());
            } else {
                if (className.length() > 0) {
                    className = (String) reverseAbbreviationMap.get(
                        className.substring(0, 1));
                }
            }
            StrBuilder canonicalClassNameBuffer = new StrBuilder(className);
            for (int i = 0; i < dim; i++) {
                canonicalClassNameBuffer.append("[]");
            }
            return canonicalClassNameBuffer.toString();
        }
    }
}
 
Example 8
Source File: ClassUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Gets the class name minus the package name from a String.</p>
 *
 * <p>The string passed in is assumed to be a class name - it is not checked.</p>
 *
 * @param className  the className to get the short name for
 * @return the class name of the class without the package name or an empty string
 */
public static String getShortClassName(String className) {
    if (className == null) {
        return StringUtils.EMPTY;
    }
    if (className.length() == 0) {
        return StringUtils.EMPTY;
    }

    StrBuilder arrayPrefix = new StrBuilder();

    // Handle array encoding
    if (className.startsWith("[")) {
        while (className.charAt(0) == '[') {
            className = className.substring(1);
            arrayPrefix.append("[]");
        }
        // Strip Object type encoding
        if (className.charAt(0) == 'L' && className.charAt(className.length() - 1) == ';') {
            className = className.substring(1, className.length() - 1);
        }
    }

    if (reverseAbbreviationMap.containsKey(className)) {
        className = (String)reverseAbbreviationMap.get(className);
    }

    int lastDotIdx = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR);
    int innerIdx = className.indexOf(
            INNER_CLASS_SEPARATOR_CHAR, lastDotIdx == -1 ? 0 : lastDotIdx + 1);
    String out = className.substring(lastDotIdx + 1);
    if (innerIdx != -1) {
        out = out.replace(INNER_CLASS_SEPARATOR_CHAR, PACKAGE_SEPARATOR_CHAR);
    }
    return out + arrayPrefix;
}
 
Example 9
Source File: CharSetUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Implementation of delete and keep
 *
 * @param str String to modify characters within
 * @param set String[] set of characters to modify
 * @param expect whether to evaluate on match, or non-match
 * @return modified String
 */
private static String modify(String str, String[] set, boolean expect) {
    CharSet chars = CharSet.getInstance(set);
    StrBuilder buffer = new StrBuilder(str.length());
    char[] chrs = str.toCharArray();
    int sz = chrs.length;
    for(int i=0; i<sz; i++) {
        if(chars.contains(chrs[i]) == expect) {
            buffer.append(chrs[i]);
        }
    }
    return buffer.toString();
}
 
Example 10
Source File: DoubleRange.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Gets the range as a <code>String</code>.</p>
 *
 * <p>The format of the String is 'Range[<i>min</i>,<i>max</i>]'.</p>
 *
 * @return the <code>String</code> representation of this range
 */
public String toString() {
    if (toString == null) {
        StrBuilder buf = new StrBuilder(32);
        buf.append("Range[");
        buf.append(min);
        buf.append(',');
        buf.append(max);
        buf.append(']');
        toString = buf.toString();
    }
    return toString;
}
 
Example 11
Source File: LongRange.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Gets the range as a <code>String</code>.</p>
 *
 * <p>The format of the String is 'Range[<i>min</i>,<i>max</i>]'.</p>
 *
 * @return the <code>String</code> representation of this range
 */
public String toString() {
    if (toString == null) {
        StrBuilder buf = new StrBuilder(32);
        buf.append("Range[");
        buf.append(min);
        buf.append(',');
        buf.append(max);
        buf.append(']');
        toString = buf.toString();
    }
    return toString;
}
 
Example 12
Source File: NumberRange.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Gets the range as a <code>String</code>.</p>
 *
 * <p>The format of the String is 'Range[<i>min</i>,<i>max</i>]'.</p>
 *
 * @return the <code>String</code> representation of this range
 */
public String toString() {
    if (toString == null) {
        StrBuilder buf = new StrBuilder(32);
        buf.append("Range[");
        buf.append(min);
        buf.append(',');
        buf.append(max);
        buf.append(']');
        toString = buf.toString();
    }
    return toString;
}
 
Example 13
Source File: CharSetUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>Translate characters in a String.
 * This is a multi character search and replace routine.</p>
 *
 * <p>An example is:</p>
 * <ul>
 *   <li>translate(&quot;hello&quot;, &quot;ho&quot;, &quot;jy&quot;)
 *    =&gt; jelly</li>
 * </ul>
 *
 * <p>If the length of characters to search for is greater than the
 * length of characters to replace, then the last character is 
 * used.</p>
 * 
 * <pre>
 * CharSetUtils.translate(null, *, *) = null
 * CharSetUtils.translate("", *, *)   = ""
 * </pre>
 *
 * @param str  String to replace characters in, may be null
 * @param searchChars   a set of characters to search for, must not be null
 * @param replaceChars  a set of characters to replace, must not be null or empty (&quot;&quot;)
 * @return translated String, <code>null</code> if null string input
 * @throws NullPointerException if <code>searchChars</code> or <code>replaceChars</code> 
 *  is <code>null</code>
 * @throws ArrayIndexOutOfBoundsException if <code>replaceChars</code> is empty (&quot;&quot;)
 * @deprecated Use {@link StringUtils#replaceChars(String, String, String)}.
 *             Method will be removed in Commons Lang 3.0.
 *  NOTE: StringUtils#replaceChars behaves differently when 'searchChars' is longer
 *  than 'replaceChars'. CharSetUtils#translate will use the last char of the replacement
 *  string whereas StringUtils#replaceChars will delete
 */
public static String translate(String str, String searchChars, String replaceChars) {
    if (StringUtils.isEmpty(str)) {
        return str;
    }
    StrBuilder buffer = new StrBuilder(str.length());
    char[] chrs = str.toCharArray();
    char[] withChrs = replaceChars.toCharArray();
    int sz = chrs.length;
    int withMax = replaceChars.length() - 1;
    for(int i=0; i<sz; i++) {
        int idx = searchChars.indexOf(chrs[i]);
        if(idx != -1) {
            if(idx > withMax) {
                idx = withMax;
            }
            buffer.append(withChrs[idx]);
        } else {
            buffer.append(chrs[i]);
        }
    }
    return buffer.toString();
}
 
Example 14
Source File: DurationFormatUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>The internal method to do the formatting.</p>
 * 
 * @param tokens  the tokens
 * @param years  the number of years
 * @param months  the number of months
 * @param days  the number of days
 * @param hours  the number of hours
 * @param minutes  the number of minutes
 * @param seconds  the number of seconds
 * @param milliseconds  the number of millis
 * @param padWithZeros  whether to pad
 * @return the formatted string
 */
static String format(Token[] tokens, int years, int months, int days, int hours, int minutes, int seconds,
        int milliseconds, boolean padWithZeros) {
    StrBuilder buffer = new StrBuilder();
    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 StringBuffer) {
            buffer.append(value.toString());
        } else {
            if (value == y) {
                buffer.append(padWithZeros ? StringUtils.leftPad(Integer.toString(years), count, '0') : Integer
                        .toString(years));
                lastOutputSeconds = false;
            } else if (value == M) {
                buffer.append(padWithZeros ? StringUtils.leftPad(Integer.toString(months), count, '0') : Integer
                        .toString(months));
                lastOutputSeconds = false;
            } else if (value == d) {
                buffer.append(padWithZeros ? StringUtils.leftPad(Integer.toString(days), count, '0') : Integer
                        .toString(days));
                lastOutputSeconds = false;
            } else if (value == H) {
                buffer.append(padWithZeros ? StringUtils.leftPad(Integer.toString(hours), count, '0') : Integer
                        .toString(hours));
                lastOutputSeconds = false;
            } else if (value == m) {
                buffer.append(padWithZeros ? StringUtils.leftPad(Integer.toString(minutes), count, '0') : Integer
                        .toString(minutes));
                lastOutputSeconds = false;
            } else if (value == s) {
                buffer.append(padWithZeros ? StringUtils.leftPad(Integer.toString(seconds), count, '0') : Integer
                        .toString(seconds));
                lastOutputSeconds = true;
            } else if (value == S) {
                if (lastOutputSeconds) {
                    milliseconds += 1000;
                    String str = padWithZeros
                            ? StringUtils.leftPad(Integer.toString(milliseconds), count, '0')
                            : Integer.toString(milliseconds);
                    buffer.append(str.substring(1));
                } else {
                    buffer.append(padWithZeros
                            ? StringUtils.leftPad(Integer.toString(milliseconds), count, '0')
                            : Integer.toString(milliseconds));
                }
                lastOutputSeconds = false;
            }
        }
    }
    return buffer.toString();
}
 
Example 15
Source File: FastDateFormat.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>Performs the parsing of tokens.</p>
 * 
 * @param pattern  the pattern
 * @param indexRef  index references
 * @return parsed token
 */
protected String parseToken(String pattern, int[] indexRef) {
    StrBuilder buf = new StrBuilder();

    int i = indexRef[0];
    int length = pattern.length();

    char c = pattern.charAt(i);
    if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z') {
        // Scan a run of the same character, which indicates a time
        // pattern.
        buf.append(c);

        while (i + 1 < length) {
            char peek = pattern.charAt(i + 1);
            if (peek == c) {
                buf.append(c);
                i++;
            } else {
                break;
            }
        }
    } else {
        // This will identify token as text.
        buf.append('\'');

        boolean inLiteral = false;

        for (; i < length; i++) {
            c = pattern.charAt(i);

            if (c == '\'') {
                if (i + 1 < length && pattern.charAt(i + 1) == '\'') {
                    // '' is treated as escaped '
                    i++;
                    buf.append(c);
                } else {
                    inLiteral = !inLiteral;
                }
            } else if (!inLiteral &&
                     (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z')) {
                i--;
                break;
            } else {
                buf.append(c);
            }
        }
    }

    indexRef[0] = i;
    return buf.toString();
}
 
Example 16
Source File: StringEscapeUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>Unescapes any Java literals found in the <code>String</code> to a
 * <code>Writer</code>.</p>
 *
 * <p>For example, it will turn a sequence of <code>'\'</code> and
 * <code>'n'</code> into a newline character, unless the <code>'\'</code>
 * is preceded by another <code>'\'</code>.</p>
 * 
 * <p>A <code>null</code> string input has no effect.</p>
 * 
 * @param out  the <code>Writer</code> used to output unescaped characters
 * @param str  the <code>String</code> to unescape, may be null
 * @throws IllegalArgumentException if the Writer is <code>null</code>
 * @throws IOException if error occurs on underlying Writer
 */
public static void unescapeJava(Writer out, String str) throws IOException {
    if (out == null) {
        throw new IllegalArgumentException("The Writer must not be null");
    }
    if (str == null) {
        return;
    }
    int sz = str.length();
    StrBuilder unicode = new StrBuilder(4);
    boolean hadSlash = false;
    boolean inUnicode = false;
    for (int i = 0; i < sz; i++) {
        char ch = str.charAt(i);
        if (inUnicode) {
            // if in unicode, then we're reading unicode
            // values in somehow
            unicode.append(ch);
            if (unicode.length() == 4) {
                // unicode now contains the four hex digits
                // which represents our unicode character
                try {
                    int value = Integer.parseInt(unicode.toString(), 16);
                    out.write((char) value);
                    unicode.setLength(0);
                    inUnicode = false;
                    hadSlash = false;
                } catch (NumberFormatException nfe) {
                    throw new NestableRuntimeException("Unable to parse unicode value: " + unicode, nfe);
                }
            }
            continue;
        }
        if (hadSlash) {
            // handle an escaped value
            hadSlash = false;
            switch (ch) {
                case '\\':
                    out.write('\\');
                    break;
                case '\'':
                    out.write('\'');
                    break;
                case '\"':
                    out.write('"');
                    break;
                case 'r':
                    out.write('\r');
                    break;
                case 'f':
                    out.write('\f');
                    break;
                case 't':
                    out.write('\t');
                    break;
                case 'n':
                    out.write('\n');
                    break;
                case 'b':
                    out.write('\b');
                    break;
                case 'u':
                    {
                        // uh-oh, we're in unicode country....
                        inUnicode = true;
                        break;
                    }
                default :
                    out.write(ch);
                    break;
            }
            continue;
        } else if (ch == '\\') {
            hadSlash = true;
            continue;
        }
        out.write(ch);
    }
    if (hadSlash) {
        // then we're in the weird case of a \ at the end of the
        // string, let's output it anyway.
        out.write('\\');
    }
}
 
Example 17
Source File: StringEscapeUtils.java    From ymate-platform-v2 with Apache License 2.0 4 votes vote down vote up
public static String unescapeJava(String str) {
    if (str == null) {
        return null;
    }
    int sz = str.length();
    StringBuilder out = new StringBuilder();
    StrBuilder unicode = new StrBuilder(4);
    boolean hadSlash = false;
    boolean inUnicode = false;
    for (int i = 0; i < sz; i++) {
        char ch = str.charAt(i);
        if (inUnicode) {
            unicode.append(ch);
            if (unicode.length() == 4) {
                try {
                    int value = Integer.parseInt(unicode.toString(), 16);
                    out.append((char) value);
                    unicode.setLength(0);
                    inUnicode = false;
                    hadSlash = false;
                } catch (NumberFormatException nfe) {
                    throw new NestableRuntimeException("Unable to parse unicode value: " + unicode, nfe);
                }
            }
            continue;
        }
        if (hadSlash) {
            hadSlash = false;
            switch (ch) {
                case '\\':
                    out.append('\\');
                    break;
                case '\'':
                    out.append('\'');
                    break;
                case '\"':
                    out.append('"');
                    break;
                case 'r':
                    out.append('\r');
                    break;
                case 'f':
                    out.append('\f');
                    break;
                case 't':
                    out.append('\t');
                    break;
                case 'n':
                    out.append('\n');
                    break;
                case 'b':
                    out.append('\b');
                    break;
                case 'u': {
                    inUnicode = true;
                    break;
                }
                default:
                    out.append(ch);
                    break;
            }
            continue;
        } else if (ch == '\\') {
            hadSlash = true;
            continue;
        }
        out.append(ch);
    }
    if (hadSlash) {
        out.append('\\');
    }
    return out.toString();
}
 
Example 18
Source File: Range.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>Gets the range as a <code>String</code>.</p>
 *
 * <p>The format of the String is 'Range[<i>min</i>,<i>max</i>]'.</p>
 * 
 * <p>This implementation uses the {@link #getMinimumNumber()} and 
 * {@link #getMaximumNumber()} methods. 
 * Subclasses may be able to optimise this.</p>
 *
 * @return the <code>String</code> representation of this range
 */
public String toString() {
    StrBuilder buf = new StrBuilder(32);
    buf.append("Range[");
    buf.append(getMinimumNumber());
    buf.append(',');
    buf.append(getMaximumNumber());
    buf.append(']');
    return buf.toString();
}