001 /*
002 * This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 * This file is licensed to You under the Eclipse Public License (EPL);
005 * You may not use this file except in compliance with the License. You
006 * may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 * See the COPYRIGHT.txt file distributed with this work for information
011 * regarding copyright ownership.
012 */
013 package org.vmutil.options;
014
015 import org.vmmagic.pragma.Uninterruptible;
016
017 /**
018 * An option that is a selection of several strings. The mapping
019 * between strings and integers is determined using indexes into
020 * a string array.
021 * <p>
022 * Enumerations are case sensitive.
023 */
024 public class EnumOption extends Option {
025 // values
026 protected int defaultValue;
027 protected int value;
028 protected String[] values;
029
030 /**
031 * Create a new enumeration option.
032 *
033 * @param set The option set this option belongs to.
034 * @param name The space separated name for the option.
035 * @param description The purpose of the option.
036 * @param values A mapping of int to string for the enum.
037 * @param defaultValue The default value of the option.
038 */
039 protected EnumOption(OptionSet set, String name, String description, String[] values, String defaultValue) {
040 super(set, ENUM_OPTION, name, description);
041 this.values = values;
042 this.value = this.defaultValue = findValue(defaultValue);
043 }
044
045 /**
046 * Search for a string in the enumeration.
047 *
048 * @return The index of the passed string.
049 */
050 private int findValue(String string) {
051 for (int i = 0; i < values.length; i++) {
052 if (values[i].equals(string)) {
053 return i;
054 }
055 }
056 fail("Invalid Enumeration Value");
057 return -1;
058 }
059
060 /**
061 * Read the current value of the option.
062 *
063 * @return The option value.
064 */
065 @Uninterruptible
066 public int getValue() {
067 return this.value;
068 }
069
070 /**
071 * Read the string for the current value of the option.
072 *
073 * @return The option value.
074 */
075 @Uninterruptible
076 public String getValueString() {
077 return this.values[this.value];
078 }
079
080 /**
081 * Read the default value of the option.
082 *
083 * @return The default value.
084 */
085 @Uninterruptible
086 public int getDefaultValue() {
087 return this.defaultValue;
088 }
089
090 /**
091 * Read the string for the default value of the option.
092 *
093 * @return The default value.
094 */
095 @Uninterruptible
096 public String getDefaultValueString() {
097 return this.values[this.defaultValue];
098 }
099
100 /**
101 * Update the value of the option, echoing the change if the echoOptions
102 * option is set. This method also calls the validate method to allow
103 * subclasses to perform any required validation.
104 *
105 * @param value The new value for the option.
106 */
107 public void setValue(int value) {
108 this.value = value;
109 validate();
110 set.logChange(this);
111 }
112
113 /**
114 * Look up the value for a string and update the value of the option
115 * accordingly, echoing the change if the echoOptions option is set.
116 * This method also calls the validate method to allow subclasses to
117 * perform any required validation.
118 *
119 * @param value The new value for the option.
120 */
121 public void setValue(String value) {
122 setValue(findValue(value));
123 }
124
125 /**
126 * Modify the default value of the option.
127 *
128 * @param value The new default value for the option.
129 */
130 public void setDefaultValue(String value) {
131 this.value = this.defaultValue = findValue(value);
132 }
133
134 /**
135 * Return the array of allowed enumeration values.
136 *
137 * @return The values array.
138 */
139 public String[] getValues() {
140 return this.values;
141 }
142 }