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.naming;
021
022import org.apache.commons.lang3.ArrayUtils;
023
024import com.puppycrawl.tools.checkstyle.api.TokenTypes;
025
026/**
027 * <p>
028 * Checks that type names conform to a format specified
029 * by the format property. The format is a
030 * {@link java.util.regex.Pattern regular expression} and defaults to
031 * <strong>^[A-Z][a-zA-Z0-9]*$</strong>.
032 * </p>
033 * <p>
034 * An example of how to configure the check is:
035 * </p>
036 * <pre>
037 * &lt;module name="TypeName"/&gt;
038 * </pre>
039 * <p>
040 * An example of how to configure the check for names that begin with
041 * a lower case letter, followed by letters, digits, and underscores is:
042 * </p>
043 * <pre>
044 * &lt;module name="TypeName"&gt;
045 *    &lt;property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/&gt;
046 * &lt;/module&gt;
047 * </pre>
048 * @author Oliver Burn
049 */
050public class TypeNameCheck
051    extends AbstractAccessControlNameCheck {
052
053    /**
054     * Default pattern for type name.
055     */
056    public static final String DEFAULT_PATTERN = "^[A-Z][a-zA-Z0-9]*$";
057
058    /**
059     * Creates a new {@code TypeNameCheck} instance.
060     */
061    public TypeNameCheck() {
062        super(DEFAULT_PATTERN);
063    }
064
065    @Override
066    public int[] getDefaultTokens() {
067        return getAcceptableTokens();
068    }
069
070    @Override
071    public int[] getAcceptableTokens() {
072        return new int[] {TokenTypes.CLASS_DEF,
073                          TokenTypes.INTERFACE_DEF,
074                          TokenTypes.ENUM_DEF,
075                          TokenTypes.ANNOTATION_DEF,
076        };
077    }
078
079    @Override
080    public int[] getRequiredTokens() {
081        return ArrayUtils.EMPTY_INT_ARRAY;
082    }
083}