Class DateRangeStrategy

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

public class DateRangeStrategy extends Object implements EnableStrategy
Date Range Strategy 日期范围策略

Strategy that enables feature within a specific time window.

在特定时间窗口内启用功能的策略。

Features | 主要功能:

  • Time-based activation - 基于时间的激活
  • Scheduled features - 定时功能
  • Limited-time promotions - 限时促销

Usage Examples | 使用示例:

// Spring sale (March 2024)
Feature feature = Feature.builder("spring-sale")
    .strategy(new DateRangeStrategy(
        Instant.parse("2024-03-01T00:00:00Z"),
        Instant.parse("2024-03-31T23:59:59Z")
    ))
    .build();

// Using LocalDateTime
Feature feature = Feature.builder("new-year-promo")
    .strategy(DateRangeStrategy.of(
        LocalDateTime.of(2024, 1, 1, 0, 0),
        LocalDateTime.of(2024, 1, 7, 23, 59)
    ))
    .build();

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: No - 空值安全: 否
Since:
JDK 25, opencode-base-feature V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • DateRangeStrategy

      public DateRangeStrategy(Instant startTime, Instant endTime)
      Create date range strategy 创建日期范围策略
      Parameters:
      startTime - the start time (inclusive) | 开始时间(包含)
      endTime - the end time (inclusive) | 结束时间(包含)
  • Method Details

    • of

      public static DateRangeStrategy of(LocalDateTime startTime, LocalDateTime endTime)
      Create from LocalDateTime using system timezone 使用系统时区从LocalDateTime创建
      Parameters:
      startTime - the start time | 开始时间
      endTime - the end time | 结束时间
      Returns:
      new DateRangeStrategy | 新的DateRangeStrategy
    • of

      public static DateRangeStrategy of(LocalDateTime startTime, LocalDateTime endTime, ZoneId zoneId)
      Create from LocalDateTime with specific timezone 使用特定时区从LocalDateTime创建
      Parameters:
      startTime - the start time | 开始时间
      endTime - the end time | 结束时间
      zoneId - the timezone | 时区
      Returns:
      new DateRangeStrategy | 新的DateRangeStrategy
    • until

      public static DateRangeStrategy until(Instant endTime)
      Create strategy that starts now and ends at specified time 创建从现在开始到指定时间结束的策略
      Parameters:
      endTime - the end time | 结束时间
      Returns:
      new DateRangeStrategy | 新的DateRangeStrategy
    • from

      public static DateRangeStrategy from(Instant startTime)
      Create strategy that starts at specified time and never ends 创建从指定时间开始永不结束的策略
      Parameters:
      startTime - the start time | 开始时间
      Returns:
      new DateRangeStrategy | 新的DateRangeStrategy
    • isEnabled

      public boolean isEnabled(Feature feature, FeatureContext context)
      Check if current time is within the date range 检查当前时间是否在日期范围内
      Specified by:
      isEnabled in interface EnableStrategy
      Parameters:
      feature - the feature | 功能
      context - the context | 上下文
      Returns:
      true if within range | 如果在范围内返回true
    • getStartTime

      public Instant getStartTime()
      Get start time 获取开始时间
      Returns:
      start time | 开始时间
    • getEndTime

      public Instant getEndTime()
      Get end time 获取结束时间
      Returns:
      end time | 结束时间
    • hasStarted

      public boolean hasStarted()
      Check if the time window has started 检查时间窗口是否已开始
      Returns:
      true if started | 如果已开始返回true
    • hasEnded

      public boolean hasEnded()
      Check if the time window has ended 检查时间窗口是否已结束
      Returns:
      true if ended | 如果已结束返回true
    • toString

      public String toString()
      Overrides:
      toString in class Object