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.Temperature;
008
009/**
010 * Unit of temperature dimension.
011 *
012 * <p>This is the base type for units of temperature dimension. It is also used to specify the
013 * dimension for {@link Measure}: <code>Measure&lt;TemperatureUnit&gt;</code>.
014 *
015 * <p>Actual units (such as {@link Units#Celsius} and {@link Units#Fahrenheit}) can be found in the
016 * {@link Units} class.
017 */
018public final class TemperatureUnit extends Unit {
019  TemperatureUnit(
020      TemperatureUnit baseUnit,
021      UnaryFunction toBaseConverter,
022      UnaryFunction fromBaseConverter,
023      String name,
024      String symbol) {
025    super(baseUnit, toBaseConverter, fromBaseConverter, name, symbol);
026  }
027
028  @Override
029  public TemperatureUnit getBaseUnit() {
030    return (TemperatureUnit) super.getBaseUnit();
031  }
032
033  @Override
034  public Temperature of(double magnitude) {
035    return new Temperature(magnitude, toBaseUnits(magnitude), this);
036  }
037
038  @Override
039  public Temperature ofBaseUnits(double baseUnitMagnitude) {
040    return new Temperature(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
041  }
042
043  @Override
044  public Temperature zero() {
045    return (Temperature) super.zero();
046  }
047
048  @Override
049  public Temperature one() {
050    return (Temperature) super.one();
051  }
052
053  @Override
054  public VelocityUnit<TemperatureUnit> per(TimeUnit period) {
055    return VelocityUnit.combine(this, period);
056  }
057
058  /**
059   * Creates a ratio unit between this unit and an arbitrary other unit.
060   *
061   * @param other the other unit
062   * @param <U> the type of the other unit
063   * @return the ratio unit
064   */
065  public <U extends Unit> PerUnit<TemperatureUnit, U> per(U other) {
066    return PerUnit.combine(this, other);
067  }
068
069  /**
070   * Converts a measurement value in terms of another temperature unit to this unit.
071   *
072   * @param magnitude the magnitude of the measurement in terms of the other temperature unit
073   * @param otherUnit the other temperature unit
074   * @return the value of the measurement in terms of this unit
075   */
076  public double convertFrom(double magnitude, TemperatureUnit otherUnit) {
077    return fromBaseUnits(otherUnit.toBaseUnits(magnitude));
078  }
079}