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.Mass;
008
009/**
010 * Unit of mass dimension.
011 *
012 * <p>This is the base type for units of mass dimension. It is also used to specify the dimension
013 * for the mass-specific {@link Mass} measurement type.
014 *
015 * <p>Actual units (such as {@link Units#Grams} and {@link Units#Pounds}) can be found in the {@link
016 * Units} class.
017 */
018public final class MassUnit extends Unit {
019  /** Creates a new unit with the given name and multiplier to the base unit. */
020  MassUnit(MassUnit baseUnit, double baseUnitEquivalent, String name, String symbol) {
021    super(baseUnit, baseUnitEquivalent, name, symbol);
022  }
023
024  MassUnit(
025      MassUnit baseUnit,
026      UnaryFunction toBaseConverter,
027      UnaryFunction fromBaseConverter,
028      String name,
029      String symbol) {
030    super(baseUnit, toBaseConverter, fromBaseConverter, name, symbol);
031  }
032
033  @Override
034  public MassUnit getBaseUnit() {
035    return (MassUnit) super.getBaseUnit();
036  }
037
038  @Override
039  public Mass of(double magnitude) {
040    return new Mass(magnitude, toBaseUnits(magnitude), this);
041  }
042
043  @Override
044  public Mass ofBaseUnits(double baseUnitMagnitude) {
045    return new Mass(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
046  }
047
048  @Override
049  public Mass zero() {
050    return (Mass) super.zero();
051  }
052
053  @Override
054  public Mass one() {
055    return (Mass) super.one();
056  }
057
058  @Override
059  public VelocityUnit<MassUnit> per(TimeUnit period) {
060    return VelocityUnit.combine(this, period);
061  }
062
063  /**
064   * Creates a ratio unit between this unit and an arbitrary other unit.
065   *
066   * @param other the other unit
067   * @param <U> the type of the other unit
068   * @return the ratio unit
069   */
070  public <U extends Unit> PerUnit<MassUnit, U> per(U other) {
071    return PerUnit.combine(this, other);
072  }
073
074  /**
075   * Converts a measurement value in terms of another mass unit to this unit.
076   *
077   * @param magnitude the magnitude of the measurement in terms of the other mass unit
078   * @param otherUnit the other mass unit
079   * @return the value of the measurement in terms of this unit
080   */
081  public double convertFrom(double magnitude, MassUnit otherUnit) {
082    return fromBaseUnits(otherUnit.toBaseUnits(magnitude));
083  }
084
085  /**
086   * Multiplies this mass unit by a unit of linear velocity to form a combined unit of linear
087   * momentum.
088   *
089   * @param velocity the unit of velocity
090   * @return the combined unit of momentum
091   */
092  public LinearMomentumUnit mult(LinearVelocityUnit velocity) {
093    return LinearMomentumUnit.combine(this, velocity);
094  }
095
096  /**
097   * Multiplies this mass unit by a unit of linear acceleration to form a combined unit of force.
098   *
099   * @param acceleration the unit of acceleration
100   * @return the combined unit of force
101   */
102  public ForceUnit mult(LinearAccelerationUnit acceleration) {
103    return ForceUnit.combine(this, acceleration);
104  }
105}