<commit_before>////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2014  <NAME>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.javadoc;

import java.util.regex.Pattern;

import com.google.common.base.CharMatcher;
import com.puppycrawl.tools.checkstyle.api.DetailNode;
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
import com.puppycrawl.tools.checkstyle.api.Utils;

/**
 * <p>
 * Checks that <a href="
 * http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html#firstsentence">
 * Javadoc summary sentence</a> does not contain phrases that are not recommended to use.
 * By default Check validate that first sentence is not empty:</p><br/>
 * <pre>
 * &lt;module name=&quot;SummaryJavadocCheck&quot;/&gt;
 * </pre>
 * <p>
 * To ensure that summary do not contain phrase like "This method returns" , use following config:
 * <p>
 * <pre>
 * &lt;module name=&quot;SummaryJavadocCheck&quot;&gt;
 *     &lt;property name=&quot;forbiddenSummaryFragments&quot;
 *     value=&quot;^This method returns.*&quot;/&gt;
 * &lt;/module&gt;
 * </pre>
 *
 * @author max
 *
 */
public class SummaryJavadocCheck extends AbstractJavadocCheck
{

    /**
     * Some javadoc.
     */
    private Pattern mForbiddenSummaryFragments = Utils.createPattern("^$");

    /**
     * Some javadoc.
     * @param aPattern Some javadoc.
     */
    public void setForbiddenSummaryFragments(String aPattern)
    {
        mForbiddenSummaryFragments = Utils.createPattern(aPattern);
    }

    @Override
    public int[] getDefaultJavadocTokens()
    {
        return new int[] {
            JavadocTokenTypes.JAVADOC,
        };
    }

    @Override
    public void visitJavadocToken(DetailNode aAst)
    {
        String firstSentence = getFirstSentence(aAst);
        final int endOfSentence = firstSentence.lastIndexOf(".");
        if (endOfSentence == -1) {
            log(aAst.getLineNumber(), "summary.first.sentence");
        }
        else {
            firstSentence = firstSentence.substring(0, endOfSentence);
            if (containsForbiddenFragment(firstSentence)) {
                log(aAst.getLineNumber(), "summary.javaDoc");
            }
        }
    }

    /**
     * Some javadoc.
     * @param aAst Some javadoc.
     * @return Some javadoc.
     */
    private String getFirstSentence(DetailNode aAst)
    {
        final StringBuilder result = new StringBuilder();
        for (DetailNode child : aAst.getChildren()) {
            if (child.getType() != JavadocTokenTypes.JAVADOC_INLINE_TAG
                && child.getText().contains(". "))
            {
                result.append(getCharsTillDot(child));
                break;
            }
            else {
                result.append(child.getText());
            }
        }
        return result.toString();
    }

    /**
     * Some javadoc.
     * @param aTextNode Some javadoc.
     * @return Some javadoc.
     */
    private String getCharsTillDot(DetailNode aTextNode)
    {
        final StringBuilder result = new StringBuilder();
        for (DetailNode child : aTextNode.getChildren()) {
            result.append(child.getText());
            if (".".equals(child.getText())
                && JavadocUtils.getNextSibling(child).getType() == JavadocTokenTypes.WS)
            {
                break;
            }
        }
        return result.toString();
    }

    /**
     * Some javadoc.
     * @param aFirstSentence Some javadoc.
     * @return Some javadoc.
     */
    private boolean containsForbiddenFragment(String aFirstSentence)
    {
        // This regexp is used to convert multiline javadoc to single line without stars.
        String javadocText = aFirstSentence.replaceAll("\n[ ]+(\\*)|^[ ]+(\\*)", " ");
        javadocText = CharMatcher.WHITESPACE.trimAndCollapseFrom(javadocText, ' ');
        return mForbiddenSummaryFragments.matcher(javadocText).find();
    }<commit_msg>Fixed javadoc in SummaryJavadocCheck  #365<commit_after>////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2014  <NAME>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.javadoc;

import java.util.regex.Pattern;

import com.google.common.base.CharMatcher;
import com.puppycrawl.tools.checkstyle.api.DetailNode;
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
import com.puppycrawl.tools.checkstyle.api.Utils;

/**
 * <p>
 * Checks that <a href="
 * http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html#firstsentence">
 * Javadoc summary sentence</a> does not contain phrases that are not recommended to use.
 * By default Check validate that first sentence is not empty:</p><br/>
 * <pre>
 * &lt;module name=&quot;SummaryJavadocCheck&quot;/&gt;
 * </pre>
 * <p>
 * To ensure that summary do not contain phrase like "This method returns" , use following config:
 * <p>
 * <pre>
 * &lt;module name=&quot;SummaryJavadocCheck&quot;&gt;
 *     &lt;property name=&quot;forbiddenSummaryFragments&quot;
 *     value=&quot;^This method returns.*&quot;/&gt;
 * &lt;/module&gt;
 * </pre>
 *
 * @author max
 *
 */
public class SummaryJavadocCheck extends AbstractJavadocCheck
{

    /**
     * Regular expression for forbidden summary fragments.
     */
    private Pattern mForbiddenSummaryFragments = Utils.createPattern("^$");

    /**
     * Sets custom value of regular expression for forbidden summary fragments.
     * @param aPattern user's value.
     */
    public void setForbiddenSummaryFragments(String aPattern)
    {
        mForbiddenSummaryFragments = Utils.createPattern(aPattern);
    }

    @Override
    public int[] getDefaultJavadocTokens()
    {
        return new int[] {
            JavadocTokenTypes.JAVADOC,
        };
    }

    @Override
    public void visitJavadocToken(DetailNode aAst)
    {
        String firstSentence = getFirstSentence(aAst);
        final int endOfSentence = firstSentence.lastIndexOf(".");
        if (endOfSentence == -1) {
            log(aAst.getLineNumber(), "summary.first.sentence");
        }
        else {
            firstSentence = firstSentence.substring(0, endOfSentence);
            if (containsForbiddenFragment(firstSentence)) {
                log(aAst.getLineNumber(), "summary.javaDoc");
            }
        }
    }

    /**
     * Finds and returns first sentence.
     * @param aAst Javadoc root node.
     * @return first sentence.
     */
    private String getFirstSentence(DetailNode aAst)
    {
        final StringBuilder result = new StringBuilder();
        for (DetailNode child : aAst.getChildren()) {
            if (child.getType() != JavadocTokenTypes.JAVADOC_INLINE_TAG
                && child.getText().contains(". "))
            {
                result.append(getCharsTillDot(child));
                break;
            }
            else {
                result.append(child.getText());
            }
        }
        return result.toString();
    }

    /**
     * Finds and returns chars till first dot.
     * @param aTextNode node with javadoc text.
     * @return String with chars till first dot.
     */
    private String getCharsTillDot(DetailNode aTextNode)
    {
        final StringBuilder result = new StringBuilder();
        for (DetailNode child : aTextNode.getChildren()) {
            result.append(child.getText());
            if (".".equals(child.getText())
                && JavadocUtils.getNextSibling(child).getType() == JavadocTokenTypes.WS)
            {
                break;
            }
        }
        return result.toString();
    }

    /**
     * Tests if first sentence contains forbidden summary fragment.
     * @param aFirstSentence String with first sentence.
     * @return true, if first sentence contains forbidden summary fragment.
     */
    private boolean containsForbiddenFragment(String aFirstSentence)
    {
        // This regexp is used to convert multiline javadoc to single line without stars.
        String javadocText = aFirstSentence.replaceAll("\n[ ]+(\\*)|^[ ]+(\\*)", " ");
        javadocText = CharMatcher.WHITESPACE.trimAndCollapseFrom(javadocText, ' ');
        return mForbiddenSummaryFragments.matcher(javadocText).find();
    }
{'fraction_non_alphanumeric': 0.13641353158367428, 'fraction_numerical': 0.006685959485081329, 'mean_word_length': 2.350718823136075, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6724497', 'n_tokens_mistral': 2913, 'n_tokens_neox': 2724, 'n_words': 761}