Class EnvironmentStrategy

java.lang.Object
cloud.opencode.base.feature.strategy.EnvironmentStrategy
All Implemented Interfaces:
EnableStrategy

public class EnvironmentStrategy extends Object implements EnableStrategy
Environment-Based Enable Strategy 基于环境的启用策略

Strategy that enables/disables features based on the deployment environment.

根据部署环境启用/禁用功能的策略。

Environment Resolution Order | 环境解析顺序:

  1. Context attribute "environment" - 上下文属性 "environment"
  2. System property "app.environment" - 系统属性 "app.environment"
  3. Environment variable "APP_ENVIRONMENT" - 环境变量 "APP_ENVIRONMENT"
  4. Default: "default" - 默认: "default"

Usage Examples | 使用示例:

EnvironmentStrategy strategy = EnvironmentStrategy.builder()
    .dev(true)
    .staging(true)
    .prod(false)
    .defaultState(false)
    .build();

Feature feature = Feature.builder("debug-mode")
    .strategy(strategy)
    .build();

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
  • Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
Since:
JDK 25, opencode-base-feature V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • isEnabled

      public boolean isEnabled(Feature feature, FeatureContext context)
      Evaluate if the feature is enabled for the current environment 评估功能是否在当前环境中启用
      Specified by:
      isEnabled in interface EnableStrategy
      Parameters:
      feature - the feature being evaluated | 正在评估的功能
      context - the evaluation context | 评估上下文
      Returns:
      true if enabled for the current environment | 如果在当前环境中启用返回true
    • getEnvironmentStates

      public Map<String,Boolean> getEnvironmentStates()
      Get the environment states map 获取环境状态映射
      Returns:
      immutable map of environment to enabled state | 环境到启用状态的不可变映射
    • getDefaultState

      public boolean getDefaultState()
      Get the default state 获取默认状态
      Returns:
      default state | 默认状态
    • builder

      public static EnvironmentStrategy.Builder builder()
      Create a new builder 创建新的构建器
      Returns:
      new builder | 新的构建器