Class CharArrayString


  • public class CharArrayString
    extends java.lang.Object
    An unsafe String class based on a publicly accessible character array. This class aims to provide similar functionality as java.lang.String, but without the overhead of copying at creation time. Consequently, you should only use this class if you have complete control over the underlying memory (i.e., char array).
    Version:
    $Id: CharArrayString.java,v 1.1 2002/09/30 19:09:09 goetz Exp $
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private char[] chars  
      private int end  
      private int len  
      private static int SEED1  
      private static int SEED2  
      private int start  
    • Constructor Summary

      Constructors 
      Constructor Description
      CharArrayString​(char[] charArray)
      Create a new CharArrayString from an array of characters.
      CharArrayString​(char[] charArray, int startPos, int length)
      Create a new CharArrayString from an array of characters.
      CharArrayString​(java.lang.String string)
      Create a new CharArrayString from a String.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      char charAt​(int pos)
      Return the character at a given position.
      CharArrayString copy()
      Copy this string.
      boolean endsWith​(char c)
      Check if we end in a given character.
      boolean endsWith​(CharArrayString string)
      Check of we end in a give string suffix.
      boolean equals​(java.lang.Object o)
      Check for equality with another CharArrayString.
      char[] getChars()
      Get the internal character array.
      int getEnd()
      Get the end position of the string in the internal array.
      int getStart()
      Get the start position of the string in the internal array.
      int hashCode()
      Get the hash code for this object.
      static int hashCode​(char[] charArray, int startPos, int endPos)
      A static method to compute the hash code for a character range in an array.
      static int hashCode​(java.lang.String s)
      Compute a hash-code for a string.
      int indexOf​(char c)
      Find the first occurence of a given char.
      int indexOf​(char c, int offset)
      Find an occurence of a given character after some position.
      int lastIndexOf​(char c)
      Find the last occurence of a character.
      int length()
      Get the length of the string.
      void setChar​(int pos, char c)
      Set the char at a certain position.
      CharArrayString substring​(int startPos)
      Return a substring starting at a given position.
      CharArrayString substring​(int startPos, int endPos)
      Returns a substring.
      java.lang.String toString()
      Return a string representation.
      CharArrayString trim()
      Trim this.
      • Methods inherited from class java.lang.Object

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

      • start

        private final int start
      • len

        private final int len
      • end

        private final int end
      • chars

        private final char[] chars
    • Constructor Detail

      • CharArrayString

        public CharArrayString​(java.lang.String string)
        Create a new CharArrayString from a String.
        Parameters:
        string - The input string. The content of this string is copied.
      • CharArrayString

        public CharArrayString​(char[] charArray)
        Create a new CharArrayString from an array of characters.
        Parameters:
        charArray - The input char array.
      • CharArrayString

        public CharArrayString​(char[] charArray,
                               int startPos,
                               int length)
        Create a new CharArrayString from an array of characters.
        Parameters:
        charArray - The input char array.
        startPos - The start of the string.
        length - The length of the string.
    • Method Detail

      • length

        public int length()
        Get the length of the string.
        Returns:
        The length.
      • getStart

        public int getStart()
        Get the start position of the string in the internal array.
        Returns:
        The start position.
      • getEnd

        public int getEnd()
        Get the end position of the string in the internal array.
        Returns:
        The end position.
      • getChars

        public char[] getChars()
        Get the internal character array.
        Returns:
        The char array.
      • trim

        public CharArrayString trim()
        Trim this.
        Returns:
        A trimmed version.
      • substring

        public CharArrayString substring​(int startPos,
                                         int endPos)
                                  throws java.lang.IndexOutOfBoundsException
        Returns a substring. The position parameters are interpreted relative to the string represented in this object, not the underlying char array. Note that the substring is NOT a copy, but uses the same underlying char array.
        Parameters:
        startPos - The start of the substring.
        endPos - The end of the substring.
        Returns:
        The corresponding substring.
        Throws:
        java.lang.IndexOutOfBoundsException - If the position parameters are not valid string positions.
      • substring

        public CharArrayString substring​(int startPos)
        Return a substring starting at a given position.
        Parameters:
        startPos - The start position of the substring.
        Returns:
        A new substring, starting at startPos.
      • lastIndexOf

        public int lastIndexOf​(char c)
        Find the last occurence of a character.
        Parameters:
        c - The char we're looking for.
        Returns:
        The last position of the character, or -1 if the character is not contained in the string.
      • toString

        public java.lang.String toString()
        Return a string representation.
        Overrides:
        toString in class java.lang.Object
        Returns:
        The string version of this CharArrayString.
      • endsWith

        public boolean endsWith​(CharArrayString string)
        Check of we end in a give string suffix.
        Parameters:
        string - The string suffix we're looking for.
        Returns:
        true iff string is a suffix of this.
      • endsWith

        public boolean endsWith​(char c)
        Check if we end in a given character.
        Parameters:
        c - The character.
        Returns:
        true iff we end in c.
      • charAt

        public char charAt​(int pos)
        Return the character at a given position.
        Parameters:
        pos - The position we're looking for.
        Returns:
        The character at the position.
      • indexOf

        public int indexOf​(char c,
                           int offset)
                    throws java.lang.IndexOutOfBoundsException
        Find an occurence of a given character after some position.
        Parameters:
        c - The char we're looking for.
        offset - An offset after which we start looking.
        Returns:
        The position, or -1 if the char wasn't found.
        Throws:
        java.lang.IndexOutOfBoundsException - If offset is less than 0.
      • indexOf

        public int indexOf​(char c)
        Find the first occurence of a given char.
        Parameters:
        c - The char we're looking for.
        Returns:
        The position of the char, or -1 if the char couldn't be found.
      • setChar

        public void setChar​(int pos,
                            char c)
                     throws java.lang.IndexOutOfBoundsException
        Set the char at a certain position.
        Parameters:
        pos - The position where to set the char.
        c - The char to set.
        Throws:
        java.lang.IndexOutOfBoundsException - If pos is out of bounds.
      • hashCode

        public int hashCode()
        Get the hash code for this object.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        The hash code.
      • hashCode

        public static final int hashCode​(char[] charArray,
                                         int startPos,
                                         int endPos)
        A static method to compute the hash code for a character range in an array.
        Parameters:
        charArray - -
        startPos - -
        endPos - -
        Returns:
        The hash code.
      • hashCode

        public static final int hashCode​(java.lang.String s)
        Compute a hash-code for a string.
        Parameters:
        s - The string to get the hash code for.
        Returns:
        A hash code.
      • equals

        public boolean equals​(java.lang.Object o)
        Check for equality with another CharArrayString.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - The other string.
        Returns:
        true iff the two strings are equal.