package com.usoftchina.uas.office.context;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.usoftchina.uas.office.entity.Master;

/**
 * @author yingp
 * @date 2020/1/13
 */
public class MasterHolder {

    private static final ThreadLocal<Master> threadLocalMaster = new TransmittableThreadLocal<>();

    /**
     * 获取当前Master
     *
     * @return
     */
    public static Master get() {
        return threadLocalMaster.get();
    }

    /**
     * 切换Master
     *
     * @param master
     */
    public static void set(Master master) {
        Master old = get();
        boolean changed = null == old || !old.equals(master);
        if (changed) {
            threadLocalMaster.set(master);
        }
    }

    public static void clear() {
        threadLocalMaster.set(null);
    }
}
