001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2022 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.Arrays; 023import java.util.Objects; 024import java.util.Optional; 025 026import com.puppycrawl.tools.checkstyle.api.DetailNode; 027import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; 028 029/** 030 * Implementation of DetailNode interface that is mutable. 031 * 032 * 033 */ 034public class JavadocNodeImpl implements DetailNode { 035 036 /** 037 * Empty array of {@link DetailNode} type. 038 */ 039 public static final JavadocNodeImpl[] EMPTY_DETAIL_NODE_ARRAY = new JavadocNodeImpl[0]; 040 041 /** 042 * Node index among parent's children. 043 */ 044 private int index; 045 046 /** 047 * Node type. 048 */ 049 private int type; 050 051 /** 052 * Node's text content. 053 */ 054 private String text; 055 056 /** 057 * Line number. 058 */ 059 private int lineNumber; 060 061 /** 062 * Column number. 063 */ 064 private int columnNumber; 065 066 /** 067 * Array of child nodes. 068 */ 069 private DetailNode[] children; 070 071 /** 072 * Parent node. 073 */ 074 private DetailNode parent; 075 076 @Override 077 public int getType() { 078 return type; 079 } 080 081 @Override 082 public String getText() { 083 return text; 084 } 085 086 @Override 087 public int getLineNumber() { 088 return lineNumber; 089 } 090 091 @Override 092 public int getColumnNumber() { 093 return columnNumber; 094 } 095 096 @Override 097 public DetailNode[] getChildren() { 098 return Optional.ofNullable(children) 099 .map(array -> Arrays.copyOf(array, array.length)) 100 .orElse(EMPTY_DETAIL_NODE_ARRAY); 101 } 102 103 @Override 104 public DetailNode getParent() { 105 return parent; 106 } 107 108 @Override 109 public int getIndex() { 110 return index; 111 } 112 113 /** 114 * Sets node's type. 115 * 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 * 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 * 134 * @param lineNumber Line number. 135 */ 136 public void setLineNumber(int lineNumber) { 137 this.lineNumber = lineNumber; 138 } 139 140 /** 141 * Sets column number. 142 * 143 * @param columnNumber Column number. 144 */ 145 public void setColumnNumber(int columnNumber) { 146 this.columnNumber = columnNumber; 147 } 148 149 /** 150 * Sets array of child nodes. 151 * 152 * @param children Array of child nodes. 153 */ 154 public void setChildren(DetailNode... children) { 155 this.children = Arrays.copyOf(children, children.length); 156 } 157 158 /** 159 * Sets parent node. 160 * 161 * @param parent Parent node. 162 */ 163 public void setParent(DetailNode parent) { 164 this.parent = parent; 165 } 166 167 /** 168 * Sets node's index among parent's children. 169 * 170 * @param index Node's index among parent's children. 171 */ 172 public void setIndex(int index) { 173 this.index = index; 174 } 175 176 @Override 177 public String toString() { 178 return "JavadocNodeImpl[" 179 + "index=" + index 180 + ", type=" + JavadocUtil.getTokenName(type) 181 + ", text='" + text + '\'' 182 + ", lineNumber=" + lineNumber 183 + ", columnNumber=" + columnNumber 184 + ", children=" + Objects.hashCode(children) 185 + ", parent=" + parent + ']'; 186 } 187 188}