01: ////////////////////////////////////////////////////////////////////////////////
02: // checkstyle: Checks Java source code for adherence to a set of rules.
03: // Copyright (C) 2001-2007 Oliver Burn
04: //
05: // This library is free software; you can redistribute it and/or
06: // modify it under the terms of the GNU Lesser General Public
07: // License as published by the Free Software Foundation; either
08: // version 2.1 of the License, or (at your option) any later version.
09: //
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 implements Serializable {
33:
34: /** the string representation of the option **/
35: private final String mStrRep;
36:
37: /**
38: * Creates a new <code>AbstractOption</code> instance.
39: * @param aStrRep the string representation
40: */
41: protected AbstractOption(String aStrRep) {
42: mStrRep = aStrRep.trim().toLowerCase();
43: final Map strToOpt = getStrToOpt();
44: strToOpt.put(mStrRep, this );
45: }
46:
47: /**
48: * Returns the map from string representations to options.
49: * @return <code>Map</code> from strings to options.
50: */
51: protected abstract Map getStrToOpt();
52:
53: /**
54: * Returns the option specified by a string representation. If no
55: * option exists then null is returned.
56: * @param aStrRep the String representation to parse
57: * @return the <code>AbstractOption</code> value represented by
58: * aStrRep, or null if none exists.
59: */
60: public AbstractOption decode(String aStrRep) {
61: final Map strToOpt = getStrToOpt();
62: return (AbstractOption) strToOpt.get(aStrRep.trim()
63: .toLowerCase());
64: }
65:
66: /**
67: * {@inheritDoc}
68: **/
69: public String toString() {
70: return mStrRep;
71: }
72:
73: /**
74: * Ensures that we don't get multiple instances of one AbstractOption
75: * during deserialization. See Section 3.6 of the Java Object
76: * Serialization Specification for details.
77: *
78: * @return the serialization replacement object
79: * @throws ObjectStreamException if a deserialization error occurs
80: */
81: protected Object readResolve() throws ObjectStreamException {
82: return decode(mStrRep);
83: }
84: }
|