[wpiunits] Add Measure.divide(Measure<U2>) (#6611)

This commit is contained in:
Wispy
2024-05-15 08:22:38 -05:00
committed by GitHub
parent b0d3bf4ddf
commit 7fbbecb5b7
5 changed files with 75 additions and 11 deletions

View File

@@ -137,16 +137,29 @@ public interface Measure<U extends Unit<U>> extends Comparable<Measure<U>> {
}
/**
* Divides this measurement by some constant divisor and returns the result. This is equivalent to
* {@code divide(divisor.baseUnitMagnitude())}
* Divides this measurement by another measure and performs some dimensional analysis to reduce
* the units.
*
* @param divisor the dimensionless measure to divide by
* @param <U2> the type of the other measure to multiply by
* @param other the unit to multiply by
* @return the resulting measure
* @see #divide(double)
* @see #times(double)
*/
default Measure<U> divide(Measure<Dimensionless> divisor) {
return divide(divisor.baseUnitMagnitude());
default <U2 extends Unit<U2>> Measure<?> divide(Measure<U2> other) {
if (unit().getBaseUnit().equals(other.unit().getBaseUnit())) {
return Units.Value.ofBaseUnits(baseUnitMagnitude() / other.baseUnitMagnitude());
}
if (other.unit() instanceof Dimensionless) {
return divide(other.baseUnitMagnitude());
}
if (other.unit() instanceof Velocity<?> velocity
&& velocity.getUnit().getBaseUnit().equals(unit().getBaseUnit())) {
return times(velocity.reciprocal().ofBaseUnits(1 / other.baseUnitMagnitude()));
}
if (other.unit() instanceof Per<?, ?> per
&& per.numerator().getBaseUnit().equals(unit().getBaseUnit())) {
return times(per.reciprocal().ofBaseUnits(1 / other.baseUnitMagnitude()));
}
return unit().per(other.unit()).ofBaseUnits(baseUnitMagnitude() / other.baseUnitMagnitude());
}
/**