Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractOptionCheck |
|
| 1.6666666666666667;1.667 |
1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2014 Oliver Burn | |
4 | // | |
5 | // This library is free software; you can redistribute it and/or | |
6 | // modify it under the terms of the GNU Lesser General Public | |
7 | // License as published by the Free Software Foundation; either | |
8 | // version 2.1 of the License, or (at your option) any later version. | |
9 | // | |
10 | // This library is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | // Lesser General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU Lesser General Public | |
16 | // License along with this library; if not, write to the Free Software | |
17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | //////////////////////////////////////////////////////////////////////////////// | |
19 | package com.puppycrawl.tools.checkstyle.checks; | |
20 | ||
21 | import org.apache.commons.beanutils.ConversionException; | |
22 | ||
23 | import com.puppycrawl.tools.checkstyle.api.Check; | |
24 | ||
25 | /** | |
26 | * Abstract class for checks with a parameter named <tt>option</tt>, where the | |
27 | * option is identified by a {@link Enum}. The logic to convert from a string | |
28 | * representation to the {@link Enum} is to {@link String#trim()} the string | |
29 | * and convert using {@link String#toUpperCase()} and then look up using | |
30 | * {@link Enum#valueOf}. | |
31 | * @param <T> the type of the option. | |
32 | * @author Oliver Burn | |
33 | * @author Rick Giles | |
34 | */ | |
35 | public abstract class AbstractOptionCheck<T extends Enum<T>> | |
36 | extends Check | |
37 | { | |
38 | /** Since I cannot get this by going <tt>T.class</tt>. */ | |
39 | private final Class<T> mOptionClass; | |
40 | /** the policy to enforce */ | |
41 | private T mOption; | |
42 | ||
43 | /** | |
44 | * Creates a new <code>AbstractOptionCheck</code> instance. | |
45 | * @param aDefault the default option. | |
46 | * @param aOptionClass the class for the option. Required due to a quirk | |
47 | * in the Java language. | |
48 | */ | |
49 | public AbstractOptionCheck(T aDefault, Class<T> aOptionClass) | |
50 | 50 | { |
51 | 50 | mOption = aDefault; |
52 | 50 | mOptionClass = aOptionClass; |
53 | 50 | } |
54 | ||
55 | /** | |
56 | * Set the option to enforce. | |
57 | * @param aOption string to decode option from | |
58 | * @throws ConversionException if unable to decode | |
59 | */ | |
60 | public void setOption(String aOption) throws ConversionException | |
61 | { | |
62 | try { | |
63 | 26 | mOption = Enum.valueOf(mOptionClass, aOption.trim().toUpperCase()); |
64 | } | |
65 | 0 | catch (IllegalArgumentException iae) { |
66 | 0 | throw new ConversionException("unable to parse " + aOption, iae); |
67 | 26 | } |
68 | 26 | } |
69 | ||
70 | /** | |
71 | * @return the <code>AbstractOption</code> set | |
72 | */ | |
73 | public T getAbstractOption() | |
74 | { | |
75 | // WARNING!! Do not rename this method to getOption(). It breaks | |
76 | // BeanUtils, which will silently not call setOption. Very annoying! | |
77 | 1240 | return mOption; |
78 | } | |
79 | } |