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