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