001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.naming;
020
021import com.puppycrawl.tools.checkstyle.api.DetailAST;
022import com.puppycrawl.tools.checkstyle.api.TokenTypes;
023
024/**
025 * <p>
026 * Abstract class for checking if a class/method type parameter's name
027 * conforms to a format specified by the format property.
028 * </p>
029 *
030 * <p>This class extends {@link AbstractNameCheck}</p>
031 *
032 * @author Travis Schneeberger
033 * @version 1.0
034 */
035public abstract class AbstractTypeParameterNameCheck
036    extends AbstractNameCheck
037{
038    /** the location of the type parameter **/
039    private int location;
040
041    /**
042     * Creates a new <code>AbstractTypeParameterNameCheck</code> instance.
043     * @param format format to check with
044     */
045    public AbstractTypeParameterNameCheck(String format)
046    {
047        super(format);
048    }
049
050    @Override
051    public final int[] getDefaultTokens()
052    {
053        return new int[] {
054            TokenTypes.TYPE_PARAMETER,
055        };
056    }
057
058    @Override
059    public final void init()
060    {
061        this.location = getLocation();
062
063        assert (this.location == TokenTypes.CLASS_DEF)
064            || (this.location == TokenTypes.METHOD_DEF)
065            || (this.location == TokenTypes.INTERFACE_DEF);
066    }
067
068    @Override
069    protected final boolean mustCheckName(DetailAST ast)
070    {
071        DetailAST location =
072            ast.getParent().getParent();
073
074        if (location.getType() == TokenTypes.MODIFIERS) {
075            location = location.getParent();
076        }
077
078        return location.getType() == this.location;
079    }
080
081    /**
082     * This method must be overriden to specify the
083     * location of the type parameter to check.
084     *
085     * @return <code> TokenTypes.CLASS_DEF </code>
086     * or <code> TokenTypes.METHOD_DEF </code>
087     */
088    protected abstract int getLocation();
089}