KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > AbstractOption


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 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 JavaDoc;
22 import java.io.ObjectStreamException JavaDoc;
23 import java.util.Map JavaDoc;
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 JavaDoc
34 {
35
36     /** the string representation of the option **/
37     private final String JavaDoc mStrRep;
38
39     /**
40      * Creates a new <code>AbstractOption</code> instance.
41      * @param aStrRep the string representation
42      */

43     protected AbstractOption(String JavaDoc aStrRep)
44     {
45         mStrRep = aStrRep.trim().toLowerCase();
46         final Map JavaDoc 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 JavaDoc 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 JavaDoc aStrRep)
64     {
65         final Map JavaDoc strToOpt = getStrToOpt();
66         return (AbstractOption) strToOpt.get(aStrRep.trim().toLowerCase());
67     }
68
69     /**
70      * {@inheritDoc}
71      **/

72     public String JavaDoc toString()
73     {
74         return mStrRep;
75     }
76
77     /**
78      * Ensures that we don't get multiple instances of one AbstractOption
79      * during deserialization. See Section 3.6 of the Java Object
80      * Serialization Specification for details.
81      *
82      * @return the serialization replacement object
83      * @throws ObjectStreamException if a deserialization error occurs
84      */

85     protected Object JavaDoc readResolve()
86         throws ObjectStreamException JavaDoc
87     {
88         return decode(mStrRep);
89     }
90 }
91
Popular Tags