Class PathResolver

java.lang.Object
cloud.opencode.base.yml.path.PathResolver

public final class PathResolver extends Object
Path Resolver - Resolves paths in YAML data structures 路径解析器 - 在 YAML 数据结构中解析路径

This class provides utilities for accessing nested values using path expressions.

此类提供使用路径表达式访问嵌套值的工具。

Features | 主要功能:

  • Dot-notation and array-index path resolution - 点号和数组索引路径解析
  • Typed value access (String, int, long, boolean, List, Map) - 类型化值访问
  • Default value and Optional support - 默认值和 Optional 支持
  • Path existence check - 路径存在性检查

Usage Examples | 使用示例:

Map<String, Object> data = OpenYml.load(yaml);

// Get value at path
String port = PathResolver.get(data, "server.port");

// Get with default
int timeout = PathResolver.get(data, "server.timeout", 30);

// Check if path exists
boolean exists = PathResolver.has(data, "database.url");

// Get optional value
Optional<String> value = PathResolver.getOptional(data, "optional.path");

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: Yes (returns null/default for null root or missing paths) - 空值安全: 是(空根或缺失路径返回 null/默认值)
Since:
JDK 25, opencode-base-yml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • get

      public static <T> T get(Object root, String path)
      Gets a value at the specified path. 获取指定路径的值。
      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      root - the root object (Map or List) | 根对象(Map 或 List)
      path - the path string | 路径字符串
      Returns:
      the value at path | 路径处的值
      Throws:
      YmlPathException - if path is not found | 如果未找到路径
    • get

      public static <T> T get(Object root, String path, T defaultValue)
      Gets a value at the specified path with default. 获取指定路径的值(带默认值)。
      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      root - the root object | 根对象
      path - the path string | 路径字符串
      defaultValue - the default value | 默认值
      Returns:
      the value at path or default | 路径处的值或默认值
    • get

      public static <T> T get(Object root, YmlPath path)
      Gets a value at the specified YmlPath. 获取指定 YmlPath 的值。
      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      root - the root object | 根对象
      path - the path | 路径
      Returns:
      the value at path | 路径处的值
      Throws:
      YmlPathException - if path is not found | 如果未找到路径
    • getOptional

      public static <T> Optional<T> getOptional(Object root, String path)
      Gets an optional value at the specified path. 获取指定路径的可选值。
      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      root - the root object | 根对象
      path - the path string | 路径字符串
      Returns:
      optional containing value if found | 如果找到则包含值的 Optional
    • has

      public static boolean has(Object root, String path)
      Checks if a path exists. 检查路径是否存在。
      Parameters:
      root - the root object | 根对象
      path - the path string | 路径字符串
      Returns:
      true if path exists | 如果路径存在则返回 true
    • getString

      public static String getString(Object root, String path)
      Gets a string value at the specified path. 获取指定路径的字符串值。
      Parameters:
      root - the root object | 根对象
      path - the path string | 路径字符串
      Returns:
      the string value | 字符串值
    • getString

      public static String getString(Object root, String path, String defaultValue)
      Gets a string value at the specified path with default. 获取指定路径的字符串值(带默认值)。
      Parameters:
      root - the root object | 根对象
      path - the path string | 路径字符串
      defaultValue - the default value | 默认值
      Returns:
      the string value or default | 字符串值或默认值
    • getInt

      public static Integer getInt(Object root, String path)
      Gets an integer value at the specified path. 获取指定路径的整数值。
      Parameters:
      root - the root object | 根对象
      path - the path string | 路径字符串
      Returns:
      the integer value | 整数值
    • getInt

      public static int getInt(Object root, String path, int defaultValue)
      Gets an integer value at the specified path with default. 获取指定路径的整数值(带默认值)。
      Parameters:
      root - the root object | 根对象
      path - the path string | 路径字符串
      defaultValue - the default value | 默认值
      Returns:
      the integer value or default | 整数值或默认值
    • getLong

      public static Long getLong(Object root, String path)
      Gets a long value at the specified path. 获取指定路径的长整数值。
      Parameters:
      root - the root object | 根对象
      path - the path string | 路径字符串
      Returns:
      the long value | 长整数值
    • getBoolean

      public static Boolean getBoolean(Object root, String path)
      Gets a boolean value at the specified path. 获取指定路径的布尔值。
      Parameters:
      root - the root object | 根对象
      path - the path string | 路径字符串
      Returns:
      the boolean value | 布尔值
    • getBoolean

      public static boolean getBoolean(Object root, String path, boolean defaultValue)
      Gets a boolean value at the specified path with default. 获取指定路径的布尔值(带默认值)。
      Parameters:
      root - the root object | 根对象
      path - the path string | 路径字符串
      defaultValue - the default value | 默认值
      Returns:
      the boolean value or default | 布尔值或默认值
    • getList

      public static <T> List<T> getList(Object root, String path)
      Gets a list value at the specified path. 获取指定路径的列表值。
      Type Parameters:
      T - the element type | 元素类型
      Parameters:
      root - the root object | 根对象
      path - the path string | 路径字符串
      Returns:
      the list | 列表
    • getMap

      public static Map<String,Object> getMap(Object root, String path)
      Gets a map value at the specified path. 获取指定路径的映射值。
      Parameters:
      root - the root object | 根对象
      path - the path string | 路径字符串
      Returns:
      the map | 映射
    • resolve

      public static Object resolve(Object root, YmlPath path)
      Resolves a path in the given root object. 在给定的根对象中解析路径。
      Parameters:
      root - the root object | 根对象
      path - the path | 路径
      Returns:
      the value at path, or null if not found | 路径处的值,如果未找到则返回 null