001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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.api;
021
022import java.util.ArrayList;
023import java.util.List;
024
025/**
026 * Represents a full identifier, including dots, with associated
027 * position information.
028 *
029 * <p>
030 * Identifiers such as {@code java.util.HashMap} are spread across
031 * multiple AST nodes in the syntax tree (three IDENT nodes, two DOT nodes).
032 * A FullIdent represents the whole String (excluding any intermediate
033 * whitespace), which is often easier to work with in Checks.
034 * </p>
035 *
036 * @author Oliver Burn
037 * @see TokenTypes#DOT
038 * @see TokenTypes#IDENT
039 **/
040public final class FullIdent {
041    /** The list holding subsequent elements of identifier. **/
042    private final List<String> elements = new ArrayList<>();
043    /** The line number. **/
044    private int lineNo;
045    /** The column number. **/
046    private int columnNo;
047
048    /** Hide default constructor. */
049    private FullIdent() {
050    }
051
052    /**
053     * Creates a new FullIdent starting from the specified node.
054     * @param ast the node to start from
055     * @return a {@code FullIdent} value
056     */
057    public static FullIdent createFullIdent(DetailAST ast) {
058        final FullIdent ident = new FullIdent();
059        extractFullIdent(ident, ast);
060        return ident;
061    }
062
063    /**
064     * Creates a new FullIdent starting from the child of the specified node.
065     * @param ast the parent node from where to start from
066     * @return a {@code FullIdent} value
067     */
068    public static FullIdent createFullIdentBelow(DetailAST ast) {
069        return createFullIdent(ast.getFirstChild());
070    }
071
072    /**
073     * Gets the text.
074     * @return the text
075     */
076    public String getText() {
077        return String.join("", elements);
078    }
079
080    /**
081     * Gets the line number.
082     * @return the line number
083     */
084    public int getLineNo() {
085        return lineNo;
086    }
087
088    /**
089     * Gets the column number.
090     * @return the column number
091     */
092    public int getColumnNo() {
093        return columnNo;
094    }
095
096    @Override
097    public String toString() {
098        return String.join("", elements) + "[" + lineNo + "x" + columnNo + "]";
099    }
100
101    /**
102     * Recursively extract a FullIdent.
103     *
104     * @param full the FullIdent to add to
105     * @param ast the node to recurse from
106     */
107    private static void extractFullIdent(FullIdent full, DetailAST ast) {
108        if (ast != null) {
109            if (ast.getType() == TokenTypes.DOT) {
110                extractFullIdent(full, ast.getFirstChild());
111                full.append(".");
112                extractFullIdent(
113                    full, ast.getFirstChild().getNextSibling());
114            }
115            else {
116                full.append(ast);
117            }
118        }
119    }
120
121    /**
122     * Append the specified text.
123     * @param text the text to append
124     */
125    private void append(String text) {
126        elements.add(text);
127    }
128
129    /**
130     * Append the specified token and also recalibrate the first line and
131     * column.
132     * @param ast the token to append
133     */
134    private void append(DetailAST ast) {
135        elements.add(ast.getText());
136        if (lineNo == 0) {
137            lineNo = ast.getLineNo();
138        }
139        else if (ast.getLineNo() > 0) {
140            lineNo = Math.min(lineNo, ast.getLineNo());
141        }
142        if (columnNo == 0) {
143            columnNo = ast.getColumnNo();
144        }
145        else if (ast.getColumnNo() > 0) {
146            columnNo = Math.min(columnNo, ast.getColumnNo());
147        }
148    }
149}