package com.bpa.scalage.HBL.DataUtil;


import com.bpa.scalage.HBL.Interface.IRunT;

import java.util.LinkedHashMap;

public class NotifyPropVar<T> {

    public NotifyPropVar(T defaultValue){
        _value=defaultValue;
    }
    public IRunT<T> ChangeNotify;
    private T _value;
    private boolean IsFirst=false;
    private LinkedHashMap<String,IRunT<T>> changedNotifys=new LinkedHashMap<>();
    public T getValue(){
        return _value;
    }

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

    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;
        }
    }


}
