001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.javadoc;
021
022import java.util.Objects;
023import java.util.Optional;
024
025import com.puppycrawl.tools.checkstyle.api.DetailNode;
026import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
027import com.puppycrawl.tools.checkstyle.utils.UnmodifiableCollectionUtil;
028
029/**
030 * Implementation of DetailNode interface that is mutable.
031 *
032 */
033public class JavadocNodeImpl implements DetailNode {
034
035    /**
036     * Empty array of {@link DetailNode} type.
037     */
038    public static final JavadocNodeImpl[] EMPTY_DETAIL_NODE_ARRAY = new JavadocNodeImpl[0];
039
040    /**
041     * Node index among parent's children.
042     */
043    private int index;
044
045    /**
046     * Node type.
047     */
048    private int type;
049
050    /**
051     * Node's text content.
052     */
053    private String text;
054
055    /**
056     * Line number.
057     */
058    private int lineNumber;
059
060    /**
061     * Column number.
062     */
063    private int columnNumber;
064
065    /**
066     * Array of child nodes.
067     */
068    private DetailNode[] children;
069
070    /**
071     * Parent node.
072     */
073    private DetailNode parent;
074
075    @Override
076    public int getType() {
077        return type;
078    }
079
080    @Override
081    public String getText() {
082        return text;
083    }
084
085    @Override
086    public int getLineNumber() {
087        return lineNumber;
088    }
089
090    @Override
091    public int getColumnNumber() {
092        return columnNumber;
093    }
094
095    @Override
096    public DetailNode[] getChildren() {
097        return Optional.ofNullable(children)
098                .map(array -> UnmodifiableCollectionUtil.copyOfArray(array, array.length))
099                .orElse(EMPTY_DETAIL_NODE_ARRAY);
100    }
101
102    @Override
103    public DetailNode getParent() {
104        return parent;
105    }
106
107    @Override
108    public int getIndex() {
109        return index;
110    }
111
112    /**
113     * Sets node's type.
114     *
115     * @param type Node's type.
116     */
117    public void setType(int type) {
118        this.type = type;
119    }
120
121    /**
122     * Sets node's text content.
123     *
124     * @param text Node's text content.
125     */
126    public void setText(String text) {
127        this.text = text;
128    }
129
130    /**
131     * Sets line number.
132     *
133     * @param lineNumber Line number.
134     */
135    public void setLineNumber(int lineNumber) {
136        this.lineNumber = lineNumber;
137    }
138
139    /**
140     * Sets column number.
141     *
142     * @param columnNumber Column number.
143     */
144    public void setColumnNumber(int columnNumber) {
145        this.columnNumber = columnNumber;
146    }
147
148    /**
149     * Sets array of child nodes.
150     *
151     * @param children Array of child nodes.
152     */
153    public void setChildren(DetailNode... children) {
154        this.children = UnmodifiableCollectionUtil.copyOfArray(children, children.length);
155    }
156
157    /**
158     * Sets parent node.
159     *
160     * @param parent Parent node.
161     */
162    public void setParent(DetailNode parent) {
163        this.parent = parent;
164    }
165
166    /**
167     * Sets node's index among parent's children.
168     *
169     * @param index Node's index among parent's children.
170     */
171    public void setIndex(int index) {
172        this.index = index;
173    }
174
175    @Override
176    public String toString() {
177        return "JavadocNodeImpl["
178                + "index=" + index
179                + ", type=" + JavadocUtil.getTokenName(type)
180                + ", text='" + text + '\''
181                + ", lineNumber=" + lineNumber
182                + ", columnNumber=" + columnNumber
183                + ", children=" + Objects.hashCode(children)
184                + ", parent=" + parent + ']';
185    }
186
187}