001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 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 com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.TokenTypes;
024
025/**
026 * Handler for array initialization blocks.
027 *
028 * @author jrichard
029 */
030public class ArrayInitHandler extends BlockParentHandler {
031    /**
032     * Construct an instance of this handler with the given indentation check,
033     * abstract syntax tree, and parent handler.
034     *
035     * @param indentCheck   the indentation check
036     * @param ast           the abstract syntax tree
037     * @param parent        the parent handler
038     */
039    public ArrayInitHandler(IndentationCheck indentCheck,
040        DetailAST ast, AbstractExpressionHandler parent) {
041        super(indentCheck, "array initialization", ast, parent);
042    }
043
044    @Override
045    protected IndentLevel getLevelImpl() {
046        final DetailAST parentAST = getMainAst().getParent();
047        final int type = parentAST.getType();
048        if (type == TokenTypes.LITERAL_NEW || type == TokenTypes.ASSIGN) {
049            // note: assumes new or assignment is line to align with
050            return new IndentLevel(getLineStart(parentAST));
051        }
052        else {
053            // at this point getParent() is instance of BlockParentHandler
054            return ((BlockParentHandler) getParent()).getChildrenExpectedLevel();
055        }
056    }
057
058    @Override
059    protected DetailAST getTopLevelAst() {
060        return null;
061    }
062
063    @Override
064    protected DetailAST getLCurly() {
065        return getMainAst();
066    }
067
068    @Override
069    protected IndentLevel curlyLevel() {
070        final IndentLevel level = new IndentLevel(getLevel(), getBraceAdjustment());
071        level.addAcceptedIndent(level.getLastIndentLevel() + getLineWrappingIndentation());
072        return level;
073    }
074
075    @Override
076    protected DetailAST getRCurly() {
077        return getMainAst().findFirstToken(TokenTypes.RCURLY);
078    }
079
080    @Override
081    protected boolean shouldStartWithRCurly() {
082        return false;
083    }
084
085    @Override
086    protected boolean canChildrenBeNested() {
087        return true;
088    }
089
090    @Override
091    protected DetailAST getListChild() {
092        return getMainAst();
093    }
094
095    @Override
096    protected IndentLevel getChildrenExpectedLevel() {
097        final IndentLevel expectedIndent =
098            new IndentLevel(getLevel(), getIndentCheck().getArrayInitIndent(),
099                    getIndentCheck().getLineWrappingIndentation());
100
101        final int firstLine = getFirstLine(Integer.MAX_VALUE, getListChild());
102        final int lcurlyPos = expandedTabsColumnNo(getLCurly());
103        final int firstChildPos =
104            getNextFirstNonBlankOnLineAfter(firstLine, lcurlyPos);
105        if (firstChildPos >= 0) {
106            expectedIndent.addAcceptedIndent(firstChildPos);
107            expectedIndent.addAcceptedIndent(lcurlyPos + getLineWrappingIndentation());
108        }
109        return expectedIndent;
110    }
111
112    /**
113     * @param lineNo   number of line on which we search
114     * @param columnNo number of column after which we search
115     *
116     * @return column number of first non-blank char after
117     *         specified column on specified line or -1 if
118     *         such char doesn't exist.
119     */
120    private int getNextFirstNonBlankOnLineAfter(int lineNo, int columnNo) {
121        int realColumnNo = columnNo + 1;
122        final String line = getIndentCheck().getLines()[lineNo - 1];
123        final int lineLength = line.length();
124        while (realColumnNo < lineLength
125               && Character.isWhitespace(line.charAt(realColumnNo))) {
126            realColumnNo++;
127        }
128
129        if (realColumnNo == lineLength) {
130            return -1;
131        }
132        else {
133            return realColumnNo;
134        }
135    }
136
137    /**
138     * A shortcut for {@code IndentationCheck} property.
139     * @return value of lineWrappingIndentation property
140     *         of {@code IndentationCheck}
141     */
142    private int getLineWrappingIndentation() {
143        return getIndentCheck().getLineWrappingIndentation();
144    }
145}