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.Collection;
040: import java.util.Iterator;
041:
042: /**
043: * Class defining a configuration option that requires a choice between
044: * mutually-exclusive possible values. Values are stored as Strings, though
045: * this could be extended to any type with a fairly simple refactoring.
046: * @version $Id: ForcedChoiceOption.java 4255 2007-08-28 19:17:37Z mgricken $
047: */
048: public class ForcedChoiceOption extends Option<String> {
049: private Collection<String> _choices;
050:
051: /**
052: * @param key The name of this option.
053: * @param def The default value of the option.
054: * @param choices A collection of all possible values of this Option, as Strings.
055: */
056: public ForcedChoiceOption(String key, String def,
057: Collection<String> choices) {
058: super (key, def);
059: _choices = choices;
060: }
061:
062: /**
063: * Checks whether the parameter String is a legal value for this option.
064: * The input String must be formatted exactly like the original, as defined
065: * by String.equals(String).
066: * @param s the value to check
067: * @return true if s is legal, false otherwise
068: */
069: public boolean isLegal(String s) {
070: return _choices.contains(s);
071: }
072:
073: /**
074: * Gets all legal values of this option.
075: * @return an Iterator containing the set of all Strings for which isLegal returns true.
076: */
077: public Iterator<String> getLegalValues() {
078: return _choices.iterator();
079: }
080:
081: /**
082: * Gets the number of legal values for this option.
083: * @return an int indicating the number of legal values.
084: */
085: public int getNumValues() {
086: return _choices.size();
087: }
088:
089: /**
090: * Parses an arbitrary String into an acceptable value for this option.
091: * @param s The String to be parsed.
092: * @return s, if s is a legal value of this option.
093: * @exception IllegalArgumentException if "s" is not one of the allowed values.
094: */
095: public String parse(String s) {
096: if (isLegal(s)) {
097: return s;
098: } else {
099: throw new OptionParseException(name, s,
100: "Value is not an acceptable choice for this option.");
101: }
102: }
103:
104: /**
105: * @param s The String to be formatted.
106: * @return "s", no actual formatting is performed.
107: */
108: public String format(String s) {
109: return s;
110: }
111: }
|