001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 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 org.antlr.v4.runtime.Token;
023
024import com.puppycrawl.tools.checkstyle.api.DetailNode;
025
026/**
027 * Implementation of DetailNode interface that is mutable.
028 *
029 * @noinspection FieldNotUsedInToString
030 * @noinspectionreason FieldNotUsedInToString - We require a specific string format for
031 *       printing to CLI.
032 */
033public class JavadocNodeImpl implements DetailNode {
034
035    /**
036     * Node type.
037     */
038    private int type;
039
040    /**
041     * Node's text content.
042     */
043    private String text;
044
045    /**
046     * Line number.
047     */
048    private int lineNumber;
049
050    /**
051     * Column number.
052     */
053    private int columnNumber;
054
055    /**
056     * Parent node.
057     */
058    private JavadocNodeImpl parent;
059
060    /**
061     * Next sibling node.
062     */
063    private JavadocNodeImpl nextSibling;
064
065    /**
066     * Previous sibling.
067     */
068    private JavadocNodeImpl previousSibling;
069
070    /**
071     * First child of this DetailAST.
072     */
073    private JavadocNodeImpl firstChild;
074
075    /**
076     * Initializes the node from the given token.
077     *
078     * @param token the token to initialize from.
079     */
080    public void initialize(Token token) {
081        type = token.getType();
082        text = token.getText();
083        lineNumber = token.getLine() - 1;
084        columnNumber = token.getCharPositionInLine();
085    }
086
087    @Override
088    public int getType() {
089        return type;
090    }
091
092    @Override
093    public String getText() {
094        return text;
095    }
096
097    @Override
098    public int getLineNumber() {
099        final JavadocNodeImpl node = firstChild;
100        if (node != null) {
101            lineNumber = node.getLineNumber();
102        }
103        return lineNumber;
104    }
105
106    @Override
107    public int getColumnNumber() {
108        final JavadocNodeImpl node = firstChild;
109        if (node != null) {
110            columnNumber = node.getColumnNumber();
111        }
112        return columnNumber;
113    }
114
115    @Override
116    public DetailNode getParent() {
117        return parent;
118    }
119
120    @Override
121    public DetailNode getNextSibling() {
122        return nextSibling;
123    }
124
125    @Override
126    public DetailNode getPreviousSibling() {
127        return previousSibling;
128    }
129
130    @Override
131    public JavadocNodeImpl getFirstChild() {
132        return firstChild;
133    }
134
135    /**
136     * Sets node's type.
137     *
138     * @param type Node's type.
139     */
140    public void setType(int type) {
141        this.type = type;
142    }
143
144    /**
145     * Sets node's text content.
146     *
147     * @param text Node's text content.
148     */
149    public void setText(String text) {
150        this.text = text;
151    }
152
153    /**
154     * Sets line number.
155     *
156     * @param lineNumber Line number.
157     */
158    public void setLineNumber(int lineNumber) {
159        this.lineNumber = lineNumber;
160    }
161
162    /**
163     * Sets column number.
164     *
165     * @param columnNumber Column number.
166     */
167    public void setColumnNumber(int columnNumber) {
168        this.columnNumber = columnNumber;
169    }
170
171    /**
172     * Sets parent node.
173     *
174     * @param node Parent node.
175     */
176    public void setParent(DetailNode node) {
177        JavadocNodeImpl instance = this;
178        final JavadocNodeImpl newParent = (JavadocNodeImpl) node;
179        do {
180            instance.parent = newParent;
181            instance = instance.nextSibling;
182        } while (instance != null);
183    }
184
185    /**
186     * Sets next sibling node.
187     *
188     * @param nextSibling Next sibling node.
189     */
190    public void setNextSibling(DetailNode nextSibling) {
191        this.nextSibling = (JavadocNodeImpl) nextSibling;
192        ((JavadocNodeImpl) nextSibling).setParent(parent);
193        ((JavadocNodeImpl) nextSibling).previousSibling = this;
194    }
195
196    /**
197     * Adds a child node to this node.
198     *
199     * @param newChild Child node to be added.
200     */
201    public void addChild(DetailNode newChild) {
202        final JavadocNodeImpl astImpl = (JavadocNodeImpl) newChild;
203        astImpl.setParent(this);
204
205        DetailNode temp = firstChild;
206        if (temp == null) {
207            firstChild = (JavadocNodeImpl) newChild;
208        }
209        else {
210            while (temp.getNextSibling() != null) {
211                temp = temp.getNextSibling();
212            }
213
214            ((JavadocNodeImpl) temp).setNextSibling(newChild);
215        }
216    }
217
218    @Override
219    public String toString() {
220        return text + "[" + getLineNumber() + "x" + getColumnNumber() + "]";
221    }
222
223}