Spring function util —— SingletonSupplier SupplierUtils

Spring function util —— SingletonSupplier SupplierUtils,第1张

Spring function util —— SingletonSupplier SupplierUtils

Spring function util —— SingletonSupplier SupplierUtils

前言SingletonSupplierSupplierUtilsdemo总结

前言

Spring 针对 java function 提供了两个工具 API

SingletonSupplierSupplierUtils SingletonSupplier

public class SingletonSupplier implements Supplier {

	// 包装的真正的 Supplier
	@Nullable
	private final Supplier instanceSupplier;

	// 缺省回调
	@Nullable
	private final Supplier defaultSupplier;

	// 包装的实例
	@Nullable
	private volatile T singletonInstance;

	
	public SingletonSupplier(@Nullable T instance, Supplier defaultSupplier) {
		this.instanceSupplier = null;
		this.defaultSupplier = defaultSupplier;
		this.singletonInstance = instance;
	}
	public SingletonSupplier(@Nullable Supplier instanceSupplier, Supplier defaultSupplier) {
		this.instanceSupplier = instanceSupplier;
		this.defaultSupplier = defaultSupplier;
	}

	// 私有构造
	private SingletonSupplier(Supplier 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 static  T 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 工具类

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5721924.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存