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

cn.nukkit.nbt.snbt.BaseNode Maven / Gradle / Ivy

There is a newer version: 1.20.40-r1
Show newest version
/* Generated by: JavaCC 21 Parser Generator. Do not edit. BaseNode.java */
package cn.nukkit.nbt.snbt;

import java.util.*;


/**
 * The base concrete class for non-terminal Nodes
 */
public class BaseNode implements Node {
    private SNBTLexer tokenSource;

    public SNBTLexer getTokenSource() {
        if (tokenSource == null) {
            for (Node child : children()) {
                tokenSource = child.getTokenSource();
                if (tokenSource != null) break;
            }
        }
        return tokenSource;
    }

    public void setTokenSource(SNBTLexer tokenSource) {
        this.tokenSource = tokenSource;
    }

    static private Class listClass;

    /**
     * Sets the List class that is used to store child nodes. By default,
     * this is java.util.ArrayList. There is probably very little reason
     * to ever use anything else, though you could use this method
     * to replace this with LinkedList or your own java.util.List implementation even.
     *
     * @param listClass the #java.util.List implementation to use internally
     *                  for the child nodes. By default #java.util.ArrayList is used.
     */
    static public void setListClass(Class listClass) {
        BaseNode.listClass = listClass;
    }

    @SuppressWarnings("unchecked")
    private List newList() {
        if (listClass == null) {
            return new ArrayList<>();
        }
        try {
            return (List) listClass.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * the parent node
     */
    private Node parent;
    /**
     * the child nodes
     */
    private List children = newList();
    private int beginOffset, endOffset;
    private boolean unparsed;

    public boolean isUnparsed() {
        return this.unparsed;
    }

    public void setUnparsed(boolean unparsed) {
        this.unparsed = unparsed;
    }

    public void setParent(Node n) {
        parent = n;
    }

    public Node getParent() {
        return parent;
    }

    public void addChild(Node n) {
        children.add(n);
        n.setParent(this);
    }

    public void addChild(int i, Node n) {
        children.add(i, n);
        n.setParent(this);
    }

    public Node getChild(int i) {
        return children.get(i);
    }

    public void setChild(int i, Node n) {
        children.set(i, n);
        n.setParent(this);
    }

    public Node removeChild(int i) {
        return children.remove(i);
    }

    public void clearChildren() {
        children.clear();
    }

    public int getChildCount() {
        return children.size();
    }

    public List children() {
        return Collections.unmodifiableList(children);
    }

    public int getBeginOffset() {
        return beginOffset;
    }

    public void setBeginOffset(int beginOffset) {
        this.beginOffset = beginOffset;
    }

    public int getEndOffset() {
        return endOffset;
    }

    public void setEndOffset(int endOffset) {
        this.endOffset = endOffset;
    }

    public String toString() {
        StringBuilder buf = new StringBuilder();
        for (Token t : getRealTokens()) {
            buf.append(t);
        }
        return buf.toString();
    }

    private Map namedChildMap;
    private Map> namedChildListMap;

    public Node getNamedChild(String name) {
        if (namedChildMap == null) {
            return null;
        }
        return namedChildMap.get(name);
    }

    public void setNamedChild(String name, Node node) {
        if (namedChildMap == null) {
            namedChildMap = new HashMap<>();
        }
        if (namedChildMap.containsKey(name)) {
            // Can't have duplicates
            String msg = String.format("Duplicate named child not allowed: {0}", name);
            throw new RuntimeException(msg);
        }
        namedChildMap.put(name, node);
    }

    public List getNamedChildList(String name) {
        if (namedChildListMap == null) {
            return null;
        }
        return namedChildListMap.get(name);
    }

    public void addToNamedChildList(String name, Node node) {
        if (namedChildListMap == null) {
            namedChildListMap = new HashMap<>();
        }
        List nodeList = namedChildListMap.get(name);
        if (nodeList == null) {
            nodeList = new ArrayList<>();
            namedChildListMap.put(name, nodeList);
        }
        nodeList.add(node);
    }

}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy