package com.qimai.android.tools

/**
 * @author wangwei

 * @version 1.1.1
 * @date 2019-08-13
 * @Description
 * @email wangwei_5521@163.com
 **/
open class SingletonHolder<out T : Any, in A>(creator: (A) -> T) {

    private var creator: ((A) -> T)? = creator


    @Volatile
    private var instance: T? = null

    fun getInstance(arg: A): T {

        var i = instance
        if (i != null) {
            return i
        }
        return synchronized(this) {
            val temp = instance
            if (temp != null) {
                return temp
            } else {
                val created = creator!!(arg)
                instance = created
                creator = null
                created
            }
        }
    }


}