All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rinde.sim.core.TimeLapse Maven / Gradle / Ivy

There is a newer version: 4.4.6
Show newest version
/**
 * 
 */
package rinde.sim.core;

import static com.google.common.base.Preconditions.checkArgument;

import javax.measure.quantity.Duration;
import javax.measure.unit.Unit;

// TODO investigate: could TimeLapse and TimeWindow both be a Range?
/**
 * Represents a consumable interval of time: [start, end). Instances of time
 * lapse are handed out by the {@link Simulator} and can be received by
 * implementing the {@link TickListener} interface.
 * @author Rinde van Lon ([email protected])
 */
public final class TimeLapse {

  private final Unit timeUnit;
  private long startTime;
  private long endTime;
  private long timeLeft;

  TimeLapse(Unit unit) {
    timeUnit = unit;
  }

  void initialize(long start, long end) {
    checkArgument(start >= 0, "time must be positive");
    checkArgument(end > start, "end time must be after start time");
    startTime = start;
    endTime = end;
    timeLeft = end - start;
  }

  /**
   * Consumes the specified amount of time, where time must be strictly positive
   * and there must be enough time left as specified by {@link #getTimeLeft()}.
   * @param time The time to consume.
   */
  public void consume(long time) {
    checkArgument(time >= 0,
        "the time to consume must be a positive value, it is %s.", time);
    checkArgument(timeLeft - time >= 0,
        "there is not enough time left to consume %s.", time);
    timeLeft -= time;
  }

  /**
   * Consumes the entire time lapse.
   */
  public void consumeAll() {
    timeLeft = 0;
  }

  /**
   * @return If there is time left to consume.
   */
  public boolean hasTimeLeft() {
    return timeLeft > 0;
  }

  /**
   * @return The amount of time left to consume.
   */
  public long getTimeLeft() {
    return timeLeft;
  }

  /**
   * @return The current time taking into account any time consumption in this
   *         instance. When looking for the time at the start of this time lapse
   *         use {@link #getStartTime()}.
   */
  public long getTime() {
    return endTime - timeLeft;
  }

  /**
   * @return The step (or length) of this time lapse.
   */
  public long getTimeStep() {
    return endTime - startTime;
  }

  /**
   * @return The time that was previously consumed in this time lapse.
   */
  public long getTimeConsumed() {
    return (endTime - startTime) - timeLeft;
  }

  /**
   * @return The start time of this time lapse.
   */
  public long getStartTime() {
    return startTime;
  }

  /**
   * @return The end time of this time lapse.
   */
  public long getEndTime() {
    return endTime;
  }

  /**
   * Checks whether the specified time lies in this {@link TimeLapse}.
   * @param t The time to check.
   * @return true if the specified time is in this
   *         {@link TimeLapse}, false otherwise.
   */
  public boolean isIn(long t) {
    return t >= startTime && t < endTime;
  }

  /**
   * @return The time unit of this {@link TimeLapse}.
   */
  public Unit getTimeUnit() {
    return timeUnit;
  }

  @Override
  public String toString() {
    return new StringBuilder("[").append(startTime).append(",").append(endTime)
        .append(")").toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy