001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.units;
006
007import edu.wpi.first.units.measure.Distance;
008
009/**
010 * Unit of linear dimension.
011 *
012 * <p>This is the base type for units of linear dimension. It is also used to specify the dimension
013 * for {@link Measure}: <code>Measure&lt;DistanceUnit&gt;</code>.
014 *
015 * <p>Actual units (such as {@link Units#Meters} and {@link Units#Inches}) can be found in the
016 * {@link Units} class.
017 */
018public final class DistanceUnit extends Unit {
019  DistanceUnit(DistanceUnit baseUnit, double baseUnitEquivalent, String name, String symbol) {
020    super(baseUnit, baseUnitEquivalent, name, symbol);
021  }
022
023  DistanceUnit(
024      DistanceUnit baseUnit,
025      UnaryFunction toBaseConverter,
026      UnaryFunction fromBaseConverter,
027      String name,
028      String symbol) {
029    super(baseUnit, toBaseConverter, fromBaseConverter, name, symbol);
030  }
031
032  @Override
033  public DistanceUnit getBaseUnit() {
034    return (DistanceUnit) super.getBaseUnit();
035  }
036
037  @Override
038  public LinearVelocityUnit per(TimeUnit period) {
039    return LinearVelocityUnit.combine(this, period);
040  }
041
042  /**
043   * Creates a ratio unit between this unit and an arbitrary other unit.
044   *
045   * @param other the other unit
046   * @param <U> the type of the other unit
047   * @return the ratio unit
048   */
049  public <U extends Unit> PerUnit<DistanceUnit, U> per(U other) {
050    return PerUnit.combine(this, other);
051  }
052
053  /**
054   * Converts a measurement value in terms of another distance unit to this unit.
055   *
056   * @param magnitude the magnitude of the measurement in terms of the other distance unit
057   * @param otherUnit the other distance unit
058   * @return the value of the measurement in terms of this unit
059   */
060  public double convertFrom(double magnitude, DistanceUnit otherUnit) {
061    return fromBaseUnits(otherUnit.toBaseUnits(magnitude));
062  }
063
064  @Override
065  public Distance of(double magnitude) {
066    return new Distance(magnitude, toBaseUnits(magnitude), this);
067  }
068
069  @Override
070  public Distance ofBaseUnits(double baseUnitMagnitude) {
071    return new Distance(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
072  }
073
074  @Override
075  public Distance zero() {
076    return (Distance) super.zero();
077  }
078
079  @Override
080  public Distance one() {
081    return (Distance) super.one();
082  }
083
084  /**
085   * Multiplies this distance unit by a unit of force to create a unit of torque.
086   *
087   * @param force the unit of force
088   * @return the combined torque unit
089   */
090  public TorqueUnit multAsTorque(ForceUnit force) {
091    return TorqueUnit.combine(this, force);
092  }
093
094  // TODO: Add a multAsEnergy equivalent
095}