/*---------------------------------------------------------------------------*\
  $Id: 2b764feb88f0950cbcfcc3e41b77296fea17e8ba $
  ---------------------------------------------------------------------------
  This software is released under a BSD-style license:

  Copyright (c) 2004-2007 Brian M. Clapper. All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:

  1.  Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.

  2.  The end-user documentation included with the redistribution, if any,
      must include the following acknowlegement:

        "This product includes software developed by Brian M. Clapper
        (bmc@clapper.org, http://www.clapper.org/bmc/). That software is
        copyright (c) 2004-2007 Brian M. Clapper."

      Alternately, this acknowlegement may appear in the software itself,
      if wherever such third-party acknowlegements normally appear.

  3.  Neither the names "clapper.org", "clapper.org Java Utility Library",
      nor any of the names of the project contributors may be used to
      endorse or promote products derived from this software without prior
      written permission. For written permission, please contact
      bmc@clapper.org.

  4.  Products derived from this software may not be called "clapper.org
      Java Utility Library", nor may "clapper.org" appear in their names
      without prior written permission of Brian M. Clapper.

  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 BRIAN M. CLAPPER 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.
\*---------------------------------------------------------------------------*/

package org.clapper.util.text;

/**
 * An XStringBuffer objects wraps a standard Java
 * StringBuffer object, providing a superset of
 * StringBuffer's functionality. (XStringBuffer
 * cannot actually subclass StringBuffer, since
 * StringBuffer is final.) Among the additional methods that this
 * class provides are:
 *
 * 
 *
 * 

Because XStringBuffer wraps a StringBuffer, it is * thread-safe, but it can also be slower than a StringBuilder. * For applications or methods that don't have to worry about thread safety, * consider using the {@link XStringBuilder} class, instead. * * @see java.lang.StringBuffer * @see XStringBuilder * * @version $Revision$ * * @author Copyright © 2004-2007 Brian M. Clapper */ public class XStringBuffer extends XStringBufBase { /*----------------------------------------------------------------------*\ Private Variables \*----------------------------------------------------------------------*/ /** * The underlying string buffer. */ private StringBuffer buf = null; /*----------------------------------------------------------------------*\ Constructor \*----------------------------------------------------------------------*/ /** * Construct an empty XStringBuffer object with a default * initial capacity (the same initial capacity as an empty * StringBuffer object). */ public XStringBuffer() { super(); buf = new StringBuffer(); } /** * Construct an empty XStringBuffer object with the specified * initial capacity. * * @param length The initial capacity */ public XStringBuffer (int length) { super(); buf = new StringBuffer (length); } /** * Construct a XStringBuffer object so that it represents the * same sequence of characters as the String argument. (The * String contrents are copied into the XStringBuffer * object.) * * @param initialContents The initial contents */ public XStringBuffer (String initialContents) { super(); if (initialContents == null) buf = new StringBuffer (); else buf = new StringBuffer (initialContents); } /*----------------------------------------------------------------------*\ Public Methods \*----------------------------------------------------------------------*/ /** * Return the current capacity of the buffer (i.e., the number of * characters left before truncation will occur). * * @return The capacity. A 0 means no more room is left, and all further * inserted or appended characters will cause truncation. * * @see #length */ public int capacity() { return buf.capacity(); } /** * Remove the character at the specified position in this object, * shortening this object by one character. * * @param index Index of the character to remove * * @return This object * * @throws StringIndexOutOfBoundsException if index is negative, * or greater than or equal to * length(). */ public XStringBuffer deleteCharAt (int index) throws StringIndexOutOfBoundsException { buf.deleteCharAt (index); return this; } /** * Ensure that the capacity of the buffer is at least equal to the * specified minimum. If the current capacity of this string buffer is * less than the argument, then a new internal buffer is allocated with * greater capacity. The new capacity is the larger of: * *

* * If the minimumCapacity argument is non-positive, this * method returns without doing anything. * * @param minimumCapacity The minimum desirec capacity */ public void ensureCapacity (int minimumCapacity) { buf.ensureCapacity (minimumCapacity); } /** * Copy the some or all of the contents of the buffer into a character * array. The first character to be copied is at index * srcBegin; the last character to be copied is at index * (srcEnd - 1). The total number of characters to be * copied is (srcEnd - srcBegin). The characters are * copied into the subarray of dst starting at index * dstBegin and ending at index * (dstBegin + (srcEnd - srcBegin) - 1). * * @param srcBegin Start copy from this offset in the string buffer * @param srcEnd Stop copy from this offset in the string buffer * @param dst Where to copy the characters. * @param dstBegin Offset into dst * * @throws IndexOutOfBoundsException invalid index */ public void getChars (int srcBegin, int srcEnd, char dst[], int dstBegin) throws IndexOutOfBoundsException { buf.getChars (srcBegin, srcEnd, dst, dstBegin); } /** * Reverse the contents of the buffer. */ public void reverse() { buf.reverse(); } /** * Set the character at a specified index. The index * parameter must be greater than or equal to 0, and less than the * length of this string buffer. * * @param index The index at which to set the value. * @param ch The character to store * * @throws StringIndexOutOfBoundsException index out of range */ public void setCharAt (int index, char ch) throws StringIndexOutOfBoundsException { buf.setCharAt (index, ch); } /** * Set the length of this XStringBuffer. This string * buffer is altered to represent a new character sequence whose length * is specified by the argument. If the newLength argument * is less than the current length of the string buffer, the string * buffer is truncated to contain exactly the number of characters * given by the newLength argument. If the * newLength argument is greater than the current length * of the string buffer, the buffer is padded with null characters out * to the new length. * * @param newLength the new length of the buffer * * @throws IndexOutOfBoundsException newLength is negative */ public void setLength (int newLength) throws IndexOutOfBoundsException { buf.setLength (newLength); } /** * Return a standard StringBuffer containing a copy of the * contents of this buffer. * * @return The string buffer */ public StringBuffer toStringBuffer() { return new StringBuffer (buf.toString()); } /*----------------------------------------------------------------------*\ Protected Methods \*----------------------------------------------------------------------*/ /** * Get the underlying buffer (e.g., StringBuffer, * StringBuilder) as an Appender object. * * @return the Appender */ protected Appendable getBufferAsAppendable() { return buf; } /** * Get a new instance of the underlying buffer type (e.g., * StringBuffer, StringBuilder) as a * CharSequence object. * * @return the Appendable */ protected CharSequence newBufferAsCharSequence() { return new StringBuffer(); } /** * Get the underlying buffer (e.g., StringBuffer, * StringBuilder) as a CharSequence object. * * @return the Appendable */ protected CharSequence getBufferAsCharSequence() { return buf; } /** * Remove the characters in a substring of this * XStringBuffer. The substring begins at the specified * start and extends to the character at index * end - 1, or to the end of the string, if no such * character exists. If start is equal to end, * no changes are made. * * @param start The beginning index, inclusive * @param end The ending index, exclusive * * @throws IndexOutOfBoundsException if start is negative, * greater than length(), * or greater than end */ protected void deleteCharacters (int start, int end) throws IndexOutOfBoundsException { buf.delete (start, end); } /** * Insert a single character into the buffer at a specified position. * Note that an insertion operation may push characters off the end of * the buffer. * * @param index Where to start inserting in the string buffer * @param ch The character to insert */ protected void insertCharacter (int index, char ch) { buf.insert (index, ch); } /** * Insert characters from a character array into the buffer at a * specified position. Note that an insertion operation may push * characters off the end of the buffer. * * @param index Where to start inserting in the string buffer * @param chars The character(s) to insert * @param offset The starting position in the chars array * @param len The number of characters to insert */ protected synchronized void insertCharacters (int index, char chars[], int offset, int len) { buf.insert (index, chars, offset, len); } /** * Replace the characters in a substring of this buffer with * characters in the specified String. The substring * begins at the specified start and extends to the * character at index end - 1, or to the end of the * XStringBuffer if no such character exists. First the * characters in the substring are removed and then the specified * String is inserted at start. (The * XStringBuffer will be lengthened to accommodate the * specified String if necessary.) * * @param start The beginning index, inclusive * @param end The ending index, exclusive * @param str The string that will replace the previous contents * * @throws IndexOutOfBoundsException if start is negative, * or greater than * length(), or greater * than end */ protected void replaceString (int start, int end, String str) throws IndexOutOfBoundsException { buf.replace (start, end, str); } }

Ads help maintain this website.