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 * A time option that stores values at a microsecond granularity.
019 */
020public class MicrosecondsOption extends Option {
021  // values
022  protected int defaultValue;
023  protected int value;
024
025  /**
026   * Create a new microsecond option.
027   *
028   * @param set The option set this option belongs to.
029   * @param name The space separated name for the option.
030   * @param desc The purpose of the option
031   * @param defaultUs The default value of the option (usec).
032   */
033  protected MicrosecondsOption(OptionSet set, String name, String desc, int defaultUs) {
034    super(set, MICROSECONDS_OPTION, name, desc);
035    this.value = this.defaultValue = defaultUs;
036  }
037
038  /**
039   * Read the current value of the option in microseconds.
040   *
041   * @return The option value.
042   */
043  @Uninterruptible
044  public int getMicroseconds() {
045    return this.value;
046  }
047
048  /**
049   * Read the current value of the option in milliseconds.
050   *
051   * @return The option value.
052   */
053  @Uninterruptible
054  public int getMilliseconds() {
055    return this.value / 1000;
056  }
057
058  /**
059   * Read the default value of the option in microseconds.
060   *
061   * @return The default value.
062   */
063  @Uninterruptible
064  public int getDefaultMicroseconds() {
065    return this.defaultValue;
066  }
067
068  /**
069   * Read the default value of the option in milliseconds.
070   *
071   * @return The default value.
072   */
073  @Uninterruptible
074  public int getDefaultMilliseconds() {
075    return this.defaultValue / 1000;
076  }
077
078  /**
079   * Update the value of the option, echoing the change if the echoOptions
080   * option is set. An error occurs if the value is negative, and then the
081   * validate method is called to allow subclasses to perform any additional
082   * validation.
083   *
084   * @param value The new value for the option.
085   */
086  public void setMicroseconds(int value) {
087    failIf(value < 0, "Unreasonable " + this.getName() + " value");
088    this.value = value;
089    validate();
090    set.logChange(this);
091  }
092
093  /**
094   * Modify the default value of the option.
095   *
096   * @param value The new default value for the option.
097   */
098  public void setDefaultMicrosends(int value) {
099    this.value = this.defaultValue = value;
100  }
101}