001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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.whitespace;
021
022import java.util.Arrays;
023import java.util.Locale;
024
025import com.puppycrawl.tools.checkstyle.StatelessCheck;
026import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
027import com.puppycrawl.tools.checkstyle.api.DetailAST;
028import com.puppycrawl.tools.checkstyle.api.TokenTypes;
029import com.puppycrawl.tools.checkstyle.utils.CodePointUtil;
030import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
031
032/**
033 * <div>
034 * Checks line wrapping with separators.
035 * </div>
036 *
037 * @since 5.8
038 */
039@StatelessCheck
040public class SeparatorWrapCheck extends AbstractCheck {
041
042    /**
043     * A key is pointing to the warning message text in "messages.properties" file.
044     */
045    public static final String MSG_LINE_PREVIOUS = "line.previous";
046
047    /**
048     * A key is pointing to the warning message text in "messages.properties" file.
049     */
050    public static final String MSG_LINE_NEW = "line.new";
051
052    /** Specify policy on how to wrap lines. */
053    private WrapOption option = WrapOption.EOL;
054
055    /**
056     * Creates a new {@code SeparatorWrapCheck} instance.
057     */
058    public SeparatorWrapCheck() {
059        // no code by default
060    }
061
062    /**
063     * Setter to specify policy on how to wrap lines.
064     *
065     * @param optionStr string to decode option from
066     * @throws IllegalArgumentException if unable to decode
067     * @since 5.8
068     */
069    public void setOption(String optionStr) {
070        option = WrapOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
071    }
072
073    @Override
074    public int[] getDefaultTokens() {
075        return new int[] {
076            TokenTypes.DOT,
077            TokenTypes.COMMA,
078        };
079    }
080
081    @Override
082    public int[] getAcceptableTokens() {
083        return new int[] {
084            TokenTypes.DOT,
085            TokenTypes.COMMA,
086            TokenTypes.SEMI,
087            TokenTypes.ELLIPSIS,
088            TokenTypes.AT,
089            TokenTypes.LPAREN,
090            TokenTypes.RPAREN,
091            TokenTypes.ARRAY_DECLARATOR,
092            TokenTypes.RBRACK,
093            TokenTypes.METHOD_REF,
094        };
095    }
096
097    @Override
098    public int[] getRequiredTokens() {
099        return CommonUtil.EMPTY_INT_ARRAY;
100    }
101
102    @Override
103    public void visitToken(DetailAST ast) {
104        final String text = ast.getText();
105        final int colNo = ast.getColumnNo();
106        final int lineNo = ast.getLineNo();
107        final int[] currentLine = getLineCodePoints(lineNo - 1);
108        final boolean isLineEmptyAfterToken = CodePointUtil.isBlank(
109                Arrays.copyOfRange(currentLine, colNo + text.length(), currentLine.length)
110        );
111        final boolean isLineEmptyBeforeToken = CodePointUtil.isBlank(
112                Arrays.copyOfRange(currentLine, 0, colNo)
113        );
114
115        if (option == WrapOption.NL
116                 && isLineEmptyAfterToken) {
117            log(ast, MSG_LINE_NEW, text);
118        }
119        else if (option == WrapOption.EOL
120                && isLineEmptyBeforeToken) {
121            log(ast, MSG_LINE_PREVIOUS, text);
122        }
123    }
124
125}