Class Longs

java.lang.Object
com.globalmentor.java.Longs

public class Longs extends Object
Utilities for manipulating long objects.
Author:
Garret Wilson
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long[]
    The shared empty array of longs.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    compare(long l1, long l2)
    Compares two longs for order.
    static int
    hashCode(long value)
    Returns a hash code for a long value.
    static byte[]
    toBytes(long value)
    Converts a long value into its equivalent big-endian sequence of bytes, equivalent to ByteBuffer.allocate(8).putLong(value).array().
    static String
    toHexString(long value, int length)
    Converts a long into a hex string with the specified number of digits.
    static int
    toInt(long value)
    Converts a long value to an integer, checking for overflow.
    static long
    toLong(byte[] bytes)
    Converts a big-endian sequence of bytes into the represented long value, equivalent to ByteBuffer.wrap(bytes).getLong().

    Methods inherited from class java.lang.Object

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

    • NO_LONGS

      public static final long[] NO_LONGS
      The shared empty array of longs.
  • Method Details

    • hashCode

      public static int hashCode(long value)
      Returns a hash code for a long value. This implementation returns the same value used by Long.hashCode().
      Parameters:
      value - The value for which a hash code should be returned.
      Returns:
      The hash code of the long value.
    • toHexString

      public static String toHexString(long value, int length)
      Converts a long into a hex string with the specified number of digits.
      Parameters:
      value - The value to convert.
      length - The number of digits the returned string should have.
      Returns:
      Lowercase hex version of the given value with the correct number of digits, using zeros to pad the left of the string to the correct length.
    • toBytes

      public static byte[] toBytes(long value)
      Converts a long value into its equivalent big-endian sequence of bytes, equivalent to ByteBuffer.allocate(8).putLong(value).array().
      Implementation Note:
      Modified from code in an answer on Stack Overflow.
      Parameters:
      value - The value to convert to a sequence of bytes.
      Returns:
      The sequence of bytes representing the long value in network byte order.
    • toLong

      public static long toLong(byte[] bytes)
      Converts a big-endian sequence of bytes into the represented long value, equivalent to ByteBuffer.wrap(bytes).getLong().
      Implementation Note:
      Modified from code in an answer on Stack Overflow.
      Parameters:
      bytes - The bytes to convert to a long.
      Returns:
      The long value represented by the network byte order sequence of bytes.
    • toInt

      public static int toInt(long value)
      Converts a long value to an integer, checking for overflow.
      Parameters:
      value - The value to convert.
      Returns:
      The long value as an integer.
      Throws:
      IllegalArgumentException - if the given value is smaller than Integer.MIN_VALUE or greater than Integer.MAX_VALUE.
    • compare

      public static int compare(long l1, long l2)
      Compares two longs for order.
      Parameters:
      l1 - The first long to compare.
      l2 - The second long to compare.
      Returns:
      a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.