Source code: com/puppycrawl/tools/checkstyle/checks/AbstractOption.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2003 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 java.io.Serializable;
22 import java.io.ObjectStreamException;
23 import java.util.Map;
24
25 /**
26 * Abstract class that represents options.
27 *
28 * @author Oliver Burn
29 * @author Rick Giles
30
31 */
32 public abstract class AbstractOption
33 implements Serializable
34 {
35
36 /** the string representation of the option **/
37 private final String mStrRep;
38
39 /**
40 * Creates a new <code>AbstractOption</code> instance.
41 * @param aStrRep the string representation
42 */
43 protected AbstractOption(String aStrRep)
44 {
45 mStrRep = aStrRep.trim().toLowerCase();
46 final Map strToOpt = getStrToOpt();
47 strToOpt.put(mStrRep, this);
48 }
49
50 /**
51 * Returns the map from string representations to options.
52 * @return <code>Map</code> from strings to options.
53 */
54 protected abstract Map getStrToOpt();
55
56 /**
57 * Returns the option specified by a string representation. If no
58 * option exists then null is returned.
59 * @param aStrRep the String representation to parse
60 * @return the <code>AbstractOption</code> value represented by
61 * aStrRep, or null if none exists.
62 */
63 public AbstractOption decode(String aStrRep)
64 {
65 final Map strToOpt = getStrToOpt();
66 return (AbstractOption) strToOpt.get(aStrRep.trim().toLowerCase());
67 }
68
69 /**
70 * Returns the string representation of this AbstractOption.
71 * @see java.lang.Object
72 **/
73 public String toString()
74 {
75 return mStrRep;
76 }
77
78 /**
79 * Ensures that we don't get multiple instances of one AbstractOption
80 * during deserialization. See Section 3.6 of the Java Object
81 * Serialization Specification for details.
82 *
83 * @return the serialization replacement object
84 * @throws ObjectStreamException if a deserialization error occurs
85 */
86 protected Object readResolve()
87 throws ObjectStreamException
88 {
89 return decode(mStrRep);
90 }
91 }