mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-01 02:41:48 +00:00
[wpiunits] Change units to track their base unit, instead of their base class (#6342)
Unit objects now have a reference to the base unit from which they're derived. Constructing a unit object without specifying a base unit implicitly signifies that it's its own base unit, eg new Angle(null, 1, "Radian", "rad") would be the base angle unit of radians, while new Angle(Radians, 2 * PI, "Rotation", "R") would be a new angle unit based on radians. This fixes much of the hacky code surrounding the derived unit types Velocity, Per, and Mult, but is a breaking change for any user code that defines custom unit classes or uses the anonymous unit type.
This commit is contained in:
@@ -14,13 +14,6 @@ public final class Units {
|
||||
|
||||
// Pseudo-classes describing the more common units of measure.
|
||||
|
||||
/**
|
||||
* Used as an internal placeholder value when a specific unit type cannot be determined. Do not
|
||||
* use this directly.
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static final Unit AnonymousBaseUnit = new Dimensionless(1, "<?>", "<?>");
|
||||
|
||||
// Distance
|
||||
/** The base unit of distance. */
|
||||
public static final Distance Meters = BaseUnits.Distance;
|
||||
@@ -98,7 +91,8 @@ public final class Units {
|
||||
* A single turn of an object around an external axis. Numerically equivalent to {@link
|
||||
* #Rotations}, but may be semantically more expressive in certain scenarios.
|
||||
*/
|
||||
public static final Angle Revolutions = new Angle(2 * Math.PI, "Revolution", "R");
|
||||
public static final Angle Revolutions =
|
||||
derive(Radians).aggregate(2 * Math.PI).named("Revolution").symbol("R").make();
|
||||
|
||||
/**
|
||||
* A single turn of an object around an external axis. Numerically equivalent to a {@link
|
||||
@@ -110,7 +104,7 @@ public final class Units {
|
||||
* A single turn of an object around an internal axis. Numerically equivalent to {@link
|
||||
* #Revolutions}, but may be semantically more expressive in certain scenarios.
|
||||
*/
|
||||
public static final Angle Rotations = new Angle(2 * Math.PI, "Rotation", "R"); // alias revolution
|
||||
public static final Angle Rotations = derive(Revolutions).named("Rotation").symbol("R").make();
|
||||
|
||||
/**
|
||||
* A single turn of an object around an internal axis. Numerically equivalent to a {@link
|
||||
@@ -510,16 +504,4 @@ public final class Units {
|
||||
public static <U extends Unit<U>> UnitBuilder<U> derive(Unit<U> unit) {
|
||||
return new UnitBuilder<>((U) unit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an anonymous unit for use when a specific unit type is not known. Do not use this
|
||||
* directly.
|
||||
*
|
||||
* @param <U> the dimension of the desired anonymous unit
|
||||
* @return the anonymous unit
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <U extends Unit<U>> U anonymous() {
|
||||
return (U) AnonymousBaseUnit;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user