Class Shorts

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BYTES
      The number of bytes required to represent a primitive short value.
      static short MAX_POWER_OF_TWO
      The largest power of two that can be represented as a short.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static List<Short> asList​(short... backingArray)
      Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
      static short checkedCast​(long value)
      Returns the short value that is equal to value, if possible.
      static int compare​(short a, short b)
      Compares the two specified short values.
      static short[] concat​(short[]... arrays)
      Returns the values from each provided array combined into a single array.
      static short constrainToRange​(short value, short min, short max)
      Returns the value nearest to value which is within the closed range [min..max].
      static boolean contains​(short[] array, short target)
      Returns true if target is present as an element anywhere in array.
      static short[] ensureCapacity​(short[] array, int minLength, int padding)
      Returns an array containing the same values as array, but guaranteed to be of a specified minimum length.
      static short fromByteArray​(byte[] bytes)
      Returns the short value whose big-endian representation is stored in the first 2 bytes of bytes; equivalent to ByteBuffer.wrap(bytes).getShort().
      static short fromBytes​(byte b1, byte b2)
      Returns the short value whose byte representation is the given 2 bytes, in big-endian order; equivalent to Shorts.fromByteArray(new byte[] {b1, b2}).
      static int hashCode​(short value)
      Returns a hash code for value; equal to the result of invoking ((Short) value).hashCode().
      static int indexOf​(short[] array, short target)
      Returns the index of the first appearance of the value target in array.
      static int indexOf​(short[] array, short[] target)
      Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.
      static String join​(String separator, short... array)
      Returns a string containing the supplied short values separated by separator.
      static int lastIndexOf​(short[] array, short target)
      Returns the index of the last appearance of the value target in array.
      static Comparator<short[]> lexicographicalComparator()
      Returns a comparator that compares two short arrays lexicographically.
      static short max​(short... array)
      Returns the greatest value present in array.
      static short min​(short... array)
      Returns the least value present in array.
      static void reverse​(short[] array)
      Reverses the elements of array.
      static void reverse​(short[] array, int fromIndex, int toIndex)
      Reverses the elements of array between fromIndex inclusive and toIndex exclusive.
      static short saturatedCast​(long value)
      Returns the short nearest in value to value.
      static void sortDescending​(short[] array)
      Sorts the elements of array in descending order.
      static void sortDescending​(short[] array, int fromIndex, int toIndex)
      Sorts the elements of array between fromIndex inclusive and toIndex exclusive in descending order.
      static Converter<String,​Short> stringConverter()
      Returns a serializable converter object that converts between strings and shorts using Short.decode(java.lang.String) and Short.toString().
      static short[] toArray​(Collection<? extends Number> collection)
      Returns an array containing each value of collection, converted to a short value in the manner of Number.shortValue().
      static byte[] toByteArray​(short value)
      Returns a big-endian representation of value in a 2-element byte array; equivalent to ByteBuffer.allocate(2).putShort(value).array().
    • Method Detail

      • hashCode

        public static int hashCode​(short value)
        Returns a hash code for value; equal to the result of invoking ((Short) value).hashCode().

        Java 8 users: use Short.hashCode(short) instead.

        Parameters:
        value - a primitive short value
        Returns:
        a hash code for the value
      • checkedCast

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

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

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

        Note for Java 7 and later: this method should be treated as deprecated; use the equivalent Short.compare(short, short) method instead.

        Parameters:
        a - the first short to compare
        b - the second short 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​(short[] array,
                                       short target)
        Returns true if target is present as an element anywhere in array.
        Parameters:
        array - an array of short values, possibly empty
        target - a primitive short value
        Returns:
        true if array[i] == target for some value of i
      • indexOf

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

        public static int indexOf​(short[] array,
                                  short[] 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 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​(short[] array,
                                      short target)
        Returns the index of the last appearance of the value target in array.
        Parameters:
        array - an array of short values, possibly empty
        target - a primitive short value
        Returns:
        the greatest index i for which array[i] == target, or -1 if no such index exists.
      • min

        @GwtIncompatible("Available in GWT! Annotation is to avoid conflict with GWT specialization of base class.")
        public static short min​(short... array)
        Returns the least value present in array.
        Parameters:
        array - a nonempty array of short values
        Returns:
        the value present in array that is less than or equal to every other value in the array
        Throws:
        IllegalArgumentException - if array is empty
      • max

        @GwtIncompatible("Available in GWT! Annotation is to avoid conflict with GWT specialization of base class.")
        public static short max​(short... array)
        Returns the greatest value present in array.
        Parameters:
        array - a nonempty array of short values
        Returns:
        the value present in array that is greater than or equal to every other value in the array
        Throws:
        IllegalArgumentException - if array is empty
      • constrainToRange

        @Beta
        public static short constrainToRange​(short value,
                                             short min,
                                             short max)
        Returns the value nearest to value which is within the closed range [min..max].

        If value is within the range [min..max], value is returned unchanged. If value is less than min, min is returned, and if value is greater than max, max is returned.

        Parameters:
        value - the short value to constrain
        min - the lower bound (inclusive) of the range to constrain value to
        max - the upper bound (inclusive) of the range to constrain value to
        Throws:
        IllegalArgumentException - if min > max
        Since:
        21.0
      • concat

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

        @GwtIncompatible
        public static byte[] toByteArray​(short value)
        Returns a big-endian representation of value in a 2-element byte array; equivalent to ByteBuffer.allocate(2).putShort(value).array(). For example, the input value (short) 0x1234 would yield the byte array {0x12, 0x34}.

        If you need to convert and concatenate several values (possibly even of different types), use a shared ByteBuffer instance, or use ByteStreams.newDataOutput() to get a growable buffer.

      • fromByteArray

        @GwtIncompatible
        public static short fromByteArray​(byte[] bytes)
        Returns the short value whose big-endian representation is stored in the first 2 bytes of bytes; equivalent to ByteBuffer.wrap(bytes).getShort(). For example, the input byte array {0x54, 0x32} would yield the short value 0x5432.

        Arguably, it's preferable to use ByteBuffer; that library exposes much more flexibility at little cost in readability.

        Throws:
        IllegalArgumentException - if bytes has fewer than 2 elements
      • fromBytes

        @GwtIncompatible
        public static short fromBytes​(byte b1,
                                      byte b2)
        Returns the short value whose byte representation is the given 2 bytes, in big-endian order; equivalent to Shorts.fromByteArray(new byte[] {b1, b2}).
        Since:
        7.0
      • ensureCapacity

        public static short[] ensureCapacity​(short[] 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:
        IllegalArgumentException - if minLength or padding is negative
      • join

        public static String join​(String separator,
                                  short... array)
        Returns a string containing the supplied short values separated by separator. For example, join("-", (short) 1, (short) 2, (short) 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 short values, possibly empty
      • sortDescending

        public static void sortDescending​(short[] array)
        Sorts the elements of array in descending order.
        Since:
        23.1
      • sortDescending

        public static void sortDescending​(short[] array,
                                          int fromIndex,
                                          int toIndex)
        Sorts the elements of array between fromIndex inclusive and toIndex exclusive in descending order.
        Since:
        23.1
      • reverse

        public static void reverse​(short[] array)
        Reverses the elements of array. This is equivalent to Collections.reverse(Shorts.asList(array)), but is likely to be more efficient.
        Since:
        23.1
      • reverse

        public static void reverse​(short[] array,
                                   int fromIndex,
                                   int toIndex)
        Reverses the elements of array between fromIndex inclusive and toIndex exclusive. This is equivalent to Collections.reverse(Shorts.asList(array).subList(fromIndex, toIndex)), but is likely to be more efficient.
        Throws:
        IndexOutOfBoundsException - if fromIndex < 0, toIndex > array.length, or toIndex > fromIndex
        Since:
        23.1
      • toArray

        public static short[] toArray​(Collection<? extends Number> collection)
        Returns an array containing each value of collection, converted to a short value in the manner of Number.shortValue().

        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 Number instances
        Returns:
        an array containing the same values as collection, in the same order, converted to primitives
        Throws:
        NullPointerException - if collection or any of its elements is null
        Since:
        1.0 (parameter was Collection<Short> before 12.0)
      • asList

        public static List<ShortasList​(short... 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 Short 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