001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.config;
038:
039: import java.util.Hashtable;
040:
041: /** The association of an OptionName with the ability to parse something to type T; the intended type
042: * parameterization is covariant: if U extends T, then OptionParser<U> extends OptionParser<T>.
043: */
044: public abstract class OptionParser<T> implements ParseStrategy<T> {
045:
046: /** The logical name of this configurable option (i.e. "indent.size") public because it's final,
047: * and a String is immutable.
048: */
049: public final String name;
050: private final T defaultValue;
051:
052: /** An inner hashtable that maps DefaultOptionMaps to value T's. Part of the magic inner workings of this package.
053: */
054: final Hashtable<DefaultOptionMap, T> map = new Hashtable<DefaultOptionMap, T>();
055:
056: /** Constructor that takes in a name
057: * @param name the name of this option (i.e. "indent.level");
058: */
059: public OptionParser(String name, T def) {
060: this .name = name;
061: defaultValue = def;
062: }
063:
064: /** Accessor for name option
065: * @return name of this option (i.e. "indent.level")
066: */
067: public String getName() {
068: return name;
069: }
070:
071: /** @return the default value */
072: public T getDefault() {
073: return defaultValue;
074: }
075:
076: /** @return the default value as a string */
077: public abstract String getDefaultString();
078:
079: /** The ability to parse a string to an object of type T. All concrete versions of this class must override this
080: * method to provide some sort of parser implementation.
081: * @param value a String to parse
082: * @return the statically-typed representation of the string value.
083: */
084: public abstract T parse(String value);
085:
086: /* PACKAGE PRIVATE MAGIC STUFF
087: * This package-private magic stuff makes all of the config "magic" types work. Basically, it's achieved via a
088: * double-dispatch stunt, so that the type information is saved. */
089:
090: abstract String getString(DefaultOptionMap om);
091:
092: /** Uses parse() and setOption() so that any changes in parsing will automatically be applied to setString(). */
093: T setString(DefaultOptionMap om, String val) {
094: return setOption(om, parse(val));
095: }
096:
097: /** The accessor for the magic-typed hashtable stunt. */
098: T getOption(DefaultOptionMap om) {
099: return map.get(om);
100: }
101:
102: /** The mutator for the magic-typed hashtable stunt. */
103: T setOption(DefaultOptionMap om, T val) {
104: return map.put(om, val);
105: }
106:
107: /** The destructor for a mapping in the magic-typed hashtable. */
108: T remove(DefaultOptionMap om) {
109: return map.remove(om);
110: }
111: }
|