Class YmlDocument

java.lang.Object
cloud.opencode.base.yml.YmlDocument

public final class YmlDocument extends Object
YAML Document - Represents a parsed YAML document YAML 文档 - 表示解析后的 YAML 文档

This class provides a wrapper around parsed YAML data with convenient access methods.

此类提供解析后 YAML 数据的包装器,带有便捷的访问方法。

Features | 主要功能:

  • Typed value access (String, int, long, boolean, List, Map) - 类型化值访问
  • Dot-notation path navigation - 点号路径导航
  • Optional value support - 可选值支持
  • Sub-document extraction - 子文档提取

Usage Examples | 使用示例:

YmlDocument doc = YmlDocument.of(data);

// Get values
String name = doc.getString("app.name");
int port = doc.getInt("server.port", 8080);

// Navigate
YmlDocument server = doc.get("server");

Security | 安全性:

  • Thread-safe: No (wraps mutable data structures) - 线程安全: 否(包装可变数据结构)
  • Null-safe: Yes (returns defaults or empty for missing paths) - 空值安全: 是(缺失路径返回默认值或空值)
Since:
JDK 25, opencode-base-yml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • of

      public static YmlDocument of(Object data)
      Creates a document from parsed data. 从解析的数据创建文档。
      Parameters:
      data - the parsed data | 解析的数据
      Returns:
      the document | 文档
    • empty

      public static YmlDocument empty()
      Creates an empty document. 创建空文档。
      Returns:
      an empty document | 空文档
    • getRoot

      public Object getRoot()
      Gets the root data. 获取根数据。
      Returns:
      the root object | 根对象
    • asMap

      public Map<String,Object> asMap()
      Gets the root as a Map. 获取根作为 Map。
      Returns:
      the root map | 根映射
    • asList

      public <T> List<T> asList()
      Gets the root as a List. 获取根作为 List。
      Returns:
      the root list | 根列表
    • get

      public <T> T get(String path)
      Gets a value at the specified path. 获取指定路径的值。
      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      path - the path string | 路径字符串
      Returns:
      the value | 值
    • get

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

      public YmlDocument getDocument(String path)
      Gets a sub-document at the specified path. 获取指定路径的子文档。
      Parameters:
      path - the path string | 路径字符串
      Returns:
      the sub-document | 子文档
    • getString

      public String getString(String path)
      Gets a string value. 获取字符串值。
      Parameters:
      path - the path | 路径
      Returns:
      the string value | 字符串值
    • getString

      public String getString(String path, String defaultValue)
      Gets a string value with default. 获取字符串值(带默认值)。
      Parameters:
      path - the path | 路径
      defaultValue - the default | 默认值
      Returns:
      the string value or default | 字符串值或默认值
    • getInt

      public Integer getInt(String path)
      Gets an integer value. 获取整数值。
      Parameters:
      path - the path | 路径
      Returns:
      the integer value | 整数值
    • getInt

      public int getInt(String path, int defaultValue)
      Gets an integer value with default. 获取整数值(带默认值)。
      Parameters:
      path - the path | 路径
      defaultValue - the default | 默认值
      Returns:
      the integer value or default | 整数值或默认值
    • getLong

      public Long getLong(String path)
      Gets a long value. 获取长整数值。
      Parameters:
      path - the path | 路径
      Returns:
      the long value | 长整数值
    • getBoolean

      public Boolean getBoolean(String path)
      Gets a boolean value. 获取布尔值。
      Parameters:
      path - the path | 路径
      Returns:
      the boolean value | 布尔值
    • getBoolean

      public boolean getBoolean(String path, boolean defaultValue)
      Gets a boolean value with default. 获取布尔值(带默认值)。
      Parameters:
      path - the path | 路径
      defaultValue - the default | 默认值
      Returns:
      the boolean value or default | 布尔值或默认值
    • getList

      public <T> List<T> getList(String path)
      Gets a list value. 获取列表值。
      Type Parameters:
      T - the element type | 元素类型
      Parameters:
      path - the path | 路径
      Returns:
      the list | 列表
    • getMap

      public Map<String,Object> getMap(String path)
      Gets a map value. 获取映射值。
      Parameters:
      path - the path | 路径
      Returns:
      the map | 映射
    • has

      public boolean has(String path)
      Checks if a path exists. 检查路径是否存在。
      Parameters:
      path - the path | 路径
      Returns:
      true if exists | 如果存在则返回 true
    • getOptional

      public <T> Optional<T> getOptional(String path)
      Gets an optional value. 获取可选值。
      Type Parameters:
      T - the type | 类型
      Parameters:
      path - the path | 路径
      Returns:
      optional value | 可选值
    • keys

      public Set<String> keys()
      Gets all keys at the root level. 获取根级别的所有键。
      Returns:
      the keys | 键集合
    • size

      public int size()
      Gets the size of the root element. 获取根元素的大小。
      Returns:
      the size | 大小
    • isEmpty

      public boolean isEmpty()
      Checks if the document is empty. 检查文档是否为空。
      Returns:
      true if empty | 如果为空则返回 true
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object