/** * Appends the pad character to the builder the specified number of times. * * @param length the length to append, negative means no append * @param padChar the character to append * @return this, to enable chaining */ public StrBuilder appendPadding(final int length, final char padChar) { if (length >= 0) { ensureCapacity(size + length); for (int i = 0; i < length; i++) { buffer[size++] = padChar; } } return this; }
/** * Appends a boolean value to the string builder. * * @param value the value to append * @return this, to enable chaining */ public StrBuilder append(final boolean value) { if (value) { ensureCapacity(size + 4); buffer[size++] = 't'; buffer[size++] = 'r'; buffer[size++] = 'u'; buffer[size++] = 'e'; } else { ensureCapacity(size + 5); buffer[size++] = 'f'; buffer[size++] = 'a'; buffer[size++] = 'l'; buffer[size++] = 's'; buffer[size++] = 'e'; } return this; }
/** * Internal method to delete a range without validation. * * @param startIndex the start index, must be valid * @param endIndex the end index (exclusive), must be valid * @param removeLen the length to remove (endIndex - startIndex), must be valid * @param insertStr the string to replace with, null means delete range * @param insertLen the length of the insert string, must be valid * @throws IndexOutOfBoundsException if any index is invalid */ private void replaceImpl(final int startIndex, final int endIndex, final int removeLen, final String insertStr, final int insertLen) { final int newSize = size - removeLen + insertLen; if (insertLen != removeLen) { ensureCapacity(newSize); System.arraycopy(buffer, endIndex, buffer, startIndex + insertLen, size - endIndex); size = newSize; } if (insertLen > 0) { insertStr.getChars(0, insertLen, buffer, startIndex); } }
/** * Appends a char value to the string builder. * * @param ch the value to append * @return this, to enable chaining * @since 3.0 */ @Override public StrBuilder append(final char ch) { final int len = length(); ensureCapacity(len + 1); buffer[size++] = ch; return this; }
/** * Inserts the value into this builder. * * @param index the index to add at, must be valid * @param value the value to insert * @return this, to enable chaining * @throws IndexOutOfBoundsException if the index is invalid */ public StrBuilder insert(final int index, final char value) { validateIndex(index); ensureCapacity(size + 1); System.arraycopy(buffer, index, buffer, index + 1, size - index); buffer[index] = value; size++; return this; }
/** * Inserts the value into this builder. * * @param index the index to add at, must be valid * @param value the value to insert * @return this, to enable chaining * @throws IndexOutOfBoundsException if the index is invalid */ public StrBuilder insert(int index, final boolean value) { validateIndex(index); if (value) { ensureCapacity(size + 4); System.arraycopy(buffer, index, buffer, index + 4, size - index); buffer[index++] = 't'; buffer[index++] = 'r'; buffer[index++] = 'u'; buffer[index] = 'e'; size += 4; } else { ensureCapacity(size + 5); System.arraycopy(buffer, index, buffer, index + 5, size - index); buffer[index++] = 'f'; buffer[index++] = 'a'; buffer[index++] = 'l'; buffer[index++] = 's'; buffer[index] = 'e'; size += 5; } return this; }
/** * Inserts the string into this builder. * Inserting null will use the stored null text value. * * @param index the index to add at, must be valid * @param str the string to insert * @return this, to enable chaining * @throws IndexOutOfBoundsException if the index is invalid */ public StrBuilder insert(final int index, String str) { validateIndex(index); if (str == null) { str = nullText; } if (str != null) { final int strLen = str.length(); if (strLen > 0) { final int newSize = size + strLen; ensureCapacity(newSize); System.arraycopy(buffer, index, buffer, index + strLen, size - index); size = newSize; str.getChars(0, strLen, buffer, index); } } return this; }
/** * Updates the length of the builder by either dropping the last characters * or adding filler of Unicode zero. * * @param length the length to set to, must be zero or positive * @return this, to enable chaining * @throws IndexOutOfBoundsException if the length is negative */ public StrBuilder setLength(final int length) { if (length < 0) { throw new StringIndexOutOfBoundsException(length); } if (length < size) { size = length; } else if (length > size) { ensureCapacity(length); final int oldEnd = size; final int newEnd = length; size = length; for (int i = oldEnd; i < newEnd; i++) { buffer[i] = CharUtils.NUL; } } return this; }
/** * Appends a string to this string builder. * Appending null will call {@link #appendNull()}. * * @param str the string to append * @return this, to enable chaining */ public StrBuilder append(final String str) { if (str == null) { return appendNull(); } final int strLen = str.length(); if (strLen > 0) { final int len = length(); ensureCapacity(len + strLen); str.getChars(0, strLen, buffer, len); size += strLen; } return this; }
/** * Appends a string buffer to this string builder. * Appending null will call {@link #appendNull()}. * * @param str the string buffer to append * @return this, to enable chaining */ public StrBuilder append(final StringBuffer str) { if (str == null) { return appendNull(); } final int strLen = str.length(); if (strLen > 0) { final int len = length(); ensureCapacity(len + strLen); str.getChars(0, strLen, buffer, len); size += strLen; } return this; }
/** * Inserts the character array into this builder. * Inserting null will use the stored null text value. * * @param index the index to add at, must be valid * @param chars the char array to insert * @return this, to enable chaining * @throws IndexOutOfBoundsException if the index is invalid */ public StrBuilder insert(final int index, final char chars[]) { validateIndex(index); if (chars == null) { return insert(index, nullText); } final int len = chars.length; if (len > 0) { ensureCapacity(size + len); System.arraycopy(buffer, index, buffer, index + len, size - index); System.arraycopy(chars, 0, buffer, index, len); size += len; } return this; }
/** * Appends a StringBuilder to this string builder. * Appending null will call {@link #appendNull()}. * * @param str the StringBuilder to append * @return this, to enable chaining * @since 3.2 */ public StrBuilder append(final StringBuilder str) { if (str == null) { return appendNull(); } final int strLen = str.length(); if (strLen > 0) { final int len = length(); ensureCapacity(len + strLen); str.getChars(0, strLen, buffer, len); size += strLen; } return this; }
/** * Appends a char array to the string builder. * Appending null will call {@link #appendNull()}. * * @param chars the char array to append * @return this, to enable chaining */ public StrBuilder append(final char[] chars) { if (chars == null) { return appendNull(); } final int strLen = chars.length; if (strLen > 0) { final int len = length(); ensureCapacity(len + strLen); System.arraycopy(chars, 0, buffer, len, strLen); size += strLen; } return this; }
/** * Appends another string builder to this string builder. * Appending null will call {@link #appendNull()}. * * @param str the string builder to append * @return this, to enable chaining */ public StrBuilder append(final StrBuilder str) { if (str == null) { return appendNull(); } final int strLen = str.length(); if (strLen > 0) { final int len = length(); ensureCapacity(len + strLen); System.arraycopy(str.buffer, 0, buffer, len, strLen); size += strLen; } return this; }
/** * Appends the contents of a char buffer to this string builder. * Appending null will call {@link #appendNull()}. * * @param buf the char buffer to append * @return this, to enable chaining * @since 3.4 */ public StrBuilder append(final CharBuffer buf) { if (buf == null) { return appendNull(); } if (buf.hasArray()) { final int length = buf.remaining(); final int len = length(); ensureCapacity(len + length); System.arraycopy(buf.array(), buf.arrayOffset() + buf.position(), buffer, len, length); size += length; } else { append(buf.toString()); } return this; }
/** * Appends part of a StringBuilder to this string builder. * Appending null will call {@link #appendNull()}. * * @param str the StringBuilder to append * @param startIndex the start index, inclusive, must be valid * @param length the length to append, must be valid * @return this, to enable chaining * @since 3.2 */ public StrBuilder append(final StringBuilder str, final int startIndex, final int length) { if (str == null) { return appendNull(); } if (startIndex < 0 || startIndex > str.length()) { throw new StringIndexOutOfBoundsException("startIndex must be valid"); } if (length < 0 || (startIndex + length) > str.length()) { throw new StringIndexOutOfBoundsException("length must be valid"); } if (length > 0) { final int len = length(); ensureCapacity(len + length); str.getChars(startIndex, startIndex + length, buffer, len); size += length; } return this; }
/** * Appends a char array to the string builder. * Appending null will call {@link #appendNull()}. * * @param chars the char array to append * @param startIndex the start index, inclusive, must be valid * @param length the length to append, must be valid * @return this, to enable chaining */ public StrBuilder append(final char[] chars, final int startIndex, final int length) { if (chars == null) { return appendNull(); } if (startIndex < 0 || startIndex > chars.length) { throw new StringIndexOutOfBoundsException("Invalid startIndex: " + length); } if (length < 0 || (startIndex + length) > chars.length) { throw new StringIndexOutOfBoundsException("Invalid length: " + length); } if (length > 0) { final int len = length(); ensureCapacity(len + length); System.arraycopy(chars, startIndex, buffer, len, length); size += length; } return this; }
@Test public void testEnsureCapacity() { final StrBuilder sb = new StrBuilder(); sb.ensureCapacity(2); assertTrue(sb.capacity() >= 2); sb.ensureCapacity(-1); assertTrue(sb.capacity() >= 0); sb.append("HelloWorld"); sb.ensureCapacity(40); assertTrue(sb.capacity() >= 40); }
/** * Appends part of a string builder to this string builder. * Appending null will call {@link #appendNull()}. * * @param str the string to append * @param startIndex the start index, inclusive, must be valid * @param length the length to append, must be valid * @return this, to enable chaining */ public StrBuilder append(final StrBuilder str, final int startIndex, final int length) { if (str == null) { return appendNull(); } if (startIndex < 0 || startIndex > str.length()) { throw new StringIndexOutOfBoundsException("startIndex must be valid"); } if (length < 0 || (startIndex + length) > str.length()) { throw new StringIndexOutOfBoundsException("length must be valid"); } if (length > 0) { final int len = length(); ensureCapacity(len + length); str.getChars(startIndex, startIndex + length, buffer, len); size += length; } return this; }
@Test public void testChaining() { final StrBuilder sb = new StrBuilder(); assertSame(sb, sb.setNewLineText(null)); assertSame(sb, sb.setNullText(null)); assertSame(sb, sb.setLength(1)); assertSame(sb, sb.setCharAt(0, 'a')); assertSame(sb, sb.ensureCapacity(0)); assertSame(sb, sb.minimizeCapacity()); assertSame(sb, sb.clear()); assertSame(sb, sb.reverse()); assertSame(sb, sb.trim()); }