package cc.util.bossshop.config;

import org.apache.commons.lang.Validate;

public class YamlNode{
    
    
    enum ChildType{
        NODE,STRING,ARRAY
    }
    
    /**该行是否已经关闭*/
    public boolean mClosed=true;
    /**节点深度(空格数+1/2)*/
    public final int mDeep;
    /**节点名字*/
    public final String mName;
    /**子节点类型,默认为null,单值为String.class,多值为List.class,节点为Object.class*/
    private ChildType mChildType=null;
    /**父节点*/
    private YamlNode mParent=null;
    
    
    public YamlNode(String pName,int pDeep,ChildType pType){
        if(pName==null) pName="";
        this.mName=pName;
        this.mDeep=pDeep;
        this.mChildType=pType;
    }
    
    /**
     * 设置节点类型,如果节点类型已经存在并且与设置的节点类型不同,将返回false
     * @param pType 节点类型不能为null
     * @return 是否设置成功
     */
    public boolean setChildType(ChildType pType){
        Validate.notNull(pType,"子节点类型不能为null");
        if(this.mChildType==null||this.mChildType==pType){
            this.mChildType=pType;
            return true;
        }else return false;
    }
    
    /**
     * 获取层级
     */
    public int getDeep(){
        return this.mDeep;
    }
    
    public String getPath(){
        if(this.mParent==null) return this.mName;
        String parentPath=this.mParent.getPath();
        if(parentPath.isEmpty()) return this.mName;
        else return parentPath+"."+this.mName;
    }
    
    public YamlNode setParent(YamlNode pParent){
        this.mParent=pParent;
        return this;
    }
    
    public YamlNode getParent(){
        return this.mParent;
    }
    
    @Override
    public String toString(){
        return this.mDeep+"|"+this.getPath();
    }
    

}
