package com.bonait.bnframework.HBL.Trig;

import com.bonait.bnframework.HBL.Interface.IRun;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 下降沿操作类
 */
public class TTrig {
    private static volatile ConcurrentHashMap<String, TTrig> _Instance;

    public static TTrig get(String name) {
        if (_Instance == null)
            _Instance = new ConcurrentHashMap<String, TTrig>();
        if (!_Instance.containsKey(name))
            _Instance.put(name, new TTrig());
        return _Instance.get(name);
    }

    public static void Remove(String name){
        if(_Instance!=null&&_Instance.containsKey(name)){
            _Instance.remove(name);
        }
    }

    private TTrig() {
    }

    private boolean flag=false;
    private boolean Q=false;

    public boolean getQ() {
        return Q;
    }

    private void setIN(boolean falag) {
        Q = !falag && flag;
        flag = falag;
    }

    public boolean Start(boolean IN, IRun callback) {
        setIN(IN);
        if (Q&&callback!=null)callback.Run();
        return Q;
    }

    public boolean Start(boolean IN) {
        setIN(IN);
        return Q;
    }

}
