Package com.globalmentor.java
Class Longs
java.lang.Object
com.globalmentor.java.Longs
Utilities for manipulating long objects.
- Author:
- Garret Wilson
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic 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 toByteBuffer.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 toByteBuffer.wrap(bytes).getLong()
.
-
Field Details
-
NO_LONGS
public static final long[] NO_LONGSThe 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 byLong.hashCode()
.- Parameters:
value
- The value for which a hash code should be returned.- Returns:
- The hash code of the long value.
-
toHexString
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 toByteBuffer.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 toByteBuffer.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 thanInteger.MIN_VALUE
or greater thanInteger.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.
-