View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.JavadocUtils;
27  
28  /**
29   * Implementation of DetailNode interface that is mutable.
30   *
31   * @author Baratali Izmailov
32   *
33   */
34  public class JavadocNodeImpl implements DetailNode {
35  
36      /**
37       * Empty array of {@link DetailNode} type.
38       */
39      public static final JavadocNodeImpl[] EMPTY_DETAIL_NODE_ARRAY = new JavadocNodeImpl[0];
40  
41      /**
42       * Node index among parent's children.
43       */
44      private int index;
45  
46      /**
47       * Node type.
48       */
49      private int type;
50  
51      /**
52       * Node's text content.
53       */
54      private String text;
55  
56      /**
57       * Line number.
58       */
59      private int lineNumber;
60  
61      /**
62       * Column number.
63       */
64      private int columnNumber;
65  
66      /**
67       * Array of child nodes.
68       */
69      private DetailNode[] children;
70  
71      /**
72       * Parent node.
73       */
74      private DetailNode parent;
75  
76      @Override
77      public int getType() {
78          return type;
79      }
80  
81      @Override
82      public String getText() {
83          return text;
84      }
85  
86      @Override
87      public int getLineNumber() {
88          return lineNumber;
89      }
90  
91      @Override
92      public int getColumnNumber() {
93          return columnNumber;
94      }
95  
96      @Override
97      public DetailNode[] getChildren() {
98          DetailNode[] nodeChildren = EMPTY_DETAIL_NODE_ARRAY;
99          if (children != null) {
100             nodeChildren = Arrays.copyOf(children, children.length);
101         }
102         return nodeChildren;
103     }
104 
105     @Override
106     public DetailNode getParent() {
107         return parent;
108     }
109 
110     @Override
111     public int getIndex() {
112         return index;
113     }
114 
115     /**
116      * Sets node's type.
117      * @param type Node's type.
118      */
119     public void setType(int type) {
120         this.type = type;
121     }
122 
123     /**
124      * Sets node's text content.
125      * @param text Node's text content.
126      */
127     public void setText(String text) {
128         this.text = text;
129     }
130 
131     /**
132      * Sets line number.
133      * @param lineNumber Line number.
134      */
135     public void setLineNumber(int lineNumber) {
136         this.lineNumber = lineNumber;
137     }
138 
139     /**
140      * Sets column number.
141      * @param columnNumber Column number.
142      */
143     public void setColumnNumber(int columnNumber) {
144         this.columnNumber = columnNumber;
145     }
146 
147     /**
148      * Sets array of child nodes.
149      * @param children Array of child nodes.
150      */
151     public void setChildren(DetailNode... children) {
152         this.children = Arrays.copyOf(children, children.length);
153     }
154 
155     /**
156      * Sets parent node.
157      * @param parent Parent node.
158      */
159     public void setParent(DetailNode parent) {
160         this.parent = parent;
161     }
162 
163     /**
164      * Sets node's index among parent's children.
165      * @param index Node's index among parent's children.
166      */
167     public void setIndex(int index) {
168         this.index = index;
169     }
170 
171     @Override
172     public String toString() {
173         return "JavadocNodeImpl["
174                 + "index=" + index
175                 + ", type=" + JavadocUtils.getTokenName(type)
176                 + ", text='" + text + '\''
177                 + ", lineNumber=" + lineNumber
178                 + ", columnNumber=" + columnNumber
179                 + ", children=" + Objects.hashCode(children)
180                 + ", parent=" + parent + ']';
181     }
182 }