Class Suppliers

java.lang.Object
cloud.opencode.base.core.Suppliers

public final class Suppliers extends Object
Suppliers - Utility methods for working with Supplier instances Supplier 工具类 - 提供 Supplier 实例的实用方法

This class provides useful operations on Supplier instances, including memoization (caching) and time-based expiration.

该类提供对 Supplier 实例的实用操作,包括记忆化(缓存)和基于时间的过期。

Features | 主要功能:

  • Memoization (lazy initialization with caching) - 记忆化(惰性初始化并缓存)
  • Time-based expiration - 基于时间的过期
  • Thread-safe implementations - 线程安全实现
  • Compose and synchronize suppliers - 组合和同步 Supplier

Usage Examples | 使用示例:

// Basic memoization | 基本记忆化
Supplier<ExpensiveObject> supplier = Suppliers.memoize(() -> {
    return new ExpensiveObject(); // Only called once
});
ExpensiveObject obj1 = supplier.get(); // Computes value
ExpensiveObject obj2 = supplier.get(); // Returns cached value (same instance)

// Memoization with expiration | 带过期的记忆化
Supplier<Config> configSupplier = Suppliers.memoizeWithExpiration(
    () -> loadConfigFromFile(),
    5, TimeUnit.MINUTES  // Cache for 5 minutes
);

// Using Duration | 使用 Duration
Supplier<Data> dataSupplier = Suppliers.memoizeWithExpiration(
    () -> fetchData(),
    Duration.ofHours(1)
);

// Compose with function | 与函数组合
Supplier<String> stringSupplier = Suppliers.compose(
    Object::toString,
    () -> someObject
);

// Synchronized supplier | 同步的 Supplier
Supplier<Counter> syncSupplier = Suppliers.synchronizedSupplier(unsafeSupplier);

Thread Safety | 线程安全:

All memoized suppliers returned by this class are thread-safe. The memoization uses double-checked locking for optimal performance.

此类返回的所有记忆化 Supplier 都是线程安全的。记忆化使用双重检查锁定以获得最佳性能。

Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • memoize

      @Deprecated(since="1.0.3") public static <T> Supplier<T> memoize(Supplier<T> delegate)
      Deprecated.
      Use Lazy.of(java.util.function.Supplier) instead, which is virtual-thread safe.
      Returns a supplier that caches the instance supplied by the delegate and returns that value on subsequent calls to get(). The delegate is only invoked once. 返回一个 Supplier,它缓存委托提供的实例,并在后续调用 get() 时返回该值。委托只调用一次。

      The returned supplier is thread-safe. The delegate's get() method will be invoked at most once, even if the returned supplier's get() method is called multiple times concurrently.

      返回的 Supplier 是线程安全的。即使并发多次调用返回的 Supplier 的 get() 方法, 委托的 get() 方法也最多只会被调用一次。

      If the delegate is already a memoized supplier (created by this method), it is returned directly.

      如果委托已经是记忆化的 Supplier(由此方法创建),则直接返回。

      Type Parameters:
      T - the type of value supplied
      Parameters:
      delegate - the underlying supplier to memoize
      Returns:
      a memoizing supplier
      Throws:
      NullPointerException - if delegate is null
    • memoizeWithExpiration

      @Deprecated(since="1.0.3") public static <T> Supplier<T> memoizeWithExpiration(Supplier<T> delegate, long duration, TimeUnit unit)
      Deprecated.
      Use Lazy.of(java.util.function.Supplier) instead, which is virtual-thread safe.
      Returns a supplier that caches the instance supplied by the delegate and returns that value on subsequent calls to get(). The value expires after the specified duration. 返回一个 Supplier,它缓存委托提供的实例,并在后续调用 get() 时返回该值。值在指定时间后过期。

      After the specified duration has passed, the next call to get() will trigger a new computation by calling the delegate again.

      指定时间过后,下次调用 get() 将再次调用委托进行新的计算。

      Type Parameters:
      T - the type of value supplied
      Parameters:
      delegate - the underlying supplier
      duration - the duration after which the cached value expires
      unit - the time unit of the duration
      Returns:
      a memoizing supplier with expiration
      Throws:
      NullPointerException - if delegate or unit is null
      IllegalArgumentException - if duration is not positive
    • memoizeWithExpiration

      @Deprecated(since="1.0.3") public static <T> Supplier<T> memoizeWithExpiration(Supplier<T> delegate, Duration duration)
      Deprecated.
      Use Lazy.of(java.util.function.Supplier) instead, which is virtual-thread safe.
      Returns a supplier that caches the instance supplied by the delegate and returns that value on subsequent calls to get(). The value expires after the specified duration. 返回一个 Supplier,它缓存委托提供的实例,并在后续调用 get() 时返回该值。值在指定时间后过期。
      Type Parameters:
      T - the type of value supplied
      Parameters:
      delegate - the underlying supplier
      duration - the duration after which the cached value expires
      Returns:
      a memoizing supplier with expiration
      Throws:
      NullPointerException - if delegate or duration is null
      IllegalArgumentException - if duration is not positive
    • compose

      public static <F,T> Supplier<T> compose(Function<? super F, T> function, Supplier<F> delegate)
      Returns a new supplier that applies the given function to the value from the delegate. 返回一个新的 Supplier,它将给定函数应用于委托的值。

      The returned supplier evaluates the delegate lazily each time get() is called. For cached behavior, wrap the result with memoize().

      返回的 Supplier 每次调用 get() 时惰性计算委托。如需缓存行为,请用 memoize() 包装结果。

      Type Parameters:
      F - the type produced by the delegate
      T - the type produced by the resulting supplier
      Parameters:
      function - the function to apply to the delegate's value
      delegate - the underlying supplier
      Returns:
      a composed supplier
      Throws:
      NullPointerException - if function or delegate is null
    • synchronizedSupplier

      public static <T> Supplier<T> synchronizedSupplier(Supplier<T> delegate)
      Returns a supplier that synchronizes on itself before calling the delegate's get() method. 返回一个 Supplier,它在调用委托的 get() 方法之前对自身进行同步。

      This is useful for making a non-thread-safe supplier thread-safe.

      这对于使非线程安全的 Supplier 变为线程安全很有用。

      Type Parameters:
      T - the type of value supplied
      Parameters:
      delegate - the underlying supplier
      Returns:
      a synchronized supplier
      Throws:
      NullPointerException - if delegate is null
    • ofInstance

      public static <T> Supplier<T> ofInstance(T instance)
      Returns a supplier that always returns the same instance. 返回一个始终返回相同实例的 Supplier。
      Type Parameters:
      T - the type of the instance
      Parameters:
      instance - the instance to return
      Returns:
      a supplier that always returns the given instance