/* XBN Java: Generically useful, non-GUI Java code. http://sourceforge.net/projects/xbnjava Copyright (C) 1997-2003, Jeff Epstein All rights reserved. Modifications: No Redistribution in binary form, with or without modifications, are permitted provided that the following conditions are met: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * If modifications are made to source code then this license should indicate that fact in the "Modifications" section above. * Neither the author, nor the contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. [NOTE: This license contains NO advertising clause.] */ package xbn.string; /**

A StringOrBuffer that wraps around a java.lang.StringBuffer. See StringBuffer.

Source code:  SOBStringBuffer.java.  Unit tests:  See the unit tests for StringOrBuffer.

@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public class SOBStringBuffer extends StringOrBuffer { private final TCInvisible tci = new TCInvisible(); private StringBuffer sb = null; /**

Create an SOBStringBuffer.

@param s_tr May not be null. **/ public SOBStringBuffer(String s_tr) { //This try block is only necessary to protect against //new SOBStringBuffer((String)null). Without the casting, //the compiler would throw an "ambiguous invocation" //error. try { sb = new StringBuffer(s_tr); } catch(NullPointerException npx) { throwAX("constructor: s_tr is null."); } } /**

Create an SOBStringBuffer.

@param str_buffer The StringBuffer to wrap around. May not be null. **/ public SOBStringBuffer(StringBuffer str_buffer) { //throwAXIfNull is only necessary to protect against //new SOBStringBuffer((StringBuffer)null). Without the //casting, the compiler would throw an "ambiguous //invocation" error. throwAXIfNull(str_buffer, "str_buffer", sCNSTR); sb = str_buffer; } /**

Get the StringBuffer for direct manipulation.

@return The internally-held StringBuffer. **/ public final StringBuffer getStringBuffer() { return sb; } /** @return false **/ public final boolean isString() { return false; } /**

Get some information about this SOBStringBuffer.

@return getStringBuffer().toString() **/ public final String toString() { return getStringBuffer().toString(); } /**

Trim whitespace from the value of this StringOrBuffer.

Equal to [TCInvisible].trim(getStringBuffer())

**/ public final void trim() { tci.trim(getStringBuffer()); } /** @return getStringBuffer().toString().trim() **/ public final String getTrimmed() { return getStringBuffer().toString().trim(); } /** @return getStringBuffer().charAt(i_dx) **/ public final char charAt(int i_dx) { return getStringBuffer().charAt(i_dx); } /**

Append a string onto the end (right).

First executes
   getStringBuffer().append(s_toAppend)

**/ public final void append(String s_toAppend) { getStringBuffer().append(s_toAppend); } /**

Append a string onto the start.

Equal to insert(0, s_toAppend)

**/ public final void appendToLeft(String s_toAppend) { insert(0, s_toAppend); } /**

Insert a string.

Equal to getStringBuffer().insert(i_dx, s_toInsert)

**/ public final void insert(int i_dx, String s_toInsert) { getStringBuffer().insert(i_dx, s_toInsert); } /**

Append a character onto the end (right).

First executes
   getStringBuffer().append(c_toAppend)

@return This SOBStringBuffer (as a StringOrBuffer). **/ public final void append(char c_toAppend) { getStringBuffer().append(c_toAppend); } /**

Append a character onto the start.

Equal to insert(0, c_toAppend)

**/ public final void appendToLeft(char c_toAppend) { insert(0, c_toAppend); } /**

Insert a character.

Equal to getStringBuffer().insert(i_dx, c_toInsert)

**/ public final void insert(int i_dx, char c_toInsert) { getStringBuffer().insert(i_dx, c_toInsert); } /**

Delete a character.

Equal to getStringBuffer().deleteCharAt(i_dx)

**/ public final void deleteCharAt(int i_dx) { getStringBuffer().deleteCharAt(i_dx); } /**

Delete a range of characters.

Equal to getStringBuffer().delete(i_idxLeft, i_idxAfterRight)

**/ public final void delete(int i_idxLeft, int i_idxAfterRight) { getStringBuffer().delete(i_idxLeft, i_idxAfterRight); } /**

Set the length.

Equal to getStringBuffer().setLength(i_newLength)

**/ public final void setLength(int i_newLength) { getStringBuffer().setLength(i_newLength); } /** @return isStringAt(s_toSearchFor, 0) If s_toSearchFor.length is greater than or equal to length.
false If s_toSearchFor.length is less than length. **/ public final boolean startsWith(String s_toSearchFor) { try { if(length() < s_toSearchFor.length()) { return false; } } catch(NullPointerException npx) { throwAXIfNull(s_toSearchFor, "s_toSearchFor", "startsWith"); } return isStringAt(s_toSearchFor, 0); } /** @return isStringAt(s_toSearchFor, (length() - s_toSearchFor.length())) If s_toSearchFor.length is greater than or equal to length.
false If s_toSearchFor.length is less than length. **/ public final boolean endsWith(String s_toSearchFor) { try { if(length() < s_toSearchFor.length()) { return false; } } catch(NullPointerException npx) { throwAXIfNull(s_toSearchFor, "s_toSearchFor", "endsWith"); } return isStringAt(s_toSearchFor, (length() - s_toSearchFor.length())); } /** @return getStringBuffer().length() **/ public final int length() { return getStringBuffer().length(); } /** @return getStringBuffer().substring(i_idxLeft) **/ public final String substring(int i_idxLeft) { return getStringBuffer().substring(i_idxLeft); } /** @return getStringBuffer().substring(i_idxLeft, i_idxAfterRight) **/ public final String substring(int i_idxLeft, int i_idxAfterRight) { return getStringBuffer().substring(i_idxLeft, i_idxAfterRight); } }