Class Longs
java.lang.Object
cloud.opencode.base.core.primitives.Longs
Long Array Utility Class - Guava-style operations for long primitive arrays
long 数组工具类 - Guava 风格的 long 原始类型数组操作
Provides comprehensive long array operations inspired by Guava Longs.
提供 long 原始类型数组的操作方法,参考 Guava Longs。
Features | 主要功能:
- Byte conversion (toByteArray, fromByteArray, fromBytes) - 字节转换
- Array operations (concat, contains, indexOf, lastIndexOf) - 数组操作
- Min/Max (min, max, constrainToRange) - 最值和范围
- Safe casting (saturatedCast, checkedCast) - 安全转换
- Collection conversion (asList, toArray) - 集合转换
- Array manipulation (reverse, rotate, sortDescending) - 数组操作
- Comparison (compare, lexicographicalComparator) - 比较
- String operations (join, tryParse) - 字符串操作
Usage Examples | 使用示例:
// Byte conversion - 字节转换
byte[] bytes = Longs.toByteArray(123456789L);
long value = Longs.fromByteArray(bytes);
// Array operations - 数组操作
long[] merged = Longs.concat(arr1, arr2);
int idx = Longs.indexOf(arr, 5L);
Security | 安全性:
- Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
- Null-safe: Partially (throws on null array) - 空值安全: 部分 (null 数组抛异常)
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final long[]static final long -
Method Summary
Modifier and TypeMethodDescriptionasList(long... array) Converts to List 转为 Liststatic intcompare(long a, long b) Compares two long values 比较两个 long 值static long[]concat(long[]... arrays) Concatenates multiple arrays 合并多个数组static longconstrainToRange(long value, long min, long max) Constrains the value within the specified range 约束值在指定范围内static booleancontains(long[] array, long target) Checks if the array contains the specified element 检查数组是否包含指定元素static long[]ensureNonNull(long[] array) static longfromByteArray(byte[] bytes) Converts byte array to long (big-endian) byte 数组转 long(大端序)static longfromBytes(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8) Converts 8 bytes to long 8 个字节转 longstatic intindexOf(long[] array, long target) Finds the element index 查找元素索引static intindexOf(long[] array, long target, int start, int end) Finds the element index within the specified range 在指定范围内查找元素索引static booleanisSorted(long[] array) Checks if the array is sorted 检查数组是否已排序static StringConverts the array to string 数组转字符串static intlastIndexOf(long[] array, long target) Finds the element index from the end 从后向前查找元素索引static intlastIndexOf(long[] array, long target, int start, int end) Finds the element index from the end within the specified range 在指定范围内从后向前查找元素索引static Comparator<long[]> Gets the comparator 获取比较器static longmax(long... array) Returns the maximum value 返回最大值static longmin(long... array) Returns the minimum value 返回最小值static voidreverse(long[] array) Reverses the array 反转数组static voidreverse(long[] array, int fromIndex, int toIndex) Reverses the specified range of the array 反转数组指定范围static voidrotate(long[] array, int distance) Rotates the array 旋转数组static voidsortDescending(long[] array) Sorts in descending order 降序排序static voidsortDescending(long[] array, int fromIndex, int toIndex) Sorts the specified range in descending order 降序排序指定范围static long[]toArray(Collection<? extends Number> collection) Converts from Collection to array 从 Collection 转为数组static byte[]toByteArray(long value) Converts long to byte array (big-endian) long 转 byte 数组(大端序)static LongAttempts to parse as long 尝试解析为 longstatic LongAttempts to parse as long (specified radix) 尝试解析为 long(指定进制)
-
Field Details
-
BYTES
public static final int BYTES- See Also:
-
MAX_POWER_OF_TWO
public static final long MAX_POWER_OF_TWO- See Also:
-
EMPTY_ARRAY
public static final long[] EMPTY_ARRAY
-
-
Method Details
-
toByteArray
public static byte[] toByteArray(long value) Converts long to byte array (big-endian) long 转 byte 数组(大端序) -
fromByteArray
public static long fromByteArray(byte[] bytes) Converts byte array to long (big-endian) byte 数组转 long(大端序) -
fromBytes
public static long fromBytes(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8) Converts 8 bytes to long 8 个字节转 long -
concat
public static long[] concat(long[]... arrays) Concatenates multiple arrays 合并多个数组 -
contains
public static boolean contains(long[] array, long target) Checks if the array contains the specified element 检查数组是否包含指定元素 -
indexOf
public static int indexOf(long[] array, long target) Finds the element index 查找元素索引 -
indexOf
public static int indexOf(long[] array, long target, int start, int end) Finds the element index within the specified range 在指定范围内查找元素索引 -
lastIndexOf
public static int lastIndexOf(long[] array, long target) Finds the element index from the end 从后向前查找元素索引 -
lastIndexOf
public static int lastIndexOf(long[] array, long target, int start, int end) Finds the element index from the end within the specified range 在指定范围内从后向前查找元素索引 -
min
public static long min(long... array) Returns the minimum value 返回最小值 -
max
public static long max(long... array) Returns the maximum value 返回最大值 -
constrainToRange
public static long constrainToRange(long value, long min, long max) Constrains the value within the specified range 约束值在指定范围内 -
compare
public static int compare(long a, long b) Compares two long values 比较两个 long 值 -
lexicographicalComparator
Gets the comparator 获取比较器 -
asList
-
toArray
Converts from Collection to array 从 Collection 转为数组 -
reverse
public static void reverse(long[] array) Reverses the array 反转数组 -
reverse
public static void reverse(long[] array, int fromIndex, int toIndex) Reverses the specified range of the array 反转数组指定范围 -
rotate
public static void rotate(long[] array, int distance) Rotates the array 旋转数组正数向右旋转,负数向左旋转
- Parameters:
array- the array | 数组distance- rotation distance | 旋转距离
-
sortDescending
public static void sortDescending(long[] array) Sorts in descending order 降序排序 -
sortDescending
public static void sortDescending(long[] array, int fromIndex, int toIndex) Sorts the specified range in descending order 降序排序指定范围 -
isSorted
public static boolean isSorted(long[] array) Checks if the array is sorted 检查数组是否已排序 -
join
-
tryParse
-
tryParse
-
ensureNonNull
public static long[] ensureNonNull(long[] array)
-