Class CharArrayString


  • public class CharArrayString
    extends 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 $
    • 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​(String string)
      Create a new CharArrayString from a String.
    • Constructor Detail

      • CharArrayString

        public CharArrayString​(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 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:
        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 String toString()
        Return a string representation.
        Overrides:
        toString in class 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 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:
        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 IndexOutOfBoundsException
        Set the char at a certain position.
        Parameters:
        pos - The position where to set the char.
        c - The char to set.
        Throws:
        IndexOutOfBoundsException - If pos is out of bounds.
      • hashCode

        public int hashCode()
        Get the hash code for this object.
        Overrides:
        hashCode in class 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​(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​(Object o)
        Check for equality with another CharArrayString.
        Overrides:
        equals in class Object
        Parameters:
        o - The other string.
        Returns:
        true iff the two strings are equal.