[wpiunits] Add isNear function implementation (#6396)

This implementation uses a tolerance in the same units as the measure it checks.
This commit is contained in:
vichik
2024-04-22 06:40:33 +03:00
committed by GitHub
parent abfe2488ff
commit e89c8c1008
2 changed files with 58 additions and 1 deletions

View File

@@ -265,6 +265,25 @@ public interface Measure<U extends Unit<U>> extends Comparable<Measure<U>> {
return Math.abs(this.baseUnitMagnitude() - other.baseUnitMagnitude()) <= tolerance;
}
/**
* Checks if this measure is near another measure of the same unit, with a specified tolerance of
* the same unit.
*
* <pre>
* Meters.of(1).isNear(Meters.of(1.2), Millimeters.of(300)) // true
* Degrees.of(90).isNear(Rotations.of(0.5), Degrees.of(45)) // false
* </pre>
*
* @param other the other measure to compare against.
* @param tolerance the tolerance allowed in which the two measures are defined as near each
* other.
* @return true if this unit is near the other measure, otherwise false.
*/
default boolean isNear(Measure<U> other, Measure<U> tolerance) {
return Math.abs(this.baseUnitMagnitude() - other.baseUnitMagnitude())
<= Math.abs(tolerance.baseUnitMagnitude());
}
/**
* Checks if this measure is equivalent to another measure of the same unit.
*