Class Ints

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

public final class Ints extends Object
Int Array Utility Class - Guava-style operations for int primitive arrays int 数组工具类 - Guava 风格的 int 原始类型数组操作

Provides comprehensive int array operations inspired by Guava Ints.

提供 int 原始类型数组的操作方法,参考 Guava Ints。

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 = Ints.toByteArray(12345);
int value = Ints.fromByteArray(bytes);

// Array operations - 数组操作
int[] merged = Ints.concat(arr1, arr2);
int idx = Ints.indexOf(arr, 5);

// Safe conversion - 安全转换
int clamped = Ints.saturatedCast(Long.MAX_VALUE); // Integer.MAX_VALUE

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 int[]
    Empty array constant 空数组常量
    static final int
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static List<Integer>
    asList(int... array)
    Converts to List 转为 List
    static int
    checkedCast(long value)
    Checked conversion (long to int, throws on overflow) 检查转换(long 转 int,溢出则抛异常)
    static int
    compare(int a, int b)
    Compares two int values 比较两个 int 值
    static int[]
    concat(int[]... arrays)
    Concatenates multiple arrays 合并多个数组
    static int
    constrainToRange(int value, int min, int max)
    Constrains the value within the specified range 约束值在指定范围内
    static boolean
    contains(int[] array, int target)
    Checks if the array contains the specified element 检查数组是否包含指定元素
    static int[]
    ensureCapacity(int[] array, int minLength, int padding)
    Ensures that array has minimum capacity, expanding if needed.
    static int[]
    ensureNonNull(int[] array)
    Ensures the array is not null 确保数组不为 null
    static int
    fromByteArray(byte[] bytes)
    Converts byte array to int (big-endian) byte 数组转 int(大端序)
    static int
    fromBytes(byte b1, byte b2, byte b3, byte b4)
    Converts 4 bytes to int 4 个字节转 int
    static int
    indexOf(int[] array, int target)
    Finds the element index 查找元素索引
    static int
    indexOf(int[] array, int[] target)
    Finds the position of a sub-array in the array 查找子数组在数组中的位置
    static int
    indexOf(int[] array, int target, int start, int end)
    Finds the element index within the specified range 在指定范围内查找元素索引
    static boolean
    isSorted(int[] array)
    Checks if the array is sorted 检查数组是否已排序
    static String
    join(String separator, int... array)
    Converts the array to string 数组转字符串
    static int
    lastIndexOf(int[] array, int target)
    Finds the element index from the end 从后向前查找元素索引
    static int
    lastIndexOf(int[] array, int target, int start, int end)
    Finds the element index from the end within the specified range 在指定范围内从后向前查找元素索引
    static Comparator<int[]>
    Gets the comparator 获取比较器
    static int
    max(int... array)
    Returns the maximum value 返回最大值
    static int
    min(int... array)
    Returns the minimum value 返回最小值
    static void
    reverse(int[] array)
    Reverses the array 反转数组
    static void
    reverse(int[] array, int fromIndex, int toIndex)
    Reverses the specified range of the array 反转数组指定范围
    static void
    rotate(int[] array, int distance)
    Rotates the array 旋转数组
    static int
    saturatedCast(long value)
    Saturated conversion (long to int) 饱和转换(long 转 int)
    static void
    sortDescending(int[] array)
    Sorts in descending order 降序排序
    static void
    sortDescending(int[] array, int fromIndex, int toIndex)
    Sorts the specified range in descending order 降序排序指定范围
    static int[]
    toArray(Collection<? extends Number> collection)
    Converts from Collection to array 从 Collection 转为数组
    static byte[]
    toByteArray(int value)
    Converts int to byte array (big-endian) int 转 byte 数组(大端序)
    static Integer
    tryParse(String string)
    Attempts to parse as int 尝试解析为 int
    static Integer
    tryParse(String string, int radix)
    Attempts to parse as int (specified radix) 尝试解析为 int(指定进制)

    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 int MAX_POWER_OF_TWO
      See Also:
    • EMPTY_ARRAY

      public static final int[] EMPTY_ARRAY
      Empty array constant 空数组常量
  • Method Details

    • toByteArray

      public static byte[] toByteArray(int value)
      Converts int to byte array (big-endian) int 转 byte 数组(大端序)
    • fromByteArray

      public static int fromByteArray(byte[] bytes)
      Converts byte array to int (big-endian) byte 数组转 int(大端序)
    • fromBytes

      public static int fromBytes(byte b1, byte b2, byte b3, byte b4)
      Converts 4 bytes to int 4 个字节转 int
    • concat

      public static int[] concat(int[]... arrays)
      Concatenates multiple arrays 合并多个数组
    • contains

      public static boolean contains(int[] array, int target)
      Checks if the array contains the specified element 检查数组是否包含指定元素
    • indexOf

      public static int indexOf(int[] array, int target)
      Finds the element index 查找元素索引
    • indexOf

      public static int indexOf(int[] array, int target, int start, int end)
      Finds the element index within the specified range 在指定范围内查找元素索引
    • indexOf

      public static int indexOf(int[] array, int[] target)
      Finds the position of a sub-array in the array 查找子数组在数组中的位置
      Parameters:
      array - the source array | 源数组
      target - the target sub-array | 目标子数组
      Returns:
      the result | 子数组首次出现的索引,未找到返回 -1
    • lastIndexOf

      public static int lastIndexOf(int[] array, int target)
      Finds the element index from the end 从后向前查找元素索引
    • lastIndexOf

      public static int lastIndexOf(int[] array, int target, int start, int end)
      Finds the element index from the end within the specified range 在指定范围内从后向前查找元素索引
    • min

      public static int min(int... array)
      Returns the minimum value 返回最小值
    • max

      public static int max(int... array)
      Returns the maximum value 返回最大值
    • constrainToRange

      public static int constrainToRange(int value, int min, int max)
      Constrains the value within the specified range 约束值在指定范围内
    • saturatedCast

      public static int saturatedCast(long value)
      Saturated conversion (long to int) 饱和转换(long 转 int)
    • checkedCast

      public static int checkedCast(long value)
      Checked conversion (long to int, throws on overflow) 检查转换(long 转 int,溢出则抛异常)
    • compare

      public static int compare(int a, int b)
      Compares two int values 比较两个 int 值
    • lexicographicalComparator

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

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

      public static int[] toArray(Collection<? extends Number> collection)
      Converts from Collection to array 从 Collection 转为数组
    • reverse

      public static void reverse(int[] array)
      Reverses the array 反转数组
    • reverse

      public static void reverse(int[] array, int fromIndex, int toIndex)
      Reverses the specified range of the array 反转数组指定范围
    • rotate

      public static void rotate(int[] array, int distance)
      Rotates the array 旋转数组

      正数向右旋转,负数向左旋转

      Parameters:
      array - the array | 数组
      distance - rotation distance | 旋转距离
    • sortDescending

      public static void sortDescending(int[] array)
      Sorts in descending order 降序排序
    • sortDescending

      public static void sortDescending(int[] array, int fromIndex, int toIndex)
      Sorts the specified range in descending order 降序排序指定范围
    • isSorted

      public static boolean isSorted(int[] array)
      Checks if the array is sorted 检查数组是否已排序
    • join

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

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

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

      public static int[] ensureNonNull(int[] array)
      Ensures the array is not null 确保数组不为 null
    • ensureCapacity

      public static int[] ensureCapacity(int[] array, int minLength, int padding)
      Ensures that array has minimum capacity, expanding if needed. 确保数组具有最小容量,如果需要则扩展。
      Parameters:
      array - the original array | 原数组
      minLength - the minimum required length | 最小需要长度
      padding - additional padding beyond minLength | 超出 minLength 的额外填充
      Returns:
      the array with ensured capacity | 确保容量后的数组