Class CharSequences


  • public final class CharSequences
    extends java.lang.Object
    8-bit ASCII strings factory and basic utilities helper. This class can work with 8-bit CharSequence instances only, any other input will have undefined behavior.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int asciiStringIndexOf​(java.lang.CharSequence sequence, char c, int fromIndex)
      Find the index of c within sequence starting at index fromIndex.
      static int caseInsensitiveHashCode​(java.lang.CharSequence seq)
      Calculate a hash code of a byte array assuming ASCII character encoding.
      static boolean contentEquals​(java.lang.CharSequence a, java.lang.CharSequence b)
      Returns true if the content of both CharSequence's are equals.
      static boolean contentEqualsIgnoreCase​(java.lang.CharSequence a, java.lang.CharSequence b)
      Perform a case-insensitive comparison of two CharSequences.
      static java.lang.CharSequence emptyAsciiString()
      Get a reference to an unmodifiable empty CharSequence with the same properties as newAsciiString(Buffer).
      static boolean equalsIgnoreCaseLower​(char a, char lowerCaseChar)
      Compare an unknown ascii character a with a known lowercase character lowerCaseChar in a case insensitive manner.
      static int forEachByte​(java.lang.CharSequence sequence, ByteProcessor visitor)
      Iterates over the readable bytes of this CharSequence with the specified ByteProcessor in ascending order.
      static int indexOf​(java.lang.CharSequence sequence, char c, int fromIndex)
      Find the index of c within sequence starting at index fromIndex.
      static boolean isAsciiString​(java.lang.CharSequence sequence)
      Check if the provided CharSequence is an AsciiString, result of a call to newAsciiString(String).
      static java.lang.CharSequence newAsciiString​(Buffer input)
      Create a new CharSequence from the specified input, supporting only 8-bit ASCII characters, and with a case-insensitive hashCode.
      static java.lang.CharSequence newAsciiString​(java.lang.String input)
      Create a new CharSequence from the specified input, supporting only 8-bit ASCII characters, and with a case-insensitive hashCode.
      static boolean regionMatches​(java.lang.CharSequence cs, boolean ignoreCase, int csStart, java.lang.CharSequence string, int start, int length)
      This methods make regionMatches operation correctly for any chars in strings.
      static java.util.List<java.lang.CharSequence> split​(java.lang.CharSequence input, char delimiter, boolean trim)
      Split a given AsciiString to separate ones on the given delimiter.
      static Buffer unwrapBuffer​(java.lang.CharSequence cs)
      Attempt to unwrap a CharSequence and obtain the underlying Buffer if possible.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • newAsciiString

        public static java.lang.CharSequence newAsciiString​(java.lang.String input)
        Create a new CharSequence from the specified input, supporting only 8-bit ASCII characters, and with a case-insensitive hashCode.
        Parameters:
        input - a string containing only 8-bit ASCII characters.
        Returns:
        a CharSequence
      • newAsciiString

        public static java.lang.CharSequence newAsciiString​(Buffer input)
        Create a new CharSequence from the specified input, supporting only 8-bit ASCII characters, and with a case-insensitive hashCode.
        Parameters:
        input - a Buffer containing
        Returns:
        a CharSequence.
      • emptyAsciiString

        public static java.lang.CharSequence emptyAsciiString()
        Get a reference to an unmodifiable empty CharSequence with the same properties as newAsciiString(Buffer).
        Returns:
        a reference to an unmodifiable empty CharSequence with the same properties as newAsciiString(Buffer).
      • isAsciiString

        public static boolean isAsciiString​(java.lang.CharSequence sequence)
        Check if the provided CharSequence is an AsciiString, result of a call to newAsciiString(String).
        Parameters:
        sequence - The CharSequence to check.
        Returns:
        true if the check passes.
      • unwrapBuffer

        @Nullable
        public static Buffer unwrapBuffer​(java.lang.CharSequence cs)
        Attempt to unwrap a CharSequence and obtain the underlying Buffer if possible.
        Parameters:
        cs - the CharSequence to unwrap.
        Returns:
        the underlying Buffer or null.
      • forEachByte

        public static int forEachByte​(java.lang.CharSequence sequence,
                                      ByteProcessor visitor)
        Iterates over the readable bytes of this CharSequence with the specified ByteProcessor in ascending order.
        Parameters:
        sequence - the CharSequence to operate on.
        visitor - the ByteProcessor visitor of each element.
        Returns:
        -1 if the processor iterated to or beyond the end of the readable bytes. The last-visited index If the ByteProcessor.process(byte) returned false.
      • contentEqualsIgnoreCase

        public static boolean contentEqualsIgnoreCase​(@Nullable
                                                      java.lang.CharSequence a,
                                                      @Nullable
                                                      java.lang.CharSequence b)
        Perform a case-insensitive comparison of two CharSequences.

        NOTE: This only supports 8-bit ASCII.

        Parameters:
        a - first CharSequence to compare.
        b - second CharSequence to compare.
        Returns:
        true if both CharSequence's are equals when ignoring the case.
      • contentEquals

        public static boolean contentEquals​(@Nullable
                                            java.lang.CharSequence a,
                                            @Nullable
                                            java.lang.CharSequence b)
        Returns true if the content of both CharSequence's are equals. This only supports 8-bit ASCII.
        Parameters:
        a - left hand side of comparison.
        b - right hand side of comparison.
        Returns:
        true if a's content equals b.
      • indexOf

        public static int indexOf​(java.lang.CharSequence sequence,
                                  char c,
                                  int fromIndex)
        Find the index of c within sequence starting at index fromIndex.
        Parameters:
        sequence - The CharSequence to search in.
        c - The character to find.
        fromIndex - The index to start searching (inclusive).
        Returns:
        The index of c or -1 otherwise.
      • asciiStringIndexOf

        public static int asciiStringIndexOf​(java.lang.CharSequence sequence,
                                             char c,
                                             int fromIndex)
        Find the index of c within sequence starting at index fromIndex. This version avoids instance type check for special occasions that we can know the type before hand. The input is expected to be an AsciiString value; if the input type is not known use the generic indexOf(CharSequence, char, int) instead.
        Parameters:
        sequence - The CharSequence to search in.
        c - The character to find.
        fromIndex - The index to start searching (inclusive).
        Returns:
        The index of c or -1 otherwise.
      • caseInsensitiveHashCode

        public static int caseInsensitiveHashCode​(java.lang.CharSequence seq)
        Calculate a hash code of a byte array assuming ASCII character encoding. The resulting hash code will be case insensitive.
        Parameters:
        seq - The ascii string to produce hashcode for.
        Returns:
        a hashcode for the given input.
      • split

        public static java.util.List<java.lang.CharSequence> split​(java.lang.CharSequence input,
                                                                   char delimiter,
                                                                   boolean trim)
        Split a given AsciiString to separate ones on the given delimiter. Trimming white-space before and after each token can be controlled by the trim flag This method has no support for regex.
        Parameters:
        input - The initial CharSequence to split, this experiences no side effects.
        delimiter - The delimiter character.
        trim - Flag to control whether the individual items must be trimmed.
        Returns:
        a List of CharSequence subsequences of the input with the separated values
      • equalsIgnoreCaseLower

        public static boolean equalsIgnoreCaseLower​(char a,
                                                    char lowerCaseChar)
        Compare an unknown ascii character a with a known lowercase character lowerCaseChar in a case insensitive manner.
        Parameters:
        a - an unknown ascii character.
        lowerCaseChar - a known to be lowercase ascii character.
        Returns:
        true if a and lowerCaseChar are case insensitive equal.
      • regionMatches

        public static boolean regionMatches​(java.lang.CharSequence cs,
                                            boolean ignoreCase,
                                            int csStart,
                                            java.lang.CharSequence string,
                                            int start,
                                            int length)
        This methods make regionMatches operation correctly for any chars in strings.
        Parameters:
        cs - the CharSequence to be processed
        ignoreCase - specifies if case should be ignored.
        csStart - the starting offset in the cs CharSequence
        string - the CharSequence to compare.
        start - the starting offset in the specified string.
        length - the number of characters to compare.
        Returns:
        true if the ranges of characters are equal, false otherwise.