<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;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

/**
 * <p>
 * Check name of the targeted item to validate
 * abbreviations(capital letters) length in it.
 * </p>
 * <p>
 * Option targets- it's array of type forbidden annotation's target.
 * </p>
 * <p>
 * Option allowedAbbreviations - list of abbreviations that
 * must be skipped for checking.
 * </p>
 * <p>
 * Option ignoreFinal allow to skip variables with final declarations.
 * </p>
 * <p>
 * Option ignoreStatic allow to skip variables with static declarations.
 * </p>
 * <p>
 * Option ignoreOverriddenMethod - Allows to
 * ignore methods tagged with '@Override' annotation
 * (that usually mean inherited name).
 * </p>
 * <p>
 * Option allowedAbbreviationLength indicates on the allowed amount of capital
 * letters in abbreviations in the classes, interfaces,
 * variables and methods names. Default value is '3'.
 * </p>
 * <p>
 * To configure to check variables definitions forbid skipping
 * variables with static declarations
 * and allow 'NUMBER' abbreviation.
 * </p>
 * <pre>
 * &lt;module name="AbbreviationAsWordInName"&gt;
 *     &lt;property name="targets" value="VARIABLE_DEF"/&gt;
 *     &lt;property name="ignoreStatic" value="false"/&gt;
 *     &lt;property name="allowedAbbreviations" value="NUMBER"/&gt;
 * &lt;/module&gt;
 * </pre>
 *
 * @author <NAME>, <NAME>, <NAME>
 */
public class AbbreviationAsWordInNameCheck extends Check
{

    /**
     * Warning message key.
     */
    public static final String MSG_KEY = "abbreviation.as.word";

    /**
     * The default value of "allowedAbbreviationLength" option.
     */
    private static final int DEFAULT_ALLOWED_ABBREVIATIONS_LENGTH = 3;

    /**
     * mAnnotationTargets is array of type forbidden annotation's target.
     */
    private int[] mTargets = new int[0];

    /**
     * Variable indicates on the allowed amount of capital letters in
     * abbreviations in the classes, interfaces, variables and methods names.
     */
    private int mAllowedAbbreviationLength =
            DEFAULT_ALLOWED_ABBREVIATIONS_LENGTH;

    /**
     * Set of allowed abbreviation to ignore in check.
     */
    private Set<String> mAllowedAbbreviations = new HashSet<String>();

    /** Allows to ignore variables with 'final' modifier. */
    private boolean mIgnoreFinal = true;

    /** Allows to ignore variables with 'static' modifier. */
    private boolean mIgnoreStatic = true;

    /** Allows to ignore methods with '@Override' annotation. */
    private boolean mIgnoreOverriddenMethods = true;

    /**
     * Sets ignore option for variables with 'final' modifier.
     * @param aIgnoreFinal
     *        Defines if ignore variables with 'final' modifier or not.
     */
    public void setIgnoreFinal(boolean aIgnoreFinal)
    {
        this.mIgnoreFinal = aIgnoreFinal;
    }

    /**
     * Sets ignore option for variables with 'static' modifier.
     * @param aIgnoreStatic
     *        Defines if ignore variables with 'static' modifier or not.
     */
    public void setIgnoreStatic(boolean aIgnoreStatic)
    {
        this.mIgnoreStatic = aIgnoreStatic;
    }

    /**
     * Sets ignore option for methods with "@Override" annotation.
     * @param aIgnoreOverriddenMethods
     *        Defines if ignore methods with "@Override" annotation or not.
     */
    public void setIgnoreOverriddenMethods(boolean aIgnoreOverriddenMethods)
    {
        this.mIgnoreOverriddenMethods = aIgnoreOverriddenMethods;
    }

    /**
     * Targets for a Check.
     * @param aTargets - array of type's names
     */
    public void setTargets(String[] aTargets)
    {
        if (aTargets != null) {
            mTargets = new int[aTargets.length];
            for (int i = 0; i < aTargets.length; i++) {
                mTargets[i] = TokenTypes.getTokenId(aTargets[i]);
            }
        }
    }

    /**
     * Allowed abbreviation length in names.
     * @param aAllowedAbbreviationLength
     *            amount of allowed capital letters in abbreviation.
     */
    public void setAllowedAbbreviationLength(int aAllowedAbbreviationLength)
    {
        mAllowedAbbreviationLength = aAllowedAbbreviationLength;
    }

    /**
     * Set a list of abbreviations that must be skipped for checking.
     * Abbreviations should be separated by comma, no spaces is allowed.
     * @param aAllowedAbbreviations
     *        an string of abbreviations that must be skipped from checking,
     *        each abbreviation separated by comma.
     */
    public void setAllowedAbbreviations(String aAllowedAbbreviations)
    {
        if (aAllowedAbbreviations != null) {
            mAllowedAbbreviations = new HashSet<String>(
                    Arrays.asList(aAllowedAbbreviations.split(",")));
        }
    }

    @Override
    public int[] getDefaultTokens()
    {
        return mTargets;
    }

    @Override
    public void visitToken(DetailAST aAst)
    {

        if (!isIgnoreSituation(aAst)) {

            final DetailAST nameAst = aAst.findFirstToken(TokenTypes.IDENT);
            final String typeName = nameAst.getText();

            final String abbr = getDisallowedAbbreviation(typeName);
            if (abbr != null) {
                log(nameAst.getLineNo(), MSG_KEY, mAllowedAbbreviationLength);
            }
        }
    }

    /**
     * Checks if it is an ignore situation.
     * @param aAst input DetailAST node.
     * @return true if it is an ignore situation found for given input DetailAST<commit_msg>AbbreviationAsAWordInName moved to naming package , documentation was rewritten, mTarget was removed from Check<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;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

/**
 * <p>
 * The Check validate abbreviations(consecutive capital letters) length in
 * identifier name. Please read more at 
 * <a href="http://google-styleguide.googlecode.com/svn/trunk/javaguide.html#s5.3-camel-case">
 * Google Style Guide</a> how to avoid long abbreviations in names 
 * </p>
 * <p>
 * Option <code>allowedAbbreviationLength</code> indicates on the allowed amount of capital
 * letters in abbreviations in the classes, interfaces,
 * variables and methods names. Default value is '3'.
 * </p>
 * <p>
 * Option <code>allowedAbbreviations</code> - list of abbreviations that
 * must be skipped for checking. Abbreviations should be separated by comma,
 * no spaces are allowed.
 * </p>
 * <p>
 * Option <code>ignoreFinal</code> allow to skip variables with <code>final<code> modifier.
 * Default value is <code>true</code>. 
 * </p>
 * <p>
 * Option <code>ignoreStatic</code> allow to skip variables with <code>static<code> modifier.
 * Default value is <code>true</code>.
 * </p>
 * <p>
 * Option <code>ignoreOverriddenMethod</code> - Allows to
 * ignore methods tagged with <code>@Override</code> annotation
 * (that usually mean inherited name). Default value is <code>true</code>.
 * </p>
 * Default configuration 
 * <pre>
 * &lt;module name="AbbreviationAsWordInName"&gt;
 * &lt;/module&gt;
 * </pre> * <p>
 * To configure to check variables and classes identifiers, do not ignore
 * variables with static modifier
 * and allow no abbreviations to use (camel case phrase).
 * </p>
 * <pre>
 * &lt;module name="AbbreviationAsWordInName"&gt;
 *     &lt;property name="targets" value="VARIABLE_DEF"/&gt;
 *     &lt;property name="ignoreStatic" value="false"/&gt;
 *     &lt;property name="allowedAbbreviations" value="1"/&gt;
 * &lt;/module&gt;
 * </pre>
 *
 * @author <NAME>, <NAME>, <NAME>
 */
public class AbbreviationAsWordInNameCheck extends Check
{

    /**
     * Warning message key.
     */
    public static final String MSG_KEY = "abbreviation.as.word";

    /**
     * The default value of "allowedAbbreviationLength" option.
     */
    private static final int DEFAULT_ALLOWED_ABBREVIATIONS_LENGTH = 3;

    /**
     * Variable indicates on the allowed amount of capital letters in
     * abbreviations in the classes, interfaces, variables and methods names.
     */
    private int mAllowedAbbreviationLength =
            DEFAULT_ALLOWED_ABBREVIATIONS_LENGTH;

    /**
     * Set of allowed abbreviation to ignore in check.
     */
    private Set<String> mAllowedAbbreviations = new HashSet<String>();

    /** Allows to ignore variables with 'final' modifier. */
    private boolean mIgnoreFinal = true;

    /** Allows to ignore variables with 'static' modifier. */
    private boolean mIgnoreStatic = true;

    /** Allows to ignore methods with '@Override' annotation. */
    private boolean mIgnoreOverriddenMethods = true;

    /**
     * Sets ignore option for variables with 'final' modifier.
     * @param aIgnoreFinal
     *        Defines if ignore variables with 'final' modifier or not.
     */
    public void setIgnoreFinal(boolean aIgnoreFinal)
    {
        this.mIgnoreFinal = aIgnoreFinal;
    }

    /**
     * Sets ignore option for variables with 'static' modifier.
     * @param aIgnoreStatic
     *        Defines if ignore variables with 'static' modifier or not.
     */
    public void setIgnoreStatic(boolean aIgnoreStatic)
    {
        this.mIgnoreStatic = aIgnoreStatic;
    }

    /**
     * Sets ignore option for methods with "@Override" annotation.
     * @param aIgnoreOverriddenMethods
     *        Defines if ignore methods with "@Override" annotation or not.
     */
    public void setIgnoreOverriddenMethods(boolean aIgnoreOverriddenMethods)
    {
        this.mIgnoreOverriddenMethods = aIgnoreOverriddenMethods;
    }

    /**
     * Allowed abbreviation length in names.
     * @param aAllowedAbbreviationLength
     *            amount of allowed capital letters in abbreviation.
     */
    public void setAllowedAbbreviationLength(int aAllowedAbbreviationLength)
    {
        mAllowedAbbreviationLength = aAllowedAbbreviationLength;
    }

    /**
     * Set a list of abbreviations that must be skipped for checking.
     * Abbreviations should be separated by comma, no spaces is allowed.
     * @param aAllowedAbbreviations
     *        an string of abbreviations that must be skipped from checking,
     *        each abbreviation separated by comma.
     */
    public void setAllowedAbbreviations(String aAllowedAbbreviations)
    {
        if (aAllowedAbbreviations != null) {
            mAllowedAbbreviations = new HashSet<String>(
                    Arrays.asList(aAllowedAbbreviations.split(",")));
        }
    }

    @Override
    public int[] getDefaultTokens()
    {
        return new int[] {
                TokenTypes.CLASS_DEF,
                TokenTypes.INTERFACE_DEF,
                TokenTypes.ENUM_DEF,
                TokenTypes.ANNOTATION_DEF,
                TokenTypes.ANNOTATION_FIELD_DEF,
                TokenTypes.PARAMETER_DEF,
                TokenTypes.VARIABLE_DEF,
                TokenTypes.METHOD_DEF
               };
    }

    @Override
    public void visitToken(DetailAST aAst)
    {

        if (!isIgnoreSituation(aAst)) {

            final DetailAST nameAst = aAst.findFirstToken(TokenTypes.IDENT);
            final String typeName = nameAst.getText();

            final String abbr = getDisallowedAbbreviation(typeName);
            if (abbr != null) {
                log(nameAst.getLineNo(), MSG_KEY, mAllowedAbbreviationLength);
            }
        }
    }

    /**
     * Checks if it is an ignore situation.
     * @param aAst input DetailAST node.
     * @return true if it is an ignore situation found for given input DetailAST
{'fraction_non_alphanumeric': 0.11245637775087244, 'fraction_numerical': 0.0040595399188092015, 'mean_word_length': 2.6359399274987054, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2501926', 'n_tokens_mistral': 4076, 'n_tokens_neox': 3737, 'n_words': 1273}