Class CharArray


  • public class CharArray
    extends java.lang.Object
    A leaner, meaner version of StringBuffer.

    It provides basic functionality to handle dynamically-growing char arrays as quickly as possible. This class is not threadsafe.

    Author:
    Chris Miller
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) char[] buffer  
      (package private) int size  
      (package private) int subStrLen  
      (package private) int subStrStart  
    • Constructor Summary

      Constructors 
      Constructor Description
      CharArray​(int size)
      Constructs a CharArray that is initialized to the specified size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      CharArray append​(char c)
      Appends a single character to the end of the character array.
      CharArray append​(char[] chars)
      Appends the supplied characters to the end of the array.
      private CharArray append​(char[] chars, int length)  
      CharArray append​(CharArray chars)
      Appends an existing CharArray on to this one.
      CharArray append​(java.lang.String str)
      Appends the supplied string to the end of this character array.
      char charAt​(int pos)
      Returns the character that is at the specified position in the array.
      boolean compareLower​(java.lang.String lowerStr, int offset)
      Compares the supplied uppercase string with the contents of the character array, starting at the offset specified.
      boolean compareLowerSubstr​(java.lang.String lowerStr)
      This compares a substring of this character array (as specified by the setSubstr(int, int) method call) with the supplied string.
      java.lang.String getLowerSubstr()
      Returns the substring that was specified by the setSubstr(int, int) call.
      private void grow​(int minSize)
      Grows the internal array by either ~100% or minSize (whichever is larger), up to a maximum size of Integer.MAX_VALUE.
      int length()
      Returns the current length of the character array.
      void setLength​(int newSize)
      Changes the size of the character array to the value specified.
      void setSubstr​(int begin, int end)
      Allows an arbitrary substring of this character array to be specified.
      int substrHashCode()
      Returns the hashcode for a lowercase version of the array's substring (as set by the setSubstr(int, int) method).
      java.lang.String substring​(int begin, int end)
      Returns a substring from within this character array.
      java.lang.String toString()
      Returns a String represenation of the character array.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • size

        int size
      • buffer

        char[] buffer
      • subStrStart

        int subStrStart
      • subStrLen

        int subStrLen
    • Constructor Detail

      • CharArray

        public CharArray​(int size)
        Constructs a CharArray that is initialized to the specified size. Do not pass in a negative value because there is no bounds checking!
    • Method Detail

      • toString

        public java.lang.String toString()
        Returns a String represenation of the character array.
        Overrides:
        toString in class java.lang.Object
      • charAt

        public char charAt​(int pos)
        Returns the character that is at the specified position in the array. There is no bounds checking on this method so be sure to pass in a sensible value.
      • setLength

        public void setLength​(int newSize)
        Changes the size of the character array to the value specified. If the new size is less than the current size, the data in the internal array will be truncated. If the new size is <= 0, the array will be reset to empty (but, unlike StringBuffer, the internal array will NOT be shrunk). If the new size is > the current size, the array will be padded out with null characters ('\u0000').
        Parameters:
        newSize - the new size of the character array
      • length

        public int length()
        Returns the current length of the character array.
      • append

        public CharArray append​(CharArray chars)
        Appends an existing CharArray on to this one. Passing in a null CharArray will result in a NullPointerException.
      • append

        public CharArray append​(char[] chars)
        Appends the supplied characters to the end of the array.
      • append

        private CharArray append​(char[] chars,
                                 int length)
      • append

        public CharArray append​(char c)
        Appends a single character to the end of the character array.
      • append

        public CharArray append​(java.lang.String str)
        Appends the supplied string to the end of this character array. Passing in a null string will result in a NullPointerException.
      • substring

        public java.lang.String substring​(int begin,
                                          int end)
        Returns a substring from within this character array. Note that NO range checking is performed!
      • setSubstr

        public void setSubstr​(int begin,
                              int end)
        Allows an arbitrary substring of this character array to be specified. This method should be called prior to calling compareLowerSubstr(String) to set the range of the substring comparison.
        Parameters:
        begin - the starting offset into the character array.
        end - the ending offset into the character array.
      • getLowerSubstr

        public java.lang.String getLowerSubstr()
        Returns the substring that was specified by the setSubstr(int, int) call.
      • compareLowerSubstr

        public boolean compareLowerSubstr​(java.lang.String lowerStr)
        This compares a substring of this character array (as specified by the setSubstr(int, int) method call) with the supplied string. The supplied string must be lowercase, otherwise the comparison will fail.
      • substrHashCode

        public int substrHashCode()
        Returns the hashcode for a lowercase version of the array's substring (as set by the setSubstr(int, int) method). This uses the same calculation as the String.hashCode() method so that it remains compatible with the hashcodes of normal strings.
      • compareLower

        public boolean compareLower​(java.lang.String lowerStr,
                                    int offset)
        Compares the supplied uppercase string with the contents of the character array, starting at the offset specified. This is a specialized method to help speed up the FastPageParser slightly.

        The supplied string is assumed to contain only uppercase ASCII characters. The offset indicates the offset into the character array that the comparison should start from.

        If (and only if) the supplied string and the relevant portion of the character array are considered equal, this method will return true.

      • grow

        private final void grow​(int minSize)
        Grows the internal array by either ~100% or minSize (whichever is larger), up to a maximum size of Integer.MAX_VALUE.