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