/** * Copies the character array into the specified array. * * @param destination the destination array, null will cause an array to be created * @return the input array, unless that was null or too small */ public char[] getChars(char[] destination) { final int len = length(); if (destination == null || destination.length < len) { destination = new char[len]; } System.arraycopy(buffer, 0, destination, 0, len); return destination; }
/** * Replaces all the occurrences of variables within the given source * builder with their matching values from the resolver. * * @param source the builder to replace in, updated, null returns zero * @return true if altered */ public boolean replaceIn(final StrBuilder source) { if (source == null) { return false; } return substitute(source, 0, source.length()); }
/** * 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; }
/** * Gets the character at the specified index. * * @see #setCharAt(int, char) * @see #deleteCharAt(int) * @param index the index to retrieve, must be valid * @return the character at the index * @throws IndexOutOfBoundsException if the index is invalid */ @Override public char charAt(final int index) { if (index < 0 || index >= length()) { throw new StringIndexOutOfBoundsException(index); } return buffer[index]; }
/** * Sets the character at the specified index. * * @see #charAt(int) * @see #deleteCharAt(int) * @param index the index to set * @param ch the new character * @return this, to enable chaining * @throws IndexOutOfBoundsException if the index is invalid */ public StrBuilder setCharAt(final int index, final char ch) { if (index < 0 || index >= length()) { throw new StringIndexOutOfBoundsException(index); } buffer[index] = ch; 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 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; }
/** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source builder as a template. * The builder is not altered by this method. * * @param source the builder to use as a template, not changed, null returns null * @return the result of the replace operation */ public String replace(final StrBuilder source) { if (source == null) { return null; } final StrBuilder buf = new StrBuilder(source.length()).append(source); substitute(buf, 0, buf.length()); return buf.toString(); }
/** * 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; }
/** * Copies the character array into the specified array. * * @param startIndex first index to copy, inclusive, must be valid * @param endIndex last index, exclusive, must be valid * @param destination the destination array, must not be null or too small * @param destinationIndex the index to start copying in destination * @throws NullPointerException if the array is null * @throws IndexOutOfBoundsException if any index is invalid */ public void getChars(final int startIndex, final int endIndex, final char destination[], final int destinationIndex) { if (startIndex < 0) { throw new StringIndexOutOfBoundsException(startIndex); } if (endIndex < 0 || endIndex > length()) { throw new StringIndexOutOfBoundsException(endIndex); } if (startIndex > endIndex) { throw new StringIndexOutOfBoundsException("end < start"); } System.arraycopy(buffer, startIndex, destination, destinationIndex, endIndex - startIndex); }
/** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source buffer as a template. * The buffer is not altered by this method. * * @param source the buffer to use as a template, not changed, null returns null * @return the result of the replace operation */ public String replace(final StringBuffer source) { if (source == null) { return null; } final StrBuilder buf = new StrBuilder(source.length()).append(source); substitute(buf, 0, buf.length()); return buf.toString(); }
/** * Replaces all the occurrences of variables in the given source object with * their matching values from the resolver. The input source object is * converted to a string using <code>toString</code> and is not altered. * * @param source the source to replace in, null returns null * @return the result of the replace operation */ public String replace(final Object source) { if (source == null) { return null; } final StrBuilder buf = new StrBuilder().append(source); substitute(buf, 0, buf.length()); return buf.toString(); }
@Test public void testReplace_StrMatcher_String_int_int_int_VaryMatcher() { StrBuilder sb = new StrBuilder("abcbccba"); sb.replace(null, "x", 0, sb.length(), -1); assertEquals("abcbccba", sb.toString()); sb.replace(StrMatcher.charMatcher('a'), "x", 0, sb.length(), -1); assertEquals("xbcbccbx", sb.toString()); sb.replace(StrMatcher.stringMatcher("cb"), "x", 0, sb.length(), -1); assertEquals("xbxcxx", sb.toString()); sb = new StrBuilder("A1-A2A3-A4"); sb.replace(A_NUMBER_MATCHER, "***", 0, sb.length(), -1); assertEquals("***-******-***", sb.toString()); sb = new StrBuilder(); sb.replace(A_NUMBER_MATCHER, "***", 0, sb.length(), -1); assertEquals("", sb.toString()); }
@Test public void testClear() { final StrBuilder sb = new StrBuilder(); sb.append("Hello"); sb.clear(); assertEquals(0, sb.length()); assertTrue(sb.buffer.length >= 5); }
@Test public void testReplace_StrMatcher_String_int_int_int_VaryReplace() { StrBuilder sb = new StrBuilder("abcbccba"); sb.replace(StrMatcher.stringMatcher("cb"), "cb", 0, sb.length(), -1); assertEquals("abcbccba", sb.toString()); sb = new StrBuilder("abcbccba"); sb.replace(StrMatcher.stringMatcher("cb"), "-", 0, sb.length(), -1); assertEquals("ab-c-a", sb.toString()); sb = new StrBuilder("abcbccba"); sb.replace(StrMatcher.stringMatcher("cb"), "+++", 0, sb.length(), -1); assertEquals("ab+++c+++a", sb.toString()); sb = new StrBuilder("abcbccba"); sb.replace(StrMatcher.stringMatcher("cb"), "", 0, sb.length(), -1); assertEquals("abca", sb.toString()); sb = new StrBuilder("abcbccba"); sb.replace(StrMatcher.stringMatcher("cb"), null, 0, sb.length(), -1); assertEquals("abca", sb.toString()); }
@Test public void testLastIndexOf_char_int() { StrBuilder sb = new StrBuilder("abab"); assertEquals(-1, sb.lastIndexOf('a', -1)); assertEquals(0, sb.lastIndexOf('a', 0)); assertEquals(0, sb.lastIndexOf('a', 1)); // should work like String#lastIndexOf assertEquals("abab".lastIndexOf('a', 1), sb.lastIndexOf('a', 1)); assertEquals(1, sb.lastIndexOf('b', 2)); assertEquals("abab".lastIndexOf('b', 2), sb.lastIndexOf('b', 2)); assertEquals(-1, sb.lastIndexOf('z', 2)); sb = new StrBuilder("xyzabc"); assertEquals(2, sb.lastIndexOf('z', sb.length())); assertEquals(-1, sb.lastIndexOf('z', 1)); }
@Test public void testAppendPadding() { final StrBuilder sb = new StrBuilder(); sb.append("foo"); assertEquals("foo", sb.toString()); sb.appendPadding(-1, '-'); assertEquals("foo", sb.toString()); sb.appendPadding(0, '-'); assertEquals("foo", sb.toString()); sb.appendPadding(1, '-'); assertEquals("foo-", sb.toString()); sb.appendPadding(16, '-'); assertEquals(20, sb.length()); // 12345678901234567890 assertEquals("foo-----------------", sb.toString()); }