|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ForcedChoiceOption.java | 75% | 80% | 77.8% | 78.6% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.config; | |
| 38 | import java.util.Arrays; | |
| 39 | import java.util.Collection; | |
| 40 | import java.util.Iterator; | |
| 41 | ||
| 42 | ||
| 43 | /** | |
| 44 | * Class defining a configuration option that requires a choice between | |
| 45 | * mutually-exclusive possible values. Values are stored as Strings, though | |
| 46 | * this could be extended to any type with a fairly simple refactoring. | |
| 47 | * @version $Id: ForcedChoiceOption.java 5246 2010-05-07 19:10:44Z mgricken $ | |
| 48 | */ | |
| 49 | public class ForcedChoiceOption extends Option<String> | |
| 50 | { | |
| 51 | private Collection<String> _choices; | |
| 52 | private Collection<String> _deprecated; // these will automatically be changed to the default | |
| 53 | ||
| 54 | /** @param key The name of this option. | |
| 55 | * @param def The default value of the option. | |
| 56 | * @param choices A collection of all possible values of this Option, as Strings. | |
| 57 | */ | |
| 58 | 1217 | public ForcedChoiceOption(String key, String def, Collection<String> choices) { |
| 59 | 1217 | this(key, def, choices, Arrays.asList(new String[0])); |
| 60 | } | |
| 61 | ||
| 62 | /** @param key The name of this option. | |
| 63 | * @param def The default value of the option. | |
| 64 | * @param choices A collection of all possible values of this Option, as Strings. | |
| 65 | * @param deprecated A collection of values that are deprecated and that should be changed to the default. | |
| 66 | */ | |
| 67 | 1451 | public ForcedChoiceOption(String key, String def, Collection<String> choices, |
| 68 | Collection<String> deprecated) { | |
| 69 | 1451 | super(key,def); |
| 70 | 1451 | _choices = choices; |
| 71 | 1451 | _deprecated = deprecated; |
| 72 | } | |
| 73 | ||
| 74 | /** Checks whether the parameter String is a legal value for this option. | |
| 75 | * The input String must be formatted exactly like the original, as defined | |
| 76 | * by String.equals(String). | |
| 77 | * @param s the value to check | |
| 78 | * @return true if s is legal, false otherwise | |
| 79 | */ | |
| 80 | 14104 | public boolean isLegal(String s) { |
| 81 | 14104 | return _choices.contains(s); |
| 82 | } | |
| 83 | ||
| 84 | /** Checks whether the parameter String is a deprecated value for this option. | |
| 85 | * The input String must be formatted exactly like the original, as defined | |
| 86 | * by String.equals(String). | |
| 87 | * @param s the value to check | |
| 88 | * @return true if s is deprecated, false otherwise | |
| 89 | */ | |
| 90 | 3 | public boolean isDeprecated(String s) { |
| 91 | 3 | return _deprecated.contains(s); |
| 92 | } | |
| 93 | ||
| 94 | /** Gets all legal values of this option. | |
| 95 | * @return an Iterator containing the set of all Strings for which isLegal returns true. | |
| 96 | */ | |
| 97 | 459 | public Iterator<String> getLegalValues() { |
| 98 | 459 | return _choices.iterator(); |
| 99 | } | |
| 100 | ||
| 101 | /** Gets all deprecated values of this option. | |
| 102 | * @return an Iterator containing the set of all Strings for which isDeprecated returns true. | |
| 103 | */ | |
| 104 | 0 | public Iterator<String> getDeprecatedValues() { |
| 105 | 0 | return _deprecated.iterator(); |
| 106 | } | |
| 107 | ||
| 108 | /** Gets the number of legal values for this option. | |
| 109 | * @return an int indicating the number of legal values. | |
| 110 | */ | |
| 111 | 0 | public int getNumValues() { |
| 112 | 0 | return _choices.size(); |
| 113 | } | |
| 114 | ||
| 115 | /** Parses an arbitrary String into an acceptable value for this option. | |
| 116 | * @param s The String to be parsed. | |
| 117 | * @return s, if s is a legal value of this option. | |
| 118 | * @exception IllegalArgumentException if "s" is not one of the allowed values. | |
| 119 | */ | |
| 120 | 14104 | public String parse(String s) |
| 121 | { | |
| 122 | 14104 | if (isLegal(s)) { |
| 123 | 14101 | return s; |
| 124 | } | |
| 125 | 3 | else if (isDeprecated(s)) { |
| 126 | 0 | return defaultValue; |
| 127 | } | |
| 128 | else { | |
| 129 | 3 | throw new OptionParseException(name, s, "Value is not an acceptable choice for this option."); |
| 130 | } | |
| 131 | } | |
| 132 | ||
| 133 | /** @param s The String to be formatted. | |
| 134 | * @return "s", no actual formatting is performed. | |
| 135 | */ | |
| 136 | 1406 | public String format(String s) { |
| 137 | 1406 | return s; |
| 138 | } | |
| 139 | } |
|
||||||||||