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.LinearAcceleration;
008
009/**
010 * A unit of linear acceleration like {@link edu.wpi.first.units.Units#MetersPerSecondPerSecond}.
011 */
012public final class LinearAccelerationUnit extends PerUnit<LinearVelocityUnit, TimeUnit> {
013  private static final CombinatoryUnitCache<LinearVelocityUnit, TimeUnit, LinearAccelerationUnit>
014      cache = new CombinatoryUnitCache<>(LinearAccelerationUnit::new);
015
016  LinearAccelerationUnit(LinearVelocityUnit numerator, TimeUnit denominator) {
017    super(
018        numerator.isBaseUnit() && denominator.isBaseUnit()
019            ? null
020            : combine(numerator.getBaseUnit(), denominator.getBaseUnit()),
021        numerator,
022        denominator);
023  }
024
025  LinearAccelerationUnit(
026      LinearAccelerationUnit baseUnit,
027      UnaryFunction toBaseConverter,
028      UnaryFunction fromBaseConverter,
029      String name,
030      String symbol) {
031    super(baseUnit, toBaseConverter, fromBaseConverter, name, symbol);
032  }
033
034  /**
035   * Combines a linear velocity and time unit to form a unit of linear acceleration.
036   *
037   * @param velocity the unit of linear velocity
038   * @param period the unit of time
039   * @return the combined unit of linear acceleration
040   */
041  public static LinearAccelerationUnit combine(LinearVelocityUnit velocity, TimeUnit period) {
042    return cache.combine(velocity, period);
043  }
044
045  @Override
046  public LinearAccelerationUnit getBaseUnit() {
047    return (LinearAccelerationUnit) super.getBaseUnit();
048  }
049
050  @Override
051  public LinearAcceleration of(double magnitude) {
052    return new LinearAcceleration(magnitude, toBaseUnits(magnitude), this);
053  }
054
055  @Override
056  public LinearAcceleration ofBaseUnits(double baseUnitMagnitude) {
057    return new LinearAcceleration(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
058  }
059
060  @Override
061  public LinearAcceleration zero() {
062    return (LinearAcceleration) super.zero();
063  }
064
065  @Override
066  public LinearAcceleration one() {
067    return (LinearAcceleration) super.one();
068  }
069
070  @Override
071  public VelocityUnit<LinearAccelerationUnit> per(TimeUnit time) {
072    return VelocityUnit.combine(this, time);
073  }
074
075  /**
076   * Creates a ratio unit between this unit and an arbitrary other unit.
077   *
078   * @param other the other unit
079   * @param <U> the type of the other unit
080   * @return the ratio unit
081   */
082  public <U extends Unit> PerUnit<LinearAccelerationUnit, U> per(U other) {
083    return PerUnit.combine(this, other);
084  }
085
086  /**
087   * Converts a measurement value in terms of another unit to this unit.
088   *
089   * @param magnitude the magnitude of the measurement in terms of the other unit
090   * @param otherUnit the other unit
091   * @return the value of the measurement in terms of this unit
092   */
093  public double convertFrom(double magnitude, LinearAccelerationUnit otherUnit) {
094    return fromBaseUnits(otherUnit.toBaseUnits(magnitude));
095  }
096
097  /**
098   * Gets the unit of the changing velocity. This is equivalent to {@link #numerator()} and is left
099   * for historical purposes.
100   *
101   * @return the unit of the changing velocity
102   */
103  public LinearVelocityUnit getUnit() {
104    return numerator();
105  }
106
107  /**
108   * Gets the unit of the acceleration period (how long it takes for a measured velocity to change
109   * by one unit of velocity). This is equivalent to {@link #numerator()} and is left for historical
110   * purposes.
111   *
112   * @return the unit of the acceleration period
113   */
114  public TimeUnit getPeriod() {
115    return denominator();
116  }
117}