|
| 1 | +/* |
| 2 | + * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product |
| 5 | + * that is described in this document. In particular, and without limitation, these intellectual property |
| 6 | + * rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or |
| 7 | + * more additional patents or pending patent applications in the U.S. and in other countries. |
| 8 | + * |
| 9 | + * U.S. Government Rights - Commercial software. Government users are subject to the Sun |
| 10 | + * Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its |
| 11 | + * supplements. |
| 12 | + * |
| 13 | + * Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or |
| 14 | + * registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks |
| 15 | + * are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the |
| 16 | + * U.S. and other countries. |
| 17 | + * |
| 18 | + * UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open |
| 19 | + * Company, Ltd. |
| 20 | + */ |
| 21 | +package com.sun.max.program.option; |
| 22 | + |
| 23 | +import com.sun.max.*; |
| 24 | + |
| 25 | +/** |
| 26 | + * The {@code Option} class represents a command-line or other configuration |
| 27 | + * option with a particular name, type, and description. |
| 28 | + * |
| 29 | + * @author Ben L. Titzer |
| 30 | + */ |
| 31 | +public class Option<T> implements Cloneable { |
| 32 | + |
| 33 | + /** |
| 34 | + * The {@code Option.Type} class represents a type for an option. This class |
| 35 | + * implements method for parsing and unparsing values from strings. |
| 36 | + */ |
| 37 | + public abstract static class Type<T> { |
| 38 | + protected final String typeName; |
| 39 | + public final Class<T> type; |
| 40 | + |
| 41 | + protected Type(Class<T> type, String typeName) { |
| 42 | + this.typeName = typeName; |
| 43 | + this.type = type; |
| 44 | + } |
| 45 | + |
| 46 | + public String getTypeName() { |
| 47 | + return typeName; |
| 48 | + } |
| 49 | + |
| 50 | + public String unparseValue(T value) { |
| 51 | + return String.valueOf(value); |
| 52 | + } |
| 53 | + |
| 54 | + public abstract T parseValue(String string) throws Option.Error; |
| 55 | + |
| 56 | + public abstract String getValueFormat(); |
| 57 | + |
| 58 | + public Option<T> cast(Option option) { |
| 59 | + return Utils.cast(option); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static class Error extends java.lang.Error { |
| 64 | + |
| 65 | + public Error(String message) { |
| 66 | + super(message); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + protected final String name; |
| 71 | + protected T defaultValue; |
| 72 | + protected final Type<T> type; |
| 73 | + protected final String help; |
| 74 | + protected T value; |
| 75 | + |
| 76 | + /** |
| 77 | + * The constructor for the {@code Option} class creates constructs a new |
| 78 | + * option with the specified parameters. |
| 79 | + * |
| 80 | + * @param name the name of the option as a string |
| 81 | + * @param defaultValue the default value of the option |
| 82 | + * @param type the type of the option, which is used for parsing and unparsing values |
| 83 | + * @param help a help description which is usually used to generate a formatted |
| 84 | + * help output |
| 85 | + */ |
| 86 | + public Option(String name, T defaultValue, Type<T> type, String help) { |
| 87 | + this.defaultValue = defaultValue; |
| 88 | + this.name = name; |
| 89 | + this.type = type; |
| 90 | + this.help = help; |
| 91 | + value = null; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * The {@code getName()} method returns the name of this option as a string. |
| 96 | + * |
| 97 | + * @return the name of this option |
| 98 | + */ |
| 99 | + public String getName() { |
| 100 | + return name; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Sets the default value for this option, |
| 105 | + * which is the value that the option retains if no assignment is made. |
| 106 | + * |
| 107 | + * @param val the default value of the option |
| 108 | + */ |
| 109 | + public void setDefaultValue(T val) { |
| 110 | + defaultValue = val; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * The {@code getDefaultValue()} method returns the default value for this option, |
| 115 | + * which is the value that the option retains if no assignment is made. |
| 116 | + * |
| 117 | + * @return the default value of the option |
| 118 | + */ |
| 119 | + public T getDefaultValue() { |
| 120 | + return defaultValue; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * The {@code getValue()} method retrieves the current value of this option. |
| 125 | + * |
| 126 | + * @return the current value of this option |
| 127 | + */ |
| 128 | + public T getValue() { |
| 129 | + return !assigned ? defaultValue : value; |
| 130 | + } |
| 131 | + |
| 132 | + private boolean assigned; |
| 133 | + |
| 134 | + /** |
| 135 | + * The {@code setValue()) method sets the value of this option. |
| 136 | + * |
| 137 | + * @param value the new value to this option |
| 138 | + */ |
| 139 | + public void setValue(T value) { |
| 140 | + assigned = true; |
| 141 | + this.value = value; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * The {@code setValue()} method sets the value of this option, given a string value. |
| 146 | + * The type of this option is used to determine how to parse the string into a value |
| 147 | + * of the appropriate type. Thus this method may potentially throw runtime exceptions |
| 148 | + * if parsing fails. |
| 149 | + * |
| 150 | + * @param string the new value of this option as a string |
| 151 | + */ |
| 152 | + public void setString(String string) { |
| 153 | + setValue(type.parseValue(string)); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * The {@code getType()} method returns the type of this option. |
| 158 | + * @return the type of this option. |
| 159 | + */ |
| 160 | + public Type<T> getType() { |
| 161 | + return type; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * The {@code getString()} method retrieves the value of this option as a string. |
| 166 | + * The type of this option is used to determine how to unparse the value into a string. |
| 167 | + * |
| 168 | + * @return the value of this option as a string |
| 169 | + */ |
| 170 | + public String getString() { |
| 171 | + return type.unparseValue(getValue()); |
| 172 | + } |
| 173 | + |
| 174 | + public String getHelp() { |
| 175 | + return help; |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public String toString() { |
| 180 | + return getName(); |
| 181 | + } |
| 182 | +} |
0 commit comments