Class TypeCoercion

java.lang.Object
cloud.opencode.base.expression.eval.TypeCoercion

public final class TypeCoercion extends Object
Type Coercion Utility 类型转换工具

Provides type conversion and coercion for expression evaluation.

为表达式求值提供类型转换和强制转换。

Features | 主要功能:

  • Convert to primitives: boolean, int, long, double - 转换为基本类型
  • Convert to BigDecimal, BigInteger - 转换为BigDecimal、BigInteger
  • Convert to date types: LocalDate, LocalDateTime - 转换为日期类型
  • Truthiness evaluation for any type - 任何类型的真值求值
  • Convertibility check - 可转换性检查

Usage Examples | 使用示例:

boolean b = TypeCoercion.toBoolean("true");  // true
int i = TypeCoercion.toInt("42");  // 42
Integer typed = TypeCoercion.convert("42", Integer.class);  // 42
boolean can = TypeCoercion.canConvert("42", Integer.class);  // true

Security | 安全性:

  • Thread-safe: Yes, stateless utility class - 线程安全: 是,无状态工具类
  • Null-safe: Yes, null returns default values (0, false, "null") - 空值安全: 是,null返回默认值
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • toBoolean

      public static boolean toBoolean(Object value)
      Convert value to boolean 将值转换为布尔值
      Parameters:
      value - the value | 值
      Returns:
      the boolean value | 布尔值
    • toInt

      public static int toInt(Object value)
      Convert value to integer 将值转换为整数
      Parameters:
      value - the value | 值
      Returns:
      the integer value | 整数值
    • toLong

      public static long toLong(Object value)
      Convert value to long 将值转换为长整数
      Parameters:
      value - the value | 值
      Returns:
      the long value | 长整数值
    • toDouble

      public static double toDouble(Object value)
      Convert value to double 将值转换为双精度浮点数
      Parameters:
      value - the value | 值
      Returns:
      the double value | 双精度值
    • toString

      public static String toString(Object value)
      Convert value to string 将值转换为字符串
      Parameters:
      value - the value | 值
      Returns:
      the string value | 字符串值
    • convert

      public static <T> T convert(Object value, Class<T> targetType)
      Convert value to target type 将值转换为目标类型
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      value - the value | 值
      targetType - the target type | 目标类型
      Returns:
      the converted value | 转换后的值
    • canConvert

      public static boolean canConvert(Object value, Class<?> targetType)
      Check if value can be converted to target type 检查值是否可以转换为目标类型
      Parameters:
      value - the value | 值
      targetType - the target type | 目标类型
      Returns:
      true if convertible | 如果可转换返回true