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