KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > config > OptionParser


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.config;
35 import java.util.Hashtable JavaDoc;
36 /** The association of an OptionName with the ability to parse something to type T; the intended type
37  * parameterization is covariant: if U extends T, then OptionParser<U> extends OptionParser<T>.
38  */

39 public abstract class OptionParser<T> implements ParseStrategy<T> {
40     
41     /** The logical name of this configurable option (i.e. "indent.size") public because it's final,
42      * and a String is immutable.
43      */

44     public final String JavaDoc name;
45     private final T defaultValue;
46
47     /** An inner hashtable that maps DefaultOptionMaps to value T's. Part of the magic inner workings of this package.
48      */

49     final Hashtable JavaDoc<DefaultOptionMap,T> map = new Hashtable JavaDoc<DefaultOptionMap,T>();
50
51     /** Constructor that takes in a name
52      * @param name the name of this option (i.e. "indent.level");
53      */

54     public OptionParser(String JavaDoc name, T def) { this.name = name; defaultValue = def; }
55     
56     /** Accessor for name option
57      * @return name of this option (i.e. "indent.level")
58      */

59     public String JavaDoc getName() { return name; }
60
61     /** @return the default value */
62     public T getDefault() { return defaultValue; }
63
64     /** @return the default value as a string */
65     public abstract String JavaDoc getDefaultString();
66   
67     /** The ability to parse a string to an object of type T. All concrete versions of this class must override this
68      * method to provide some sort of parser implementation.
69      * @param value a String to parse
70      * @return the statically-typed representation of the string value.
71      */

72     public abstract T parse(String JavaDoc value);
73      
74     /* PACKAGE PRIVATE MAGIC STUFF
75      * This package-private magic stuff makes all of the config "magic" types work. Basically, it's achieved via a
76      * double-dispatch stunt, so that the type information is saved. */

77
78     abstract String JavaDoc getString(DefaultOptionMap om);
79     
80     /** Uses parse() and setOption() so that any changes in parsing will automatically be applied to setString(). */
81     T setString(DefaultOptionMap om, String JavaDoc val) { return setOption(om,parse(val)); }
82     
83     /** The accessor for the magic-typed hashtable stunt. */
84     T getOption(DefaultOptionMap om) { return map.get(om); }
85
86     /** The mutator for the magic-typed hashtable stunt. */
87     T setOption(DefaultOptionMap om, T val) { return map.put(om,val); }
88     
89     /** The destructor for a mapping in the magic-typed hashtable. */
90     T remove(DefaultOptionMap om) { return map.remove(om); }
91 }
92
93
94
95
96
97
98
99
100
101
102
103
Popular Tags