package com.eactive.eai.transformer.message.data;

import java.util.ArrayList;
import java.util.Iterator;

public class DataNode {
    private String name;
    private int typeId;

    // Lazy Initialization
    private ArrayList<DataNode> childList;
    private ArrayList<Object> dataList;

    public DataNode(String name, int typeId) {
        this.name = name;
        this.typeId = typeId;
    }

    public String getName() {
        return name;
    }

    public int getTypeId() {
        return typeId;
    }

    public DataNode addData(Object data) {
        if (dataList == null) {
            dataList = new ArrayList<>();
        }

        dataList.add(data);

        return this;
    }

    public Object getData(int i) {
        if (dataList == null) {
            return null;
        }

        return dataList.get(i);
    }

    public int getDataCount() {
        return ((dataList == null) ? 0 : dataList.size());
    }

    @SuppressWarnings("rawtypes")
	public Iterator getDataIterator() {
        return ((dataList == null) ? null : dataList.iterator());
    }

    public boolean hasData() {
        return (dataList != null);
    }

    public DataNode addNode(DataNode node) {
        if (childList == null) {
            childList = new ArrayList<>();
        }

        childList.add(node);

        return node;
    }

    public DataNode getNode(int index) {
        if (childList == null) {
            return null;
        }

        return childList.get(index);
    }

    public int getChildCount() {
        return ((childList == null) ? 0 : childList.size());
    }

    @SuppressWarnings("rawtypes")
	public Iterator getChildIterator() {
        return ((childList == null) ? null : childList.iterator());
    }

    public boolean hasChild() {
        return (childList != null);
    }

    public DataNode findNode(String name) {
        if (childList == null) {
            return null;
        }

        for (Iterator<DataNode> itr = childList.iterator(); itr.hasNext();) {
            DataNode childNode = itr.next();

            if (childNode != null && childNode.getName().equals(name)) {
                return childNode;
            }
        }

        return null;
    }
}
