Class MapContext

java.lang.Object
cloud.opencode.base.expression.context.MapContext
All Implemented Interfaces:
EvaluationContext

public class MapContext extends Object implements EvaluationContext
Map-based Evaluation Context 基于 Map 的求值上下文

A lightweight context that wraps a Map for simple variable-based expressions. The map values are treated as variables.

一个轻量级上下文,包装 Map 用于简单的基于变量的表达式。Map 值被视为变量。

Usage | 用法

Map<String, Object> vars = Map.of("name", "John", "age", 30);
MapContext ctx = new MapContext(vars);
Object result = OpenExpression.eval("name + ' is ' + age", ctx);

Features | 主要功能:

  • Lightweight context backed by HashMap - 基于HashMap的轻量级上下文
  • Map values treated as variables - Map值被视为变量
  • Builder pattern for construction - 构建器模式用于构造

Security | 安全性:

  • Thread-safe: No, uses HashMap internally - 线程安全: 否,内部使用HashMap
  • Null-safe: Yes, null name ignored on setVariable - 空值安全: 是,setVariable中忽略null名称

Usage Examples | 使用示例:

MapContext ctx = new MapContext(Map.of("name", "John", "age", 30));
Object result = OpenExpression.eval("name + ' is ' + age", ctx);  // "John is 30"
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • MapContext

      public MapContext()
      Create empty map context 创建空的 Map 上下文
    • MapContext

      public MapContext(Map<String,Object> variables)
      Create map context with initial variables 使用初始变量创建 Map 上下文
      Parameters:
      variables - the variable map | 变量映射
    • MapContext

      public MapContext(Map<String,Object> variables, FunctionRegistry functionRegistry, Sandbox sandbox)
      Create map context with full customization 使用完整自定义创建 Map 上下文
      Parameters:
      variables - the variable map | 变量映射
      functionRegistry - the function registry | 函数注册表
      sandbox - the security sandbox | 安全沙箱
  • Method Details

    • getRootObject

      public Object getRootObject()
      Description copied from interface: EvaluationContext
      Get the root object 获取根对象
      Specified by:
      getRootObject in interface EvaluationContext
      Returns:
      the root object | 根对象
    • setRootObject

      public void setRootObject(Object root)
      Description copied from interface: EvaluationContext
      Set the root object 设置根对象
      Specified by:
      setRootObject in interface EvaluationContext
      Parameters:
      root - the root object | 根对象
    • getVariable

      public Object getVariable(String name)
      Description copied from interface: EvaluationContext
      Get variable value 获取变量值
      Specified by:
      getVariable in interface EvaluationContext
      Parameters:
      name - the variable name | 变量名
      Returns:
      the variable value | 变量值
    • setVariable

      public void setVariable(String name, Object value)
      Description copied from interface: EvaluationContext
      Set variable value 设置变量值
      Specified by:
      setVariable in interface EvaluationContext
      Parameters:
      name - the variable name | 变量名
      value - the variable value | 变量值
    • hasVariable

      public boolean hasVariable(String name)
      Description copied from interface: EvaluationContext
      Check if variable exists 检查变量是否存在
      Specified by:
      hasVariable in interface EvaluationContext
      Parameters:
      name - the variable name | 变量名
      Returns:
      true if exists | 如果存在返回true
    • getVariables

      public Map<String,Object> getVariables()
      Description copied from interface: EvaluationContext
      Get all variables 获取所有变量
      Specified by:
      getVariables in interface EvaluationContext
      Returns:
      the variable map | 变量映射
    • getFunctionRegistry

      public FunctionRegistry getFunctionRegistry()
      Description copied from interface: EvaluationContext
      Get function registry 获取函数注册表
      Specified by:
      getFunctionRegistry in interface EvaluationContext
      Returns:
      the function registry | 函数注册表
    • getPropertyAccessors

      public List<PropertyAccessor> getPropertyAccessors()
      Description copied from interface: EvaluationContext
      Get property accessors 获取属性访问器
      Specified by:
      getPropertyAccessors in interface EvaluationContext
      Returns:
      the property accessor list | 属性访问器列表
    • getTypeConverter

      public TypeConverter getTypeConverter()
      Description copied from interface: EvaluationContext
      Get type converter 获取类型转换器
      Specified by:
      getTypeConverter in interface EvaluationContext
      Returns:
      the type converter | 类型转换器
    • getSandbox

      public Sandbox getSandbox()
      Description copied from interface: EvaluationContext
      Get security sandbox 获取安全沙箱
      Specified by:
      getSandbox in interface EvaluationContext
      Returns:
      the sandbox, or null if not configured | 沙箱,如果未配置则返回null
    • createChild

      public EvaluationContext createChild()
      Description copied from interface: EvaluationContext
      Create child context 创建子上下文

      Creates a child context that inherits from this context but can have its own variable bindings. Used for collection operations.

      创建继承自此上下文但可以有自己变量绑定的子上下文。用于集合操作。

      Specified by:
      createChild in interface EvaluationContext
      Returns:
      the child context | 子上下文
    • builder

      public static MapContext.Builder builder()
      Create a builder for MapContext 创建 MapContext 的构建器
      Returns:
      the builder | 构建器
    • of

      public static MapContext of(Map<String,Object> map)
      Create from map 从 Map 创建
      Parameters:
      map - the variable map | 变量映射
      Returns:
      the context | 上下文