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 */
013package org.vmutil.options;
014
015import 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 */
024public 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   * Searches for a string in the enumeration.
047   *
048   * @param string the string to search for
049   * @return The index of the passed string.
050   */
051  private int findValue(String string) {
052    for (int i = 0; i < values.length; i++) {
053      if (values[i].equals(string)) {
054        return i;
055      }
056    }
057    fail("Invalid Enumeration Value");
058    return -1;
059  }
060
061  /**
062   * Read the current value of the option.
063   *
064   * @return The option value.
065   */
066  @Uninterruptible
067  public int getValue() {
068    return this.value;
069  }
070
071  /**
072   * Read the string for the current value of the option.
073   *
074   * @return The option value.
075   */
076  @Uninterruptible
077  public String getValueString() {
078    return this.values[this.value];
079  }
080
081  /**
082   * Read the default value of the option.
083   *
084   * @return The default value.
085   */
086  @Uninterruptible
087  public int getDefaultValue() {
088    return this.defaultValue;
089  }
090
091  /**
092   * Read the string for the default value of the option.
093   *
094   * @return The default value.
095   */
096  @Uninterruptible
097  public String getDefaultValueString() {
098    return this.values[this.defaultValue];
099  }
100
101  /**
102   * Update the value of the option, echoing the change if the echoOptions
103   * option is set. This method also calls the validate method to allow
104   * subclasses to perform any required validation.
105   *
106   * @param value The new value for the option.
107   */
108  public void setValue(int value) {
109    this.value = value;
110    validate();
111    set.logChange(this);
112  }
113
114  /**
115   * Look up the value for a string and update the value of the option
116   * accordingly, echoing the change if the echoOptions option is set.
117   * This method also calls the validate method to allow subclasses to
118   * perform any required validation.
119   *
120   * @param value The new value for the option.
121   */
122  public void setValue(String value) {
123    setValue(findValue(value));
124  }
125
126  /**
127   * Modify the default value of the option.
128   *
129   * @param value The new default value for the option.
130   */
131  public void setDefaultValue(String value) {
132    this.value = this.defaultValue = findValue(value);
133  }
134
135  /**
136   * Return the array of allowed enumeration values.
137   *
138   * @return The values array.
139   */
140  public String[] getValues() {
141    return this.values;
142  }
143}