1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.javadoc;
21  
22  import java.util.Arrays;
23  import java.util.Objects;
24  
25  import com.puppycrawl.tools.checkstyle.api.DetailNode;
26  import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
27  
28  /**
29   * Implementation of DetailNode interface that is mutable.
30   *
31   *
32   */
33  public class JavadocNodeImpl implements DetailNode {
34  
35      /**
36       * Empty array of {@link DetailNode} type.
37       */
38      public static final JavadocNodeImpl[] EMPTY_DETAIL_NODE_ARRAY = new JavadocNodeImpl[0];
39  
40      /**
41       * Node index among parent's children.
42       */
43      private int index;
44  
45      /**
46       * Node type.
47       */
48      private int type;
49  
50      /**
51       * Node's text content.
52       */
53      private String text;
54  
55      /**
56       * Line number.
57       */
58      private int lineNumber;
59  
60      /**
61       * Column number.
62       */
63      private int columnNumber;
64  
65      /**
66       * Array of child nodes.
67       */
68      private DetailNode[] children;
69  
70      /**
71       * Parent node.
72       */
73      private DetailNode parent;
74  
75      @Override
76      public int getType() {
77          return type;
78      }
79  
80      @Override
81      public String getText() {
82          return text;
83      }
84  
85      @Override
86      public int getLineNumber() {
87          return lineNumber;
88      }
89  
90      @Override
91      public int getColumnNumber() {
92          return columnNumber;
93      }
94  
95      @Override
96      public DetailNode[] getChildren() {
97          DetailNode[] nodeChildren = EMPTY_DETAIL_NODE_ARRAY;
98          if (children != null) {
99              nodeChildren = Arrays.copyOf(children, children.length);
100         }
101         return nodeChildren;
102     }
103 
104     @Override
105     public DetailNode getParent() {
106         return parent;
107     }
108 
109     @Override
110     public int getIndex() {
111         return index;
112     }
113 
114     /**
115      * Sets node's type.
116      * @param type Node's type.
117      */
118     public void setType(int type) {
119         this.type = type;
120     }
121 
122     /**
123      * Sets node's text content.
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      * @param lineNumber Line number.
133      */
134     public void setLineNumber(int lineNumber) {
135         this.lineNumber = lineNumber;
136     }
137 
138     /**
139      * Sets column number.
140      * @param columnNumber Column number.
141      */
142     public void setColumnNumber(int columnNumber) {
143         this.columnNumber = columnNumber;
144     }
145 
146     /**
147      * Sets array of child nodes.
148      * @param children Array of child nodes.
149      */
150     public void setChildren(DetailNode... children) {
151         this.children = Arrays.copyOf(children, children.length);
152     }
153 
154     /**
155      * Sets parent node.
156      * @param parent Parent node.
157      */
158     public void setParent(DetailNode parent) {
159         this.parent = parent;
160     }
161 
162     /**
163      * Sets node's index among parent's children.
164      * @param index Node's index among parent's children.
165      */
166     public void setIndex(int index) {
167         this.index = index;
168     }
169 
170     @Override
171     public String toString() {
172         return "JavadocNodeImpl["
173                 + "index=" + index
174                 + ", type=" + JavadocUtil.getTokenName(type)
175                 + ", text='" + text + '\''
176                 + ", lineNumber=" + lineNumber
177                 + ", columnNumber=" + columnNumber
178                 + ", children=" + Objects.hashCode(children)
179                 + ", parent=" + parent + ']';
180     }
181 
182 }