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

rinde.sim.pdptw.common.DefaultVehicle Maven / Gradle / Ivy

The newest version!
/**
 * 
 */
package rinde.sim.pdptw.common;

import static com.google.common.base.Preconditions.checkArgument;
import rinde.sim.core.model.pdp.PDPModel;
import rinde.sim.core.model.pdp.Vehicle;
import rinde.sim.core.model.road.RoadModel;

import com.google.common.base.Optional;

/**
 * Default implementation of {@link Vehicle}, it initializes the vehicle based
 * on a {@link VehicleDTO} but it does not move.
 * @author Rinde van Lon 
 */
public abstract class DefaultVehicle extends Vehicle {

  /**
   * The data transfer object which holds the immutable properties of this
   * vehicle.
   */
  protected final VehicleDTO dto;

  /**
   * A reference to the {@link RoadModel}, it is absent until
   * {@link #initRoadPDP(RoadModel, PDPModel)} is called.
   */
  protected Optional roadModel;

  /**
   * A reference to the {@link PDPModel}, it is absent until
   * {@link #initRoadPDP(RoadModel, PDPModel)} is called.
   */
  protected Optional pdpModel;

  private double speed;

  /**
   * Instantiate a new vehicle using the specified properties.
   * @param pDto {@link #dto}
   */
  public DefaultVehicle(VehicleDTO pDto) {
    setStartPosition(pDto.startPosition);
    setCapacity(pDto.capacity);
    setSpeed(pDto.speed);
    dto = pDto;
    roadModel = Optional.absent();
    pdpModel = Optional.absent();
  }

  @Override
  public final double getSpeed() {
    return speed;
  }

  /**
   * Changes the speed of the vehicle.
   * @param newSpeed The new speed.
   */
  protected final void setSpeed(double newSpeed) {
    checkArgument(newSpeed >= 0, "Speed may not be negative.");
    speed = newSpeed;
  }

  @Override
  public void initRoadPDP(RoadModel pRoadModel, PDPModel pPdpModel) {
    roadModel = Optional.of(pRoadModel);
    pdpModel = Optional.of(pPdpModel);
  }

  /**
   * @return The {@link #dto}.
   */
  public VehicleDTO getDTO() {
    return dto;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy