Class ExpressionCompiler

java.lang.Object
cloud.opencode.base.expression.compiler.ExpressionCompiler

public class ExpressionCompiler extends Object
Expression Compiler 表达式编译器

Compiles expression strings into optimized AST nodes that can be efficiently evaluated. Supports caching and optimization for frequently used expressions.

将表达式字符串编译为可高效求值的优化 AST 节点。支持对常用表达式进行缓存和优化。

Features | 主要功能:

  • Compile expressions with optional optimization - 编译表达式,可选优化
  • Configurable caching strategy - 可配置的缓存策略
  • Builder pattern for customization - 构建器模式用于自定义
  • Factory methods for common configurations - 常见配置的工厂方法

Usage Examples | 使用示例:

// Default compiler with cache and optimization
ExpressionCompiler compiler = ExpressionCompiler.getDefault();
CompiledExpression expr = compiler.compile("a + b * c");

// Custom compiler via builder
ExpressionCompiler custom = ExpressionCompiler.builder()
    .cacheSize(500)
    .optimization(true)
    .build();

Security | 安全性:

  • Thread-safe: Yes, delegates to thread-safe cache - 线程安全: 是,委托给线程安全的缓存
  • Null-safe: No, null expression not supported - 空值安全: 否,不支持null表达式

Performance | 性能特性:

  • Time complexity: compile() O(1) for cached expressions; O(n) for first compilation where n is the expression length - 时间复杂度: 缓存命中时 compile() 为 O(1);首次编译为 O(n),n为表达式长度
  • Space complexity: O(n) for the AST; O(c) for the cache where c is the cache size - 空间复杂度: AST 为 O(n);缓存为 O(c),c为缓存大小
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • ExpressionCompiler

      public ExpressionCompiler()
      Create compiler with default settings 使用默认设置创建编译器
    • ExpressionCompiler

      public ExpressionCompiler(ExpressionCache cache, Optimizer optimizer, boolean optimizationEnabled)
      Create compiler with custom settings 使用自定义设置创建编译器
      Parameters:
      cache - the expression cache | 表达式缓存
      optimizer - the optimizer | 优化器
      optimizationEnabled - whether optimization is enabled | 是否启用优化
  • Method Details

    • getDefault

      public static ExpressionCompiler getDefault()
      Get the default compiler instance 获取默认编译器实例
      Returns:
      the default compiler | 默认编译器
    • create

      public static ExpressionCompiler create()
      Create a new compiler 创建新编译器
      Returns:
      new compiler | 新编译器
    • withoutCache

      public static ExpressionCompiler withoutCache()
      Create a compiler without caching 创建不带缓存的编译器
      Returns:
      the compiler | 编译器
    • withoutOptimization

      public static ExpressionCompiler withoutOptimization()
      Create a compiler without optimization 创建不带优化的编译器
      Returns:
      the compiler | 编译器
    • compile

      public CompiledExpression compile(String expression)
      Compile expression string 编译表达式字符串
      Parameters:
      expression - the expression string | 表达式字符串
      Returns:
      the compiled expression | 编译后的表达式
    • compileWithoutCache

      public CompiledExpression compileWithoutCache(String expression)
      Compile expression without caching 编译表达式不使用缓存
      Parameters:
      expression - the expression string | 表达式字符串
      Returns:
      the compiled expression | 编译后的表达式
    • compile

      public CompiledExpression compile(String expression, boolean optimize)
      Compile expression with explicit optimization setting 使用显式优化设置编译表达式
      Parameters:
      expression - the expression string | 表达式字符串
      optimize - whether to optimize | 是否优化
      Returns:
      the compiled expression | 编译后的表达式
    • isCached

      public boolean isCached(String expression)
      Check if expression is cached 检查表达式是否已缓存
      Parameters:
      expression - the expression string | 表达式字符串
      Returns:
      true if cached | 如果已缓存返回 true
    • getCache

      public ExpressionCache getCache()
      Get the cache 获取缓存
      Returns:
      the cache | 缓存
    • getOptimizer

      public Optimizer getOptimizer()
      Get the optimizer 获取优化器
      Returns:
      the optimizer | 优化器
    • isOptimizationEnabled

      public boolean isOptimizationEnabled()
      Check if optimization is enabled 检查是否启用优化
      Returns:
      true if enabled | 如果启用返回 true
    • clearCache

      public void clearCache()
      Clear the cache 清除缓存
    • builder

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