RingBuffer implements CharSequence, Serializable { public RingBuffer trim() { trimStart(); trimEnd(); return this; } RingBuffer();  RingBuffer(final int initialSize); private  RingBuffer(final char[] buffer, final int off, final int len); static RingBuffer fromString(final String string); RingBuffer append(final char c); RingBuffer append(final char[] ch, int start, int len); RingBuffer append(final String string); RingBuffer append(final Object obj); void clear(); RingBuffer trimStart(); RingBuffer trimEnd(); RingBuffer trim(); void removeEnd(final int len); void removeStart(final int len); void setLength(final int len); boolean isEmpty(); boolean isBlank(); @Override String toString(); @Override char charAt(final int index); @Override int length(); int capacity(); @Override CharSequence subSequence(final int off, final int len); RingBuffer unsafeCopy(); RingBuffer copy(); char[] toCharArray(); void pad(final int num, final char c); void move(int increment); @Override boolean equals(final Object other); @Override int hashCode();  }
@Test void testTrim() { final RingBuffer buffer = RingBuffer.fromString("0123456789"); buffer.removeEnd(3); assertThat(buffer.toString()).isEqualTo("0123456"); buffer.removeEnd(3); assertThat(buffer.toString()).isEqualTo("0123"); buffer.removeEnd(3); assertThat(buffer.toString()).isEqualTo("0"); try { buffer.removeEnd(3); fail("You can't remove more content than exists"); } catch (final IndexOutOfBoundsException e) { } assertThat(buffer.toString()).isEqualTo("0"); buffer.removeEnd(1); assertThat(buffer.toString()).isEqualTo(""); try { buffer.removeEnd(-1); fail("You can't remove a negative number of characters"); } catch (final IllegalArgumentException e) { } buffer.append("0123456789"); buffer.removeStart(3); assertThat(buffer.toString()).isEqualTo("3456789"); buffer.removeStart(3); assertThat(buffer.toString()).isEqualTo("6789"); buffer.removeStart(3); assertThat(buffer.toString()).isEqualTo("9"); try { buffer.removeStart(3); fail("You can't remove more content than exists"); } catch (final IndexOutOfBoundsException e) { } assertThat(buffer.toString()).isEqualTo("9"); buffer.removeStart(1); assertThat(buffer.toString()).isEqualTo(""); try { buffer.removeStart(-1); fail("You can't remove a negative number of characters"); } catch (final IllegalArgumentException e) { } buffer.append("0123456789"); buffer.trim(); assertThat(buffer.toString()).isEqualTo("0123456789"); buffer.append(" "); assertThat(buffer.toString()).isEqualTo("3456789 "); buffer.trim(); assertThat(buffer.toString()).isEqualTo("3456789"); buffer.append(" "); assertThat(buffer.toString()).isEqualTo("3456789 "); buffer.append(" 0123 "); assertThat(buffer.toString()).isEqualTo(" 0123 "); buffer.trim(); assertThat(buffer.toString()).isEqualTo("0123"); buffer.append(" 0123 "); assertThat(buffer.toString()).isEqualTo(" 0123 "); buffer.trimStart(); assertThat(buffer.toString()).isEqualTo("0123 "); buffer.append(" 0123 "); assertThat(buffer.toString()).isEqualTo(" 0123 "); buffer.trimEnd(); assertThat(buffer.toString()).isEqualTo(" 0123"); }