/** * Appends part of a CharSequence to this string builder. * Appending null will call {@link #appendNull()}. * * @param seq the CharSequence 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.0 */ @Override public StrBuilder append(final CharSequence seq, final int startIndex, final int length) { if (seq == null) { return appendNull(); } return append(seq.toString(), startIndex, length); }
/** * 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; }
/** * Appends an object to this string builder. * Appending null will call {@link #appendNull()}. * * @param obj the object to append * @return this, to enable chaining */ public StrBuilder append(final Object obj) { if (obj == null) { return appendNull(); } if (obj instanceof CharSequence) { return append((CharSequence) obj); } return append(obj.toString()); }
/** * 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 part of a string 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 String 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 part of a string buffer 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 StringBuffer 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; }
/** * Appends a CharSequence to this string builder. * Appending null will call {@link #appendNull()}. * * @param seq the CharSequence to append * @return this, to enable chaining * @since 3.0 */ @Override public StrBuilder append(final CharSequence seq) { if (seq == null) { return appendNull(); } if (seq instanceof StrBuilder) { return append((StrBuilder) seq); } if (seq instanceof StringBuilder) { return append((StringBuilder) seq); } if (seq instanceof StringBuffer) { return append((StringBuffer) seq); } if (seq instanceof CharBuffer) { return append((CharBuffer) seq); } return append(seq.toString()); }
/** * 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 testAppend_Object() { final StrBuilder sb = new StrBuilder(); sb.appendNull(); assertEquals("", sb.toString()); sb.append((Object) null); assertEquals("", sb.toString()); sb.append(FOO); assertEquals("foo", sb.toString()); sb.append((StringBuffer) null); assertEquals("foo", sb.toString()); sb.append(new StringBuffer("baz")); assertEquals("foobaz", sb.toString()); sb.append(new StrBuilder("yes")); assertEquals("foobazyes", sb.toString()); sb.append((CharSequence) "Seq"); assertEquals("foobazyesSeq", sb.toString()); sb.append(new StringBuilder("bld")); // Check it supports StringBuilder assertEquals("foobazyesSeqbld", sb.toString()); }
@Test public void testAppendWithNullText() { final StrBuilder sb = new StrBuilder(); sb.setNullText("NULL"); assertEquals("", sb.toString()); sb.appendNull(); assertEquals("NULL", sb.toString()); sb.append((Object) null); assertEquals("NULLNULL", sb.toString()); sb.append(FOO); assertEquals("NULLNULLfoo", sb.toString()); sb.append((String) null); assertEquals("NULLNULLfooNULL", sb.toString()); sb.append(""); assertEquals("NULLNULLfooNULL", sb.toString()); sb.append("bar"); assertEquals("NULLNULLfooNULLbar", sb.toString()); sb.append((StringBuffer) null); assertEquals("NULLNULLfooNULLbarNULL", sb.toString()); sb.append(new StringBuffer("baz")); assertEquals("NULLNULLfooNULLbarNULLbaz", sb.toString()); }
/** * Appends a CharSequence to this string builder. * Appending null will call {@link #appendNull()}. * * @param seq the CharSequence to append * @return this, to enable chaining * @since 3.0 */ @Override public StrBuilder append(final CharSequence seq) { if (seq == null) { return appendNull(); } return append(seq.toString()); }
/** * Appends an object to this string builder. * Appending null will call {@link #appendNull()}. * * @param obj the object to append * @return this, to enable chaining */ public StrBuilder append(final Object obj) { if (obj == null) { return appendNull(); } if (obj instanceof CharSequence) { return append((CharSequence) obj); } return append(obj.toString()); }
/** * Appends an object to this string builder. * Appending null will call {@link #appendNull()}. * * @param obj the object to append * @return this, to enable chaining */ public StrBuilder append(final Object obj) { if (obj == null) { return appendNull(); } if (obj instanceof CharSequence) { return append((CharSequence) obj); } return append(obj.toString()); }