Class CharsWrapper

  • All Implemented Interfaces:
    java.lang.CharSequence, java.lang.Cloneable, java.lang.Iterable<java.lang.Character>

    public final class CharsWrapper
    extends java.lang.Object
    implements java.lang.CharSequence, java.lang.Cloneable, java.lang.Iterable<java.lang.Character>
    A simple, efficient implementation of CharSequence, designed to avoid data copying and to maximize performance.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  CharsWrapper.Builder
      Builder class for constructing CharsWrappers.
    • Constructor Summary

      Constructors 
      Constructor Description
      CharsWrapper​(char... chars)
      Creates a new CharsWrapper backed by the given char array.
      CharsWrapper​(char[] chars, int offset, int limit)
      Creates a new CharsWrapper backed by the given char array.
      CharsWrapper​(java.lang.CharSequence csq)
      Creates a new CharsWrapper containing the same characters as the specified CharSequence.
      CharsWrapper​(java.lang.CharSequence csq, int begin, int end)
      Creates a new CharsWrapper containing the same characters as the specified CharSequence.
      CharsWrapper​(java.lang.String str)
      Creates a new CharsWrapper containing the same characters as the specified String.
      CharsWrapper​(java.lang.String str, int begin, int end)
      Creates a new CharsWrapper containing the same characters as the specified String.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      char charAt​(int index)  
      CharsWrapper clone()
      Creates and returns a copy of this CharsWrapper.
      boolean contains​(char c)
      Checks if this CharsWrapper contains the specified character.
      boolean contentEquals​(char[] array)
      Compares this CharsWrapper to an array of characters.
      boolean contentEquals​(java.lang.CharSequence cs)
      Compares this CharsWrapper to a CharSequence.
      boolean equals​(java.lang.Object obj)  
      boolean equalsIgnoreCase​(java.lang.CharSequence cs)
      Compares this CharsWrapper to a CharSequence, ignoring case considerations.
      char get​(int index)  
      int hashCode()
      Calculates the hash code of this CharsWrapper.
      int indexOf​(char c)
      Returns the index within this CharsWrapper of the first occurrence of the specified character.
      int indexOfFirst​(char... ch)
      Returns the index within this CharsWrapper of the first occurrence of one of the specified characters.
      boolean isEmpty()
      Checks if this CharsWrapper is empty, ie if its length is zero.
      java.util.Iterator<java.lang.Character> iterator()  
      int length()  
      void replaceAll​(char ch, char replacement)
      Replaces all occurences in this Wrapper of a character by another one.
      void set​(int index, char ch)
      Sets the value of a character.
      boolean startsWith​(java.lang.CharSequence cs)
      Checks if this CharsWrapper starts with the same characters as the given CharSequence.
      CharsWrapper subSequence​(int start, int end)
      CharsWrapper subView​(int start)
      Creates a view of a part of this CharsWrapper.
      CharsWrapper subView​(int start, int end)
      Creates a view of a part of this CharsWrapper.
      java.lang.String toString()  
      CharsWrapper trimmedView()
      Creates a trimmed view of this CharsWrapper, with any leading and trailing whitespace removed.
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.CharSequence

        chars, codePoints
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • CharsWrapper

        public CharsWrapper​(char... chars)
        Creates a new CharsWrapper backed by the given char array. Any modification to the array is reflected in the CharsWrapper and vice-versa.
        Parameters:
        chars - the char array to use
      • CharsWrapper

        public CharsWrapper​(char[] chars,
                            int offset,
                            int limit)
        Creates a new CharsWrapper backed by the given char array. Any modification to the array is reflected in the CharsWrapper and vice-versa.
        Parameters:
        chars - the char array to use
        offset - the index (in the array) of the first character to use
        limit - the index +1 (in the array) of the last character to use
      • CharsWrapper

        public CharsWrapper​(java.lang.String str)
        Creates a new CharsWrapper containing the same characters as the specified String. The data is copied and the new CharsWrapper is completely independent.
        Parameters:
        str - the String to copy
      • CharsWrapper

        public CharsWrapper​(java.lang.String str,
                            int begin,
                            int end)
        Creates a new CharsWrapper containing the same characters as the specified String. The data is copied and the new CharsWrapper is completely independent.
        Parameters:
        str - the String to copy
        begin - index of the first character to copy from str
        end - index after the last character to copy from str
      • CharsWrapper

        public CharsWrapper​(java.lang.CharSequence csq)
        Creates a new CharsWrapper containing the same characters as the specified CharSequence. The data is copied and the new CharsWrapper is completely independent.
        Parameters:
        csq - the sequence to copy
      • CharsWrapper

        public CharsWrapper​(java.lang.CharSequence csq,
                            int begin,
                            int end)
        Creates a new CharsWrapper containing the same characters as the specified CharSequence. The data is copied and the new CharsWrapper is completely independent.
        Parameters:
        csq - the sequence to copy
        begin - index of the first character to copy from csq
        end - index after the last character to copy from csq
    • Method Detail

      • isEmpty

        public boolean isEmpty()
        Checks if this CharsWrapper is empty, ie if its length is zero.
        Returns:
        true if it's empty, false otherwise
      • length

        public int length()
        Specified by:
        length in interface java.lang.CharSequence
      • charAt

        public char charAt​(int index)
        Specified by:
        charAt in interface java.lang.CharSequence
      • get

        public char get​(int index)
        Parameters:
        index - the character's index (the first character is at index 0)
        Returns:
        the character at the specified index
      • set

        public void set​(int index,
                        char ch)
        Sets the value of a character.
        Parameters:
        index - the character's index (the first character is at index 0)
        ch - the character value to set
      • replaceAll

        public void replaceAll​(char ch,
                               char replacement)
        Replaces all occurences in this Wrapper of a character by another one.
        Parameters:
        ch - the character to replace
        replacement - the replacement to use
      • contains

        public boolean contains​(char c)
        Checks if this CharsWrapper contains the specified character.
        Parameters:
        c - the character to look for
        Returns:
        true if it contains the character, false if it does not
      • indexOf

        public int indexOf​(char c)
        Returns the index within this CharsWrapper of the first occurrence of the specified character. Returns -1 if this CharsWrapper doesn't contain the character.
        Parameters:
        c - the character to look for
        Returns:
        the index of the first occurence of c, or -1 if not found.
      • indexOfFirst

        public int indexOfFirst​(char... ch)
        Returns the index within this CharsWrapper of the first occurrence of one of the specified characters. Returns -1 if this CharsWrapper doesn't contain any of these characters.
        Parameters:
        ch - the characters to look for
        Returns:
        the index of the first occurence of a character of ch, or -1 if not found.
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • equalsIgnoreCase

        public boolean equalsIgnoreCase​(java.lang.CharSequence cs)
        Compares this CharsWrapper to a CharSequence, ignoring case considerations.
        Parameters:
        cs - the CharSequence to compare with this CharsWrapper
        Returns:
        true if cs isn't null and contains the same characters as this CharsWrapper, ignoring case considerations.
        See Also:
        String.equalsIgnoreCase(String)
      • contentEquals

        public boolean contentEquals​(java.lang.CharSequence cs)
        Compares this CharsWrapper to a CharSequence.
        Parameters:
        cs - the CharSequence to compare with this CharsWrapper
        Returns:
        true if cs isn't null and contains the same characters as this CharsWrapper
        See Also:
        String.contentEquals(CharSequence)
      • contentEquals

        public boolean contentEquals​(char[] array)
        Compares this CharsWrapper to an array of characters.
        Parameters:
        array - the array to compare with this CharsWrapper
        Returns:
        true if the array isn't null and contains the same characters as this CharsWrapper
      • startsWith

        public boolean startsWith​(java.lang.CharSequence cs)
        Checks if this CharsWrapper starts with the same characters as the given CharSequence.
        Parameters:
        cs - the sequence to compare to the beginning of this CharsWrapper
        Returns:
        true if the first characters of this wrapper are the same as the given sequence
      • subSequence

        public CharsWrapper subSequence​(int start,
                                        int end)

        This method copies the data so the returned CharsWrapper doesn't share its array with this CharsWrapper and is completely independant.

        Specified by:
        subSequence in interface java.lang.CharSequence
      • subView

        public CharsWrapper subView​(int start,
                                    int end)
        Creates a view of a part of this CharsWrapper. Any modification to the view is reflected in the original CharsWrapper and vice-versa.
        Parameters:
        start - the start index, inclusive
        end - the end index, exclusive
        Returns:
        a new CharsWrapper that is a view of a part of this CharsWrapper
      • subView

        public CharsWrapper subView​(int start)
        Creates a view of a part of this CharsWrapper. Any modification to the view is reflected in the original CharsWrapper and vice-versa.
        Parameters:
        start - the start index, inclusive
        Returns:
        a new CharsWrapper that is a view of a part of this CharsWrapper
      • trimmedView

        public CharsWrapper trimmedView()
        Creates a trimmed view of this CharsWrapper, with any leading and trailing whitespace removed. Any modification to the view is reflected in the original CharsWrapper and vice-versa.
        Returns:
        a new CharsWrapper that is a trimmed view of this CharsWrapper
        See Also:
        String.trim()
      • toString

        public java.lang.String toString()
        Specified by:
        toString in interface java.lang.CharSequence
        Overrides:
        toString in class java.lang.Object
      • hashCode

        public int hashCode()
        Calculates the hash code of this CharsWrapper.

        Relation to String's hash code

        The hash code calculated by this method is guaranteed to return the same thing as wrapper.toString().hashCode(). That is, if a String and a CharsWrapper contain exactly the same characters then they will have the same hash code.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        a hash code for the current content of this CharsWrapper
        See Also:
        String.hashCode()
      • clone

        public CharsWrapper clone()
        Creates and returns a copy of this CharsWrapper. The underlying char array is copied and used to create a new instance of CharsWrapper.
        Overrides:
        clone in class java.lang.Object
        Returns:
        a copy of this CharsWrapper
      • iterator

        public java.util.Iterator<java.lang.Character> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<java.lang.Character>