前言SingletonSupplierSupplierUtilsdemo总结
前言Spring 针对 java function 提供了两个工具 API
SingletonSupplierSupplierUtils SingletonSupplier
public class SingletonSupplierimplements Supplier { // 包装的真正的 Supplier @Nullable private final Supplier extends T> instanceSupplier; // 缺省回调 @Nullable private final Supplier extends T> defaultSupplier; // 包装的实例 @Nullable private volatile T singletonInstance; public SingletonSupplier(@Nullable T instance, Supplier extends T> defaultSupplier) { this.instanceSupplier = null; this.defaultSupplier = defaultSupplier; this.singletonInstance = instance; } public SingletonSupplier(@Nullable Supplier extends T> instanceSupplier, Supplier extends T> defaultSupplier) { this.instanceSupplier = instanceSupplier; this.defaultSupplier = defaultSupplier; } // 私有构造 private SingletonSupplier(Supplier extends T> supplier) { this.instanceSupplier = supplier; this.defaultSupplier = null; } private SingletonSupplier(T singletonInstance) { this.instanceSupplier = null; this.defaultSupplier = null; this.singletonInstance = singletonInstance; } // 获取最终实例 @Override @Nullable public T get() { T instance = this.singletonInstance; if (instance == null) { synchronized (this) { instance = this.singletonInstance; if (instance == null) { if (this.instanceSupplier != null) { instance = this.instanceSupplier.get(); } if (instance == null && this.defaultSupplier != null) { instance = this.defaultSupplier.get(); } this.singletonInstance = instance; } } } return instance; } // 该方法不会返回 null 的实例 public T obtain() { T instance = get(); Assert.state(instance != null, "No instance from Supplier"); return instance; } public static SingletonSupplier of(T instance) { return new SingletonSupplier<>(instance); } @Nullable public static SingletonSupplier ofNullable(@Nullable T instance) { return (instance != null ? new SingletonSupplier<>(instance) : null); } public static SingletonSupplier of(Supplier supplier) { return new SingletonSupplier<>(supplier); } @Nullable public static SingletonSupplier ofNullable(@Nullable Supplier supplier) { return (supplier != null ? new SingletonSupplier<>(supplier) : null); } }
一个维护单例的 Supplier,可以分别指定
instance:单例实例instanceSupplier:单例 SupplierdefaultSupplier:缺省回调 get 方法获取最终单例实例obtain 是 NullSafe 的 get提供了一系列静态构造方法 SupplierUtils
public abstract class SupplierUtils { @Nullable public staticT resolve(@Nullable Supplier supplier) { return (supplier != null ? supplier.get() : null); } }
supplier.get() 的 NullSafe 版本配合 SingletonSupplier 食用更佳 demo
public class SingletonSupplierDemo { @Test public void test() { SingletonSupplier总结supplier = new SingletonSupplier<>(null, () -> "default"); System.out.println(supplier.get()); supplier = new SingletonSupplier<>(() -> "dd", () -> "default"); System.out.println(supplier.get()); supplier = SingletonSupplier.ofNullable("dd"); System.out.println(supplier.get()); supplier = SingletonSupplier.ofNullable(() -> "dd"); System.out.println(supplier.get()); System.out.println(SupplierUtils.resolve(supplier)); } }
Spring 工具类
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)