package com.bonait.bnframework.HBL.DataUtil;



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

import java.io.Serializable;
import java.util.LinkedHashMap;

public class NotifyPropVar<T extends Serializable>   {

    public NotifyPropVar(T defaultValue){
        _value=defaultValue;
    }
    public IRunT<T> ChangeNotify;

    private boolean IsFirst=false;
    private LinkedHashMap<String,IRunT<T>> changedNotifys=new LinkedHashMap<>();

    public void Register(String key,IRunT<T> notify){
        if(changedNotifys.containsKey(key))changedNotifys.remove(key);
        changedNotifys.put(key,notify);
    }

    private T _value;
    public T getValue(){ return _value; }
    public void setValue(T value){
        if (value != null && !value.equals(_value) || !IsFirst)
        {
            _value=value;
           if(ChangeNotify!=null) ChangeNotify.Run(value);
            changedNotifys.values().forEach(item->{item.Run(value);});
            IsFirst = true;
        }
    }
}

