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.Resistance;
008
009/**
010 * Unit of resistance dimension.
011 *
012 * <p>This is the base type for units of resistance dimension. It is also used to specify the
013 * dimension for {@link Measure}: <code>Measure&lt;ResistanceUnit&gt;</code>.
014 *
015 * <p>Actual units (such as {@link Units#Ohms} and {@link Units#KiloOhms}) can be found in the
016 * {@link Units} class.
017 */
018public final class ResistanceUnit extends PerUnit<VoltageUnit, CurrentUnit> {
019  private static final CombinatoryUnitCache<VoltageUnit, CurrentUnit, ResistanceUnit> cache =
020      new CombinatoryUnitCache<>(ResistanceUnit::new);
021
022  ResistanceUnit(VoltageUnit numerator, CurrentUnit denominator) {
023    super(
024        numerator.isBaseUnit() && denominator.isBaseUnit()
025            ? null
026            : combine(numerator.getBaseUnit(), denominator.getBaseUnit()),
027        numerator,
028        denominator);
029  }
030
031  ResistanceUnit(
032      ResistanceUnit baseUnit,
033      UnaryFunction toBaseConverter,
034      UnaryFunction fromBaseConverter,
035      String name,
036      String symbol) {
037    super(baseUnit, toBaseConverter, fromBaseConverter, name, symbol);
038  }
039
040  /**
041   * Combines an voltage and a current unit to form a unit of resistance.
042   *
043   * @param voltage the unit of voltage
044   * @param current the unit of current
045   * @return the combined unit of resistance
046   */
047  public static ResistanceUnit combine(VoltageUnit voltage, CurrentUnit current) {
048    return cache.combine(voltage, current);
049  }
050
051  @Override
052  public ResistanceUnit getBaseUnit() {
053    return (ResistanceUnit) super.getBaseUnit();
054  }
055
056  @Override
057  public Resistance of(double magnitude) {
058    return new Resistance(magnitude, toBaseUnits(magnitude), this);
059  }
060
061  @Override
062  public Resistance ofBaseUnits(double baseUnitMagnitude) {
063    return new Resistance(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
064  }
065
066  @Override
067  public Resistance zero() {
068    return (Resistance) super.zero();
069  }
070
071  @Override
072  public Resistance one() {
073    return (Resistance) super.one();
074  }
075
076  /**
077   * Creates a ratio unit between this unit and an arbitrary other unit.
078   *
079   * @param other the other unit
080   * @param <U> the type of the other unit
081   * @return the ratio unit
082   */
083  public <U extends Unit> PerUnit<ResistanceUnit, U> per(U other) {
084    return PerUnit.combine(this, other);
085  }
086
087  /**
088   * Converts a measurement value in terms of another power unit to this unit.
089   *
090   * @param magnitude the magnitude of the measurement in terms of the other power unit
091   * @param otherUnit the other power unit
092   * @return the value of the measurement in terms of this unit
093   */
094  public double convertFrom(double magnitude, ResistanceUnit otherUnit) {
095    return fromBaseUnits(otherUnit.toBaseUnits(magnitude));
096  }
097}