// Copyright (c) FIRST and other WPILib contributors. // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. package edu.wpi.first.units; import java.util.Objects; /** * Unit of measurement that defines a quantity, such as grams, meters, or seconds. * *
This is the base class for units. Actual units (such as {@link Units#Grams} and {@link
* Units#Meters}) can be found in the {@link Units} class.
*
* @param the self type, e.g. {@code class SomeUnit extends Unit
*
* @return the base unit
*/
public U getBaseUnit() {
return m_baseUnit;
}
/**
* Checks if this unit is the base unit for its own system of measurement.
*
* @return true if this is the base unit, false if not
*/
public boolean isBaseUnit() {
return this.equals(m_baseUnit);
}
/**
* Converts a value in terms of base units to a value in terms of this unit.
*
* @param valueInBaseUnits the value in base units to convert
* @return the equivalent value in terms of this unit
*/
public double fromBaseUnits(double valueInBaseUnits) {
return m_fromBaseConverter.apply(valueInBaseUnits);
}
/**
* Converts a value in terms of this unit to a value in terms of the base unit.
*
* @param valueInNativeUnits the value in terms of this unit to convert
* @return the equivalent value in terms of the base unit
*/
public double toBaseUnits(double valueInNativeUnits) {
return m_toBaseConverter.apply(valueInNativeUnits);
}
/**
* Converts a magnitude in terms of another unit of the same dimension to a magnitude in terms of
* this unit.
*
*
* Unit baseUnit = new Unit(null, ...);
* baseUnit.getBaseUnit(); // returns baseUnit
*
* Unit derivedUnit = new Unit(baseUnit, ...);
* derivedUnit.getBaseUnit(); // returns baseUnit
*
* Inches.convertFrom(12, Feet) // 144.0
* Kilograms.convertFrom(2.2, Pounds) // 0.9979024
*
*
* @param magnitude a magnitude measured in another unit
* @param otherUnit the unit to convert the magnitude to
* @return the corresponding value in terms of this unit.
*/
public double convertFrom(double magnitude, Unit otherUnit) {
if (this.equivalent(otherUnit)) {
// same unit, don't bother converting
return magnitude;
}
return this.fromBaseUnits(otherUnit.toBaseUnits(magnitude));
}
/**
* Gets the conversion function used to convert values to base unit terms. This generally
* shouldn't need to be used directly; prefer {@link #toBaseUnits(double)} instead.
*
* @return the conversion function
*/
public UnaryFunction getConverterToBase() {
return m_toBaseConverter;
}
/**
* Gets the conversion function used to convert values to terms of this unit. This generally
* shouldn't need to be used directly; prefer {@link #fromBaseUnits(double)} instead.
*
* @return the conversion function
*/
public UnaryFunction getConverterFromBase() {
return m_fromBaseConverter;
}
/**
* Creates a new measure of this unit with the given value. The resulting measure is
* immutable and cannot have its value modified.
*
* @param magnitude the magnitude of the measure to create
* @return the measure
*/
public Measure of(double magnitude) {
if (magnitude == 0) {
// reuse static object
return zero();
}
if (magnitude == 1) {
// reuse static object
return one();
}
return ImmutableMeasure.ofRelativeUnits(magnitude, this);
}
/**
* Creates a new measure with a magnitude equal to the given base unit magnitude, converted to be
* in terms of this unit.
*
* @param baseUnitMagnitude the magnitude of the measure in terms of the base unit
* @return the measure
*/
public Measure ofBaseUnits(double baseUnitMagnitude) {
return ImmutableMeasure.ofBaseUnits(baseUnitMagnitude, this);
}
/**
* Gets a measure with a magnitude of 0 in terms of this unit.
*
* @return the zero-valued measure
*/
public Measure zero() {
// lazy init because 'this' is null in object initialization
if (m_zero == null) {
m_zero = ImmutableMeasure.ofRelativeUnits(0, this);
}
return m_zero;
}
/**
* Gets a measure with a magnitude of 1 in terms of this unit.
*
* @return the 1-valued measure
*/
public Measure one() {
// lazy init because 'this' is null in object initialization
if (m_one == null) {
m_one = ImmutableMeasure.ofRelativeUnits(1, this);
}
return m_one;
}
/**
* Creates a velocity unit derived from this one. Can be chained to denote velocity, acceleration,
* jerk, etc.
*
*
* Meters.per(Second) // linear velocity
* Kilograms.per(Second) // mass flow
* Feet.per(Second).per(Second).of(32) // roughly 1G of acceleration
*
*
* @param period the time period of the velocity, such as seconds or milliseconds
* @return a velocity unit corresponding to the rate of change of this unit over time
*/
public Velocity per(Time period) {
return Velocity.combine(this, period);
}
/**
* Takes this unit and creates a new proportional unit where this unit is the numerator and the
* given denominator is the denominator.
*
*
* Volts.per(Meter) // V/m
*
*
* @param
* Volts.mult(Meter) // V*m
*
*
* @param