Class CompareUtil

java.lang.Object
com.sure.tool.util.CompareUtil

public class CompareUtil extends Object
比较工具类:基础类型比较、空安全比较、取最值与范围约束,零依赖。
Since:
0.2.0
Author:
suretool
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T extends Comparable<? super T>>
    T
    clamp(T value, T min, T max)
    值约束在 [min, max] 区间(空安全)。
    static int
    compare(double a, double b)
    双精度比较(含 NaN 处理:NaN 视为最大)。
    static int
    compare(int a, int b)
    整数比较。
    static int
    compare(long a, long b)
    长整数比较。
    static <T> int
    compare(T a, T b, Comparator<? super T> comparator)
    空安全比较:null 视为最小;两值相等或同为 null 返回 0。
    static int
    忽略大小写比较字符串。
    static <T> T
    max(Comparator<? super T> comparator, T... values)
    取最大值(空安全,比较器可空)。
    static <T> T
    min(Comparator<? super T> comparator, T... values)
    取最小值(空安全,比较器可空)。

    Methods inherited from class Object

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

    • compare

      public static int compare(int a, int b)
      整数比较。
      Parameters:
      a - 第一个
      b - 第二个
      Returns:
      -1/0/1
    • compare

      public static int compare(long a, long b)
      长整数比较。
      Parameters:
      a - 第一个
      b - 第二个
      Returns:
      -1/0/1
    • compare

      public static int compare(double a, double b)
      双精度比较(含 NaN 处理:NaN 视为最大)。
      Parameters:
      a - 第一个
      b - 第二个
      Returns:
      -1/0/1
    • compare

      public static <T> int compare(T a, T b, Comparator<? super T> comparator)
      空安全比较:null 视为最小;两值相等或同为 null 返回 0。
      Type Parameters:
      T - 类型
      Parameters:
      a - 第一个
      b - 第二个
      comparator - 比较器(null 时要求值实现 Comparable)
      Returns:
      -1/0/1
    • compareIgnoreCase

      public static int compareIgnoreCase(String a, String b)
      忽略大小写比较字符串。
      Parameters:
      a - 第一个
      b - 第二个
      Returns:
      -1/0/1
    • max

      @SafeVarargs public static <T> T max(Comparator<? super T> comparator, T... values)
      取最大值(空安全,比较器可空)。
      Type Parameters:
      T - 类型
      Parameters:
      comparator - 比较器
      values - 值数组
      Returns:
      最大值;全部 null 返回 null
    • min

      @SafeVarargs public static <T> T min(Comparator<? super T> comparator, T... values)
      取最小值(空安全,比较器可空)。
      Type Parameters:
      T - 类型
      Parameters:
      comparator - 比较器
      values - 值数组
      Returns:
      最小值;全部 null 返回 null
    • clamp

      public static <T extends Comparable<? super T>> T clamp(T value, T min, T max)
      值约束在 [min, max] 区间(空安全)。
      Type Parameters:
      T - 类型
      Parameters:
      value - 值
      min - 下界
      max - 上界
      Returns:
      约束后的值