All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.sdk.json.JSONNodeData Maven / Gradle / Ivy

There is a newer version: 1.5.17
Show newest version
package org.zodiac.sdk.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.zodiac.sdk.json.core.Feature;

public class JSONNodeData {

    /** 节点数据的 value */
    public JSONValue  value = null;
    /** 节点数据的 object */
    public Map object = null;
    /** 节点数据的 array */
    public List array = null;

    /** 节点类型 */
    public JSONNodeType nodeType = JSONNodeType.Null;

    protected JSONNode _n;

    public JSONNodeData(JSONNode n){
        _n = n;
    }

    public Map object(){
        tryInitObject();
        return object;
    }

    public List array(){
        tryInitArray();
        return array;
    }

    public JSONValue value(){
        tryInitValue();
        return value;
    }

    /** 尝试初始化为 null */
    protected void tryInitNull(){
        if(nodeType != JSONNodeType.Null) {
            nodeType = JSONNodeType.Null;

            if (object != null) {
                object.clear();
                object = null;
            }

            if (array != null) {
                array.clear();
                array = null;
            }

            value = null;
        }
    }

    /** 尝试初始化为 value */
    protected  void tryInitValue() {
        if (nodeType != JSONNodeType.Value) {
            nodeType = JSONNodeType.Value;

            if (value == null) {
                value = new JSONValue(_n);
            }
        }
    }

    /** 尝试初始化为 object */
    protected  void tryInitObject() {
        if (nodeType != JSONNodeType.Object) {
            nodeType = JSONNodeType.Object;

            if (object == null) {
                if(_n._o.hasFeature(Feature.OrderedField)){
                    object = new ONodeLinkedObject();
                }else {
                    object = new ONodeObject();
                }
            }
        }
    }

    /** 尝试初始化为 array */
    protected  void tryInitArray() {
        if (nodeType != JSONNodeType.Array) {
            nodeType = JSONNodeType.Array;

            if (array == null) {
                array = new ONodeArray();
            }
        }
    }

    /** 尝试将 object 换为 array(一般用不到) */
    protected void shiftToArray(){
        tryInitArray();

        if(object!=null) {
            for (JSONNode n1 : object.values()) {
                array.add(n1);
            }

            object.clear();
            object = null;
        }
    }

    //////////////////////////////

    /** 节点的 特性 */
    public Map attrs = null;
    public String attrGet(String key){
        if(attrs != null){
            return attrs.get(key);
        }else{
            return null;
        }
    }
    public void attrSet(String key, String val){
        if(attrs == null){
            attrs = new LinkedHashMap<>();
        }

        attrs.put(key, val);
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null) {
            return false;
        }

        return this.hashCode() == o.hashCode();
    }

    @Override
    public int hashCode() {
        if(nodeType == JSONNodeType.Object){
            return object.hashCode();
        }

        if(nodeType == JSONNodeType.Array){
            return array.hashCode();
        }

        if(nodeType == JSONNodeType.Value){
            return value.hashCode();
        }

        return 0;
    }

    class ONodeArray extends ArrayList {
        @Override
        public int indexOf(Object o) {
            for (int i = 0; i < size(); i++)
                if (get(i).equals(o))
                    return i;

            return -1;
        }
    }

    class ONodeObject extends HashMap {
        @Override
        public boolean containsValue(Object value) {
            for(JSONNode n: values()){
                if(n.equals(value)){
                    return true;
                }
            }
            return false;
        }
    }

    class ONodeLinkedObject extends LinkedHashMap {
        @Override
        public boolean containsValue(Object value) {
            for(JSONNode n: values()){
                if(n.equals(value)){
                    return true;
                }
            }
            return false;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy