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