001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2016 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;
021
022import java.util.Locale;
023
024import org.apache.commons.beanutils.ConversionException;
025
026import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
027
028/**
029 * Abstract class for checks with a parameter named <tt>option</tt>, where the
030 * option is identified by a {@link Enum}. The logic to convert from a string
031 * representation to the {@link Enum} is to {@link String#trim()} the string
032 * and convert using {@link String#toUpperCase()} and then look up using
033 * {@link Enum#valueOf}.
034 * @deprecated Checkstyle will not support abstract checks anymore. Use
035 *             {@link AbstractCheck} instead.
036 * @author Oliver Burn
037 * @author Rick Giles
038 * @param <T> the type of the option.
039 * @noinspection AbstractClassNeverImplemented
040 */
041@Deprecated
042public abstract class AbstractOptionCheck<T extends Enum<T>>
043    extends AbstractCheck {
044
045    /** Semicolon literal. */
046    protected static final String SEMICOLON = ";";
047
048    /** Since I cannot get this by going <tt>T.class</tt>. */
049    private final Class<T> optionClass;
050    /** The policy to enforce. */
051    private T abstractOption;
052
053    /**
054     * Creates a new {@code AbstractOptionCheck} instance.
055     * @param literalDefault the default option.
056     * @param optionClass the class for the option. Required due to a quirk
057     *        in the Java language.
058     */
059    protected AbstractOptionCheck(T literalDefault, Class<T> optionClass) {
060        abstractOption = literalDefault;
061        this.optionClass = optionClass;
062    }
063
064    /**
065     * Set the option to enforce.
066     * @param optionStr string to decode option from
067     * @throws ConversionException if unable to decode
068     */
069    public void setOption(String optionStr) {
070        try {
071            abstractOption =
072                    Enum.valueOf(optionClass, optionStr.trim().toUpperCase(Locale.ENGLISH));
073        }
074        catch (IllegalArgumentException iae) {
075            throw new ConversionException("unable to parse " + optionStr, iae);
076        }
077    }
078
079    /**
080     * Gets AbstractOption set.
081     * @return the {@code AbstractOption} set
082     */
083    public T getAbstractOption() {
084        // WARNING!! Do not rename this method to getOption(). It breaks
085        // BeanUtils, which will silently not call setOption. Very annoying!
086        return abstractOption;
087    }
088}