Class Chars


  • @GwtCompatible(emulated=true)
    public final class Chars
    extends java.lang.Object
    Static utility methods pertaining to char primitives, that are not already found in either Character or Arrays.

    All the operations in this class treat char values strictly numerically; they are neither Unicode-aware nor locale-dependent.

    See the Guava User Guide article on primitive utilities.

    Since:
    1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BYTES
      The number of bytes required to represent a primitive char value.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.List<java.lang.Character> asList​(char... backingArray)
      Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
      static char checkedCast​(long value)
      Returns the char value that is equal to value, if possible.
      static int compare​(char a, char b)
      Compares the two specified char values.
      static char[] concat​(char[]... arrays)
      Returns the values from each provided array combined into a single array.
      static boolean contains​(char[] array, char target)
      Returns true if target is present as an element anywhere in array.
      static char[] ensureCapacity​(char[] array, int minLength, int padding)
      Returns an array containing the same values as array, but guaranteed to be of a specified minimum length.
      static int hashCode​(char value)
      Returns a hash code for value; equal to the result of invoking ((Character) value).hashCode().
      static int indexOf​(char[] array, char target)
      Returns the index of the first appearance of the value target in array.
      static int indexOf​(char[] array, char[] target)
      Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.
      static java.lang.String join​(java.lang.String separator, char... array)
      Returns a string containing the supplied char values separated by separator.
      static int lastIndexOf​(char[] array, char target)
      Returns the index of the last appearance of the value target in array.
      static java.util.Comparator<char[]> lexicographicalComparator()
      Returns a comparator that compares two char arrays lexicographically.
      static char max​(char... array)
      Returns the greatest value present in array.
      static char min​(char... array)
      Returns the least value present in array.
      static char saturatedCast​(long value)
      Returns the char nearest in value to value.
      static char[] toArray​(java.util.Collection<java.lang.Character> collection)
      Copies a collection of Character instances into a new array of primitive char values.
      • Methods inherited from class java.lang.Object

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

      • BYTES

        public static final int BYTES
        The number of bytes required to represent a primitive char value.
        See Also:
        Constant Field Values
    • Method Detail

      • hashCode

        public static int hashCode​(char value)
        Returns a hash code for value; equal to the result of invoking ((Character) value).hashCode().
        Parameters:
        value - a primitive char value
        Returns:
        a hash code for the value
      • checkedCast

        public static char checkedCast​(long value)
        Returns the char value that is equal to value, if possible.
        Parameters:
        value - any value in the range of the char type
        Returns:
        the char value that equals value
        Throws:
        java.lang.IllegalArgumentException - if value is greater than Character.MAX_VALUE or less than Character.MIN_VALUE
      • saturatedCast

        public static char saturatedCast​(long value)
        Returns the char nearest in value to value.
        Parameters:
        value - any long value
        Returns:
        the same value cast to char if it is in the range of the char type, Character.MAX_VALUE if it is too large, or Character.MIN_VALUE if it is too small
      • compare

        public static int compare​(char a,
                                  char b)
        Compares the two specified char values. The sign of the value returned is the same as that of ((Character) a).compareTo(b).

        Note: projects using JDK 7 or later should use the equivalent Character.compare(char, char) method instead.

        Parameters:
        a - the first char to compare
        b - the second char to compare
        Returns:
        a negative value if a is less than b; a positive value if a is greater than b; or zero if they are equal
      • contains

        public static boolean contains​(char[] array,
                                       char target)
        Returns true if target is present as an element anywhere in array.
        Parameters:
        array - an array of char values, possibly empty
        target - a primitive char value
        Returns:
        true if array[i] == target for some value of i
      • indexOf

        public static int indexOf​(char[] array,
                                  char target)
        Returns the index of the first appearance of the value target in array.
        Parameters:
        array - an array of char values, possibly empty
        target - a primitive char value
        Returns:
        the least index i for which array[i] == target, or -1 if no such index exists.
      • indexOf

        public static int indexOf​(char[] array,
                                  char[] target)
        Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.

        More formally, returns the lowest index i such that java.util.Arrays.copyOfRange(array, i, i + target.length) contains exactly the same elements as target.

        Parameters:
        array - the array to search for the sequence target
        target - the array to search for as a sub-sequence of array
      • lastIndexOf

        public static int lastIndexOf​(char[] array,
                                      char target)
        Returns the index of the last appearance of the value target in array.
        Parameters:
        array - an array of char values, possibly empty
        target - a primitive char value
        Returns:
        the greatest index i for which array[i] == target, or -1 if no such index exists.
      • min

        public static char min​(char... array)
        Returns the least value present in array.
        Parameters:
        array - a nonempty array of char values
        Returns:
        the value present in array that is less than or equal to every other value in the array
        Throws:
        java.lang.IllegalArgumentException - if array is empty
      • max

        public static char max​(char... array)
        Returns the greatest value present in array.
        Parameters:
        array - a nonempty array of char values
        Returns:
        the value present in array that is greater than or equal to every other value in the array
        Throws:
        java.lang.IllegalArgumentException - if array is empty
      • concat

        public static char[] concat​(char[]... arrays)
        Returns the values from each provided array combined into a single array. For example, concat(new char[] {a, b}, new char[] {}, new char[] {c} returns the array {a, b, c}.
        Parameters:
        arrays - zero or more char arrays
        Returns:
        a single array containing all the values from the source arrays, in order
      • ensureCapacity

        public static char[] ensureCapacity​(char[] array,
                                            int minLength,
                                            int padding)
        Returns an array containing the same values as array, but guaranteed to be of a specified minimum length. If array already has a length of at least minLength, it is returned directly. Otherwise, a new array of size minLength + padding is returned, containing the values of array, and zeroes in the remaining places.
        Parameters:
        array - the source array
        minLength - the minimum length the returned array must guarantee
        padding - an extra amount to "grow" the array by if growth is necessary
        Returns:
        an array containing the values of array, with guaranteed minimum length minLength
        Throws:
        java.lang.IllegalArgumentException - if minLength or padding is negative
      • join

        public static java.lang.String join​(java.lang.String separator,
                                            char... array)
        Returns a string containing the supplied char values separated by separator. For example, join("-", '1', '2', '3') returns the string "1-2-3".
        Parameters:
        separator - the text that should appear between consecutive values in the resulting string (but not at the start or end)
        array - an array of char values, possibly empty
      • lexicographicalComparator

        public static java.util.Comparator<char[]> lexicographicalComparator()
        Returns a comparator that compares two char arrays lexicographically. That is, it compares, using compare(char, char)), the first pair of values that follow any common prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For example, [] < ['a'] < ['a', 'b'] < ['b'].

        The returned comparator is inconsistent with Object.equals(Object) (since arrays support only identity equality), but it is consistent with Arrays.equals(char[], char[]).

        Since:
        2.0
        See Also:
        Lexicographical order article at Wikipedia
      • toArray

        public static char[] toArray​(java.util.Collection<java.lang.Character> collection)
        Copies a collection of Character instances into a new array of primitive char values.

        Elements are copied from the argument collection as if by collection.toArray(). Calling this method is as thread-safe as calling that method.

        Parameters:
        collection - a collection of Character objects
        Returns:
        an array containing the same values as collection, in the same order, converted to primitives
        Throws:
        java.lang.NullPointerException - if collection or any of its elements is null
      • asList

        public static java.util.List<java.lang.Character> asList​(char... backingArray)
        Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). The list supports List.set(int, Object), but any attempt to set a value to null will result in a NullPointerException.

        The returned list maintains the values, but not the identities, of Character objects written to or read from it. For example, whether list.get(0) == list.get(0) is true for the returned list is unspecified.

        Parameters:
        backingArray - the array to back the list
        Returns:
        a list view of the array