[wpiunits] Add Measure.per overloads for all known unit types (#7699)

Instead of only providing per(TimeUnit)

Useful for making conversion factors easier, eg `Inches.of(10).per(Rotation)` vs `Inches.of(10).per(Rotation.one())`

Update VelocityUnit.one() and VelocityUnit.zero() to return Velocity objects instead of generic Measure<? extends VelocityUnit<D>>; VelocityUnit is final, so the wildcard generic is unnecessary, and this makes the generated `per` functions possible for this type
This commit is contained in:
Sam Carlberg
2025-01-17 02:24:11 -05:00
committed by GitHub
parent a9f3fc6b2c
commit e52f400687
28 changed files with 3300 additions and 145 deletions

View File

@@ -1021,6 +1021,270 @@ public interface Measure<U extends Unit> extends Comparable<Measure<U>> {
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by an acceleration unit and returns the result in the most appropriate
* unit. This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(AccelerationUnit<?> divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by an angle unit and returns the result in the most appropriate unit. This
* is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(AngleUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by an angular acceleration unit and returns the result in the most
* appropriate unit. This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(AngularAccelerationUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by an angular momentum unit and returns the result in the most appropriate
* unit. This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(AngularMomentumUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by an angular velocity unit and returns the result in the most appropriate
* unit. This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(AngularVelocityUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a unit of electric current and returns the result in the most
* appropriate unit. This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(CurrentUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a distance unit and returns the result in the most appropriate unit.
* This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(DistanceUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by an energy unit and returns the result in the most appropriate unit.
* This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(EnergyUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a force unit and returns the result in the most appropriate unit. This
* is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(ForceUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a frequency unit and returns the result in the most appropriate unit.
* This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(FrequencyUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a linear acceleration unit and returns the result in the most
* appropriate unit. This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(LinearAccelerationUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a linear momentum unit and returns the result in the most appropriate
* unit. This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(LinearMomentumUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a linear velocity unit and returns the result in the most appropriate
* unit. This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(LinearVelocityUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a mass unit and returns the result in the most appropriate unit. This
* is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(MassUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a moment of inertia unit and returns the result in the most appropriate
* unit. This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(MomentOfInertiaUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a generic compound unit and returns the result in the most appropriate
* unit. This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(MultUnit<?, ?> divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a power unit and returns the result in the most appropriate unit. This
* is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(PowerUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a generic ratio unit and returns the result in the most appropriate
* unit. This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(PerUnit<?, ?> divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a temperature unit and returns the result in the most appropriate unit.
* This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(TemperatureUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a time period and returns the result in the most appropriate unit. This
* is equivalent to {@code div(period.of(1))}.
*
* @param period the time period measurement to divide by.
* @return the division result
*/
default Measure<?> per(TimeUnit period) {
return div(period.of(1));
}
/**
* Divides this measure by a torque unit and returns the result in the most appropriate unit. This
* is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(TorqueUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a velocity unit and returns the result in the most appropriate unit.
* This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(VelocityUnit<?> divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a voltage unit and returns the result in the most appropriate unit.
* This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(VoltageUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a resistance unit and returns the result in the most appropriate unit.
* This is equivalent to {@code div(divisorUnit.of(1))}.
*
* @param divisorUnit the unit to divide by.
* @return the division result
*/
default Measure<?> per(ResistanceUnit divisorUnit) {
return div(divisorUnit.one());
}
/**
* Divides this measure by a unitless scalar and returns the result.
*
@@ -1371,17 +1635,6 @@ public interface Measure<U extends Unit> extends Comparable<Measure<U>> {
baseUnitMagnitude() / divisor.baseUnitMagnitude(), divisor.unit().denominator());
}
/**
* Divides this measure by a time period and returns the result in the most appropriate unit. This
* is equivalent to {@code div(period.of(1))}.
*
* @param period the time period measurement to divide by.
* @return the division result
*/
default Measure<?> per(TimeUnit period) {
return div(period.of(1));
}
/**
* Checks if this measure is near another measure of the same unit. Provide a variance threshold
* for use for a +/- scalar, such as 0.05 for +/- 5%.

View File

@@ -90,14 +90,14 @@ public final class VelocityUnit<D extends Unit> extends PerUnit<D, TimeUnit> {
@Override
@SuppressWarnings("unchecked")
public Measure<? extends VelocityUnit<D>> zero() {
return (Measure<? extends VelocityUnit<D>>) super.zero();
public Velocity<D> zero() {
return (Velocity<D>) super.zero();
}
@Override
@SuppressWarnings("unchecked")
public Measure<? extends VelocityUnit<D>> one() {
return (Measure<? extends VelocityUnit<D>>) super.one();
public Velocity<D> one() {
return (Velocity<D>) super.one();
}
@Override