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