All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.util.StringBuilderPlus Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util;

import java.io.IOException;
import java.io.OutputStream;

//------------------------------------------------------------------------------
/**
 * A StringBuilder with extra functionality [delimtedAppend(), printf(), appendln(),
 * replaceAll(), and carriage return support].
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

// Can't extend StringBuilder since it is is a final class so we just have to wrap it.
public class StringBuilderPlus extends OutputStream implements CharSequence
{
   private StringBuilder mBuffer;
   private String        mDelimiter     = sDefaultDelimiter;
   private String        mLineSeparator = sDefaultLineSeparator;
   private boolean       mCarriageReturnEnabled;

   private static String sDefaultDelimiter     = ", ";
   private static String sDefaultLineSeparator = System.getProperty("line.separator");

   //##########################################################################
   // CONSTRUCTORS
   //##########################################################################

   //--------------------------------------------------------------------------
   public StringBuilderPlus()
   {
      mBuffer = new StringBuilder();
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus(int inSize)
   {
      mBuffer = new StringBuilder(inSize);
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus(CharSequence inContent)
   {
      mBuffer = new StringBuilder();
      if (inContent != null)
      {
         mBuffer.append(inContent);
      }
   }

   //##########################################################################
   // PUBLIC METHODS
   //##########################################################################

   //--------------------------------------------------------------------------
   /**
    Sets an override for the default line separator of System.getProperty("line.separator").
    */
   public static void setDefaultLineSeparator(String inValue)
   {
      sDefaultLineSeparator = inValue;
   }

   //--------------------------------------------------------------------------
   public static void setDefaultDelimiter(String inValue)
   {
      sDefaultDelimiter = inValue;
   }
   

   //--------------------------------------------------------------------------
   public StringBuilderPlus setLineSeparator(String inValue)
   {
      mLineSeparator = inValue;

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus setDelimiter(String inValue)
   {
      mDelimiter = inValue;

      return this;
   }

   //--------------------------------------------------------------------------
   public String getDelimiter()
   {
      return mDelimiter;
   }

   //--------------------------------------------------------------------------
   public void moveCharAt(int inFromIndex, int intToIndex)
   {
      char charToMove = mBuffer.charAt(inFromIndex);
      mBuffer.insert(inFromIndex < intToIndex ? intToIndex + 1 : intToIndex, charToMove);
      mBuffer.deleteCharAt(inFromIndex < intToIndex ? inFromIndex : inFromIndex + 1);
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus replaceAll(Character inTarget, Character inReplacement)
   {
      return replaceAll(inTarget + "", inReplacement != null ? inReplacement + "" : null);
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus replaceAll(CharSequence inTarget, Character inReplacement)
   {
      return replaceAll(inTarget, inReplacement != null ? inReplacement + "" : null);
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus replaceAll(Character inTarget, CharSequence inReplacement)
   {
      return replaceAll(inTarget + "", inReplacement);
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus replaceAll(CharSequence inTarget, CharSequence inReplacement)
   {
      String replacement = inReplacement != null ? inReplacement + "" : "";

      int index;
      int fromIndex = 0;
      while ((index = indexOf(inTarget + "", fromIndex)) >= 0)
      {
         replace(index, index + inTarget.length(), replacement);
         fromIndex = index + replacement.length();
      }

      return this;
   }

   //--------------------------------------------------------------------------
   /**
    Causes any '\r' characters to be immediately interpreted as carriage returns.
    * @return this StringBuilderPlus object to allow method chaining.
    */
   public StringBuilderPlus enableCarriageReturnSupport()
   {
      mCarriageReturnEnabled = true;

      // Process any pre-existing carriage returns
      if (length() > 0
          && indexOf("\r") >= 0)
      {
         processAddedContentForCarriageReturns(toString());
      }

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus disableCarriageReturnSupport()
   {
      mCarriageReturnEnabled = false;
      return this;
   }

   //--------------------------------------------------------------------------
   public boolean startsWith(String inValue)
   {
      return toString().startsWith(inValue);
   }

   //--------------------------------------------------------------------------
   public boolean endsWith(String inValue)
   {
      return toString().endsWith(inValue);
   }

   // OutputStream methods

   //--------------------------------------------------------------------------
   @Override
   public void write(int inByte)
         throws IOException
   {
      append((char) inByte);
   }

   //--------------------------------------------------------------------------
   @Override
   public void write(byte inBytes[], int inOffset, int inLength)
   {
      append(new String(inBytes, inOffset, inLength));
   }

   //--------------------------------------------------------------------------
   @Override
   public void write(byte inBytes[])
   {
      append(new String(inBytes));
   }



   //--------------------------------------------------------------------------
   public StringBuilderPlus delimitedAppend(CharSequence inContent)
   {
      delimitIfNecessary();

      append(inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus delimitedAppend(char inContent)
   {
      delimitIfNecessary();

      append(inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus delimitedAppend(int inContent)
   {
      delimitIfNecessary();

      append(inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus delimitedAppend(long inContent)
   {
      delimitIfNecessary();

      append(inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus delimitedAppend(float inContent)
   {
      delimitIfNecessary();

      append(inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus delimitedAppend(double inContent)
   {
      delimitIfNecessary();

      append(inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus delimitedAppend(Object inContent)
   {
      delimitIfNecessary();

      append(inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus printf(String inFormatString, Object... inArgs)
   {
      return append(String.format(inFormatString, inArgs));
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus appendln(CharSequence inContent)
   {
      append(inContent);
      mBuffer.append(mLineSeparator);
      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus appendln()
   {
      mBuffer.append(mLineSeparator);

      return this;
   }

   // Methods from StringBuilder:

   //--------------------------------------------------------------------------
   public StringBuilderPlus append(Object inObj)
   {
      mBuffer.append(inObj);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus append(CharSequence inContent)
   {
      CharSequence content = inContent;
      if (mCarriageReturnEnabled
            && inContent != null
            && inContent.toString().contains("\r"))
      {
         content = processAddedContentForCarriageReturns(inContent);
         if (content.charAt(0) == '\r')
         {
            int lastLineFeedIndex = lastIndexOf("\n");
            replace(lastLineFeedIndex + 1, length(), "");
            // Remove the '\r' from the beginning of the added content
            content = content.subSequence(1, content.length());
         }
      }

      mBuffer.append(content);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus append(CharSequence inContent, int inStart, int inEnd)
   {
      mBuffer.append(inContent, inStart, inEnd);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus append(char[] inContent)
   {
      mBuffer.append(inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus append(char[] inContent, int inOffset, int inLen)
   {
      mBuffer.append(inContent, inOffset, inLen);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus append(boolean inContent)
   {
      mBuffer.append(inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus append(char inContent)
   {
      if (mCarriageReturnEnabled
            && inContent == '\r')
      {
         append(inContent + "");
      }
      else
      {
         mBuffer.append(inContent);
      }

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus append(int inContent)
   {
      mBuffer.append(inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus append(long inContent)
   {
      mBuffer.append(inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus append(float inContent)
   {
      mBuffer.append(inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus append(double inContent)
   {
      mBuffer.append(inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus appendCodePoint(int inContent)
   {
      mBuffer.appendCodePoint(inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus delete(int inStart, int inEnd)
   {
      mBuffer.delete(inStart, inEnd);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus deleteCharAt(int inIndex)
   {
      mBuffer.deleteCharAt(inIndex);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus replace(int inStart, int inEnd, String inReplacement)
   {
      mBuffer.replace(inStart, inEnd, inReplacement);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus insert(int inIndex, char[] inContent)
   {
      mBuffer.insert(inIndex, inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus insert(int inIndex, char[] inContent, int inOffset, int inLen)
   {
      mBuffer.insert(inIndex, inContent, inOffset, inLen);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus insert(int inIndex, Object inContent)
   {
      mBuffer.insert(inIndex, inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus insert(int inIndex, CharSequence inContent)
   {
      mBuffer.insert(inIndex, inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus insert(int inIndex, CharSequence inContent, int inStart, int inEnd)
   {
      mBuffer.insert(inIndex, inContent, inStart, inEnd);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus insert(int inIndex, boolean inContent)
   {
      mBuffer.insert(inIndex, inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus insert(int inIndex, char inContent)
   {
      mBuffer.insert(inIndex, inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus insert(int inIndex, int inContent)
   {
      mBuffer.insert(inIndex, inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus insert(int inIndex, long inContent)
   {
      mBuffer.insert(inIndex, inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus insert(int inIndex, float inContent)
   {
      mBuffer.insert(inIndex, inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus insert(int inIndex, double inContent)
   {
      mBuffer.insert(inIndex, inContent);

      return this;
   }

   //--------------------------------------------------------------------------
   public int indexOf(String inString)
   {
      return mBuffer.indexOf(inString);
   }

   //--------------------------------------------------------------------------
   public int indexOf(String inString, int inFromIndex)
   {
      return mBuffer.indexOf(inString, inFromIndex);
   }

   //--------------------------------------------------------------------------
   public int lastIndexOf(String inString)
   {
      return mBuffer.lastIndexOf(inString);
   }

   //--------------------------------------------------------------------------
   public int lastIndexOf(String inString, int inFromIndex)
   {
      return mBuffer.lastIndexOf(inString, inFromIndex);
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus reverse()
   {
      mBuffer.reverse();

      return this;
   }

   //--------------------------------------------------------------------------
   @Override
   public String toString()
   {
      return mBuffer.toString();
   }

   //--------------------------------------------------------------------------
   public int length()
   {
      return mBuffer.length();
   }

   //--------------------------------------------------------------------------
   public int capacity()
   {
      return mBuffer.capacity();
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus ensureCapacity(int inMinimumCapacity)
   {
      mBuffer.ensureCapacity(inMinimumCapacity);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus trimToSize()
   {
      mBuffer.trimToSize();

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus setLength(int inNewLength)
   {
      mBuffer.setLength(inNewLength);

      return this;
   }

   //--------------------------------------------------------------------------
   public char charAt(int inIndex)
   {
      return mBuffer.charAt(inIndex);
   }

   //--------------------------------------------------------------------------
   public int codePointAt(int inIndex)
   {
      return mBuffer.codePointAt(inIndex);
   }

   //--------------------------------------------------------------------------
   public int codePointBefore(int inIndex)
   {
      return mBuffer.codePointBefore(inIndex);
   }

   //--------------------------------------------------------------------------
   public int codePointCount(int inBeginIndex, int inEndIndex)
   {
      return mBuffer.codePointCount(inBeginIndex, inEndIndex);
   }

   //--------------------------------------------------------------------------
   public int offsetByCodePoints(int inIndex, int inCodePointOffset)
   {
      return mBuffer.offsetByCodePoints(inIndex, inCodePointOffset);
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus getChars(int inSrcBegin, int inSrcEnd, char[] inDest, int inDestBegin)
   {
      mBuffer.getChars(inSrcBegin, inSrcEnd, inDest, inDestBegin);

      return this;
   }

   //--------------------------------------------------------------------------
   public StringBuilderPlus setCharAt(int inIndex, char inChar)
   {
      mBuffer.setCharAt(inIndex, inChar);

      return this;
   }

   //--------------------------------------------------------------------------
   public String substring(int inStart)
   {
      return mBuffer.substring(inStart);
   }

   //--------------------------------------------------------------------------
   public String substring(int inStart, int inEnd)
   {
      return mBuffer.substring(inStart, inEnd);
   }

   //--------------------------------------------------------------------------
   public CharSequence subSequence(int inStart, int inEnd)
   {
      return mBuffer.subSequence(inStart, inEnd);
   }

   //##########################################################################
   // PRIVATE METHODS
   //##########################################################################


   //--------------------------------------------------------------------------
   private void delimitIfNecessary()
   {
      if (length() > 0)
      {
         append(mDelimiter);
      }
   }

   //--------------------------------------------------------------------------
   private CharSequence processAddedContentForCarriageReturns(CharSequence inContent)
   {
      StringBuilderPlus buffer = new StringBuilderPlus(inContent);
      int index = buffer.indexOf("\r", 1);
      while (index > 0)
      {
         int lastLineFeedIndex = buffer.substring(0, index).lastIndexOf('\n');
         buffer.replace(lastLineFeedIndex + 1, index + (lastLineFeedIndex < 0 ? 0 : 1), "");
         index = buffer.indexOf("\r", lastLineFeedIndex);
      }

      return buffer;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy