Class Longs

java.lang.Object
cloud.opencode.base.core.primitives.Longs

public final class Longs extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
    static final long[]
     
    static final long
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static List<Long>
    asList(long... array)
    Converts to List 转为 List
    static int
    compare(long a, long b)
    Compares two long values 比较两个 long 值
    static long[]
    concat(long[]... arrays)
    Concatenates multiple arrays 合并多个数组
    static long
    constrainToRange(long value, long min, long max)
    Constrains the value within the specified range 约束值在指定范围内
    static boolean
    contains(long[] array, long target)
    Checks if the array contains the specified element 检查数组是否包含指定元素
    static long[]
    ensureNonNull(long[] array)
     
    static long
    fromByteArray(byte[] bytes)
    Converts byte array to long (big-endian) byte 数组转 long(大端序)
    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
    static int
    indexOf(long[] array, long target)
    Finds the element index 查找元素索引
    static int
    indexOf(long[] array, long target, int start, int end)
    Finds the element index within the specified range 在指定范围内查找元素索引
    static boolean
    isSorted(long[] array)
    Checks if the array is sorted 检查数组是否已排序
    static String
    join(String separator, long... array)
    Converts the array to string 数组转字符串
    static int
    lastIndexOf(long[] array, long target)
    Finds the element index from the end 从后向前查找元素索引
    static int
    lastIndexOf(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 long
    max(long... array)
    Returns the maximum value 返回最大值
    static long
    min(long... array)
    Returns the minimum value 返回最小值
    static void
    reverse(long[] array)
    Reverses the array 反转数组
    static void
    reverse(long[] array, int fromIndex, int toIndex)
    Reverses the specified range of the array 反转数组指定范围
    static void
    rotate(long[] array, int distance)
    Rotates the array 旋转数组
    static void
    sortDescending(long[] array)
    Sorts in descending order 降序排序
    static void
    sortDescending(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 Long
    tryParse(String string)
    Attempts to parse as long 尝试解析为 long
    static Long
    tryParse(String string, int radix)
    Attempts to parse as long (specified radix) 尝试解析为 long(指定进制)

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public static Comparator<long[]> lexicographicalComparator()
      Gets the comparator 获取比较器
    • asList

      public static List<Long> asList(long... array)
      Converts to List 转为 List
    • toArray

      public static long[] toArray(Collection<? extends Number> collection)
      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

      public static String join(String separator, long... array)
      Converts the array to string 数组转字符串
    • tryParse

      public static Long tryParse(String string)
      Attempts to parse as long 尝试解析为 long
    • tryParse

      public static Long tryParse(String string, int radix)
      Attempts to parse as long (specified radix) 尝试解析为 long(指定进制)
    • ensureNonNull

      public static long[] ensureNonNull(long[] array)