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.indentation; 021 022import java.util.SortedMap; 023import java.util.TreeMap; 024 025import com.puppycrawl.tools.checkstyle.api.DetailAST; 026import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 027 028/** 029 * Represents a set of abstract syntax tree. 030 * 031 */ 032public class DetailAstSet { 033 034 /** 035 * The instance of {@code IndentationCheck} used by this class. 036 */ 037 private final IndentationCheck indentCheck; 038 039 /** 040 * Maps line numbers to their ast. 041 */ 042 private final SortedMap<Integer, DetailAST> astLines = new TreeMap<>(); 043 044 /** 045 * Construct an instance of this class with {@code IndentationCheck} parameters. 046 * 047 * @param indentCheck IndentationCheck parameters 048 */ 049 public DetailAstSet(IndentationCheck indentCheck) { 050 this.indentCheck = indentCheck; 051 } 052 053 /** 054 * Add ast to the set of ast. 055 * 056 * @param ast the ast to add 057 */ 058 public void addAst(DetailAST ast) { 059 addLineWithAst(ast.getLineNo(), ast); 060 } 061 062 /** 063 * Map ast with their line number. 064 * 065 * @param lineNo line number of ast to add 066 * @param ast ast to add 067 */ 068 private void addLineWithAst(int lineNo, DetailAST ast) { 069 astLines.put(lineNo, ast); 070 } 071 072 /** 073 * Get starting column number for the ast. 074 * 075 * @param lineNum the line number as key 076 * @return start column for ast 077 */ 078 public Integer getStartColumn(int lineNum) { 079 Integer startColumn = null; 080 final DetailAST ast = getAst(lineNum); 081 082 if (ast != null) { 083 startColumn = expandedTabsColumnNo(ast); 084 } 085 086 return startColumn; 087 } 088 089 /** 090 * Check if this set of ast is empty. 091 * 092 * @return true if empty, false otherwise 093 */ 094 public boolean isEmpty() { 095 return astLines.isEmpty(); 096 } 097 098 /** 099 * The first line in set of ast. 100 * 101 * @return first line in set of ast. 102 */ 103 public DetailAST firstLine() { 104 return astLines.get(astLines.firstKey()); 105 } 106 107 /** 108 * Get the ast corresponding to line number. 109 * 110 * @param lineNum line number of ast. 111 * @return ast with their corresponding line number or null if no mapping is present 112 */ 113 public DetailAST getAst(int lineNum) { 114 return astLines.get(lineNum); 115 } 116 117 /** 118 * Get the line number of the last line. 119 * 120 * @return the line number of the last line 121 */ 122 public Integer lastLine() { 123 return astLines.lastKey(); 124 } 125 126 /** 127 * Get the column number for the start of a given expression, expanding 128 * tabs out into spaces in the process. 129 * 130 * @param ast the expression to find the start of 131 * 132 * @return the column number for the start of the expression 133 */ 134 protected final int expandedTabsColumnNo(DetailAST ast) { 135 final String line = 136 indentCheck.getLine(ast.getLineNo() - 1); 137 138 return CommonUtil.lengthExpandedTabs(line, ast.getColumnNo(), 139 indentCheck.getIndentationTabWidth()); 140 } 141 142}