001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package edu.wpi.first.units; 006 007import static edu.wpi.first.units.Units.Value; 008 009import edu.wpi.first.units.measure.Acceleration; 010import edu.wpi.first.units.measure.Angle; 011import edu.wpi.first.units.measure.AngularAcceleration; 012import edu.wpi.first.units.measure.AngularMomentum; 013import edu.wpi.first.units.measure.AngularVelocity; 014import edu.wpi.first.units.measure.Current; 015import edu.wpi.first.units.measure.Dimensionless; 016import edu.wpi.first.units.measure.Distance; 017import edu.wpi.first.units.measure.Energy; 018import edu.wpi.first.units.measure.Force; 019import edu.wpi.first.units.measure.Frequency; 020import edu.wpi.first.units.measure.LinearAcceleration; 021import edu.wpi.first.units.measure.LinearMomentum; 022import edu.wpi.first.units.measure.LinearVelocity; 023import edu.wpi.first.units.measure.Mass; 024import edu.wpi.first.units.measure.MomentOfInertia; 025import edu.wpi.first.units.measure.Mult; 026import edu.wpi.first.units.measure.Per; 027import edu.wpi.first.units.measure.Power; 028import edu.wpi.first.units.measure.Resistance; 029import edu.wpi.first.units.measure.Temperature; 030import edu.wpi.first.units.measure.Time; 031import edu.wpi.first.units.measure.Torque; 032import edu.wpi.first.units.measure.Velocity; 033import edu.wpi.first.units.measure.Voltage; 034 035/** 036 * A measure holds the magnitude and unit of some dimension, such as distance, time, or speed. Two 037 * measures with the same <i>unit</i> and <i>magnitude</i> are effectively equivalent objects. 038 * 039 * @param <U> the unit type of the measure 040 */ 041public interface Measure<U extends Unit> extends Comparable<Measure<U>> { 042 /** 043 * The threshold for two measures to be considered equivalent if converted to the same unit. This 044 * is only needed due to floating-point error. 045 */ 046 double EQUIVALENCE_THRESHOLD = 1e-12; 047 048 /** 049 * Gets the unitless magnitude of this measure. 050 * 051 * @return the magnitude in terms of {@link #unit() the unit}. 052 */ 053 double magnitude(); 054 055 /** 056 * Gets the magnitude of this measure in terms of the base unit. If the unit is the base unit for 057 * its system of measure, then the value will be equivalent to {@link #magnitude()}. 058 * 059 * @return the magnitude in terms of the base unit 060 */ 061 double baseUnitMagnitude(); 062 063 /** 064 * Gets the units of this measure. 065 * 066 * @return the unit 067 */ 068 U unit(); 069 070 /** 071 * Converts this measure to a measure with a different unit of the same type, eg minutes to 072 * seconds. Converting to the same unit is equivalent to calling {@link #magnitude()}. 073 * 074 * <pre> 075 * Meters.of(12).in(Feet) // 39.3701 076 * Seconds.of(15).in(Minutes) // 0.25 077 * </pre> 078 * 079 * @param unit the unit to convert this measure to 080 * @return the value of this measure in the given unit 081 */ 082 default double in(U unit) { 083 if (this.unit().equals(unit)) { 084 return magnitude(); 085 } else { 086 return unit.fromBaseUnits(baseUnitMagnitude()); 087 } 088 } 089 090 /** 091 * A convenience method to get the base unit of the measurement. Equivalent to {@code 092 * unit().getBaseUnit()}. 093 * 094 * @return the base unit of measure. 095 */ 096 @SuppressWarnings("unchecked") 097 default U baseUnit() { 098 return (U) unit().getBaseUnit(); 099 } 100 101 /** 102 * Absolute value of measure. 103 * 104 * @param unit unit to use 105 * @return the absolute value of this measure in the given unit 106 */ 107 default double abs(U unit) { 108 return Math.abs(this.in(unit)); 109 } 110 111 /** 112 * Take the sign of another measure. 113 * 114 * @param other measure from which to take sign 115 * @param unit unit to use 116 * @return the value of the measure in the given unit with the sign of the provided measure 117 */ 118 default double copySign(Measure<U> other, U unit) { 119 return Math.copySign(this.in(unit), other.in(unit)); 120 } 121 122 /** 123 * Returns a measure equivalent to this one equal to zero minus its current value. For non-linear 124 * unit types like temperature, the zero point is treated as the zero value of the base unit (eg 125 * Kelvin). In effect, this means code like {@code Celsius.of(10).unaryMinus()} returns a value 126 * equivalent to -10 Kelvin, and <i>not</i> -10° Celsius. 127 * 128 * @return a measure equal to zero minus this measure 129 */ 130 Measure<U> unaryMinus(); 131 132 /** 133 * Returns a measure equivalent to this one equal to zero minus its current value. For non-linear 134 * unit types like temperature, the zero point is treated as the zero value of the base unit (eg 135 * Kelvin). In effect, this means code like {@code Celsius.of(10).negate()} returns a value 136 * equivalent to -10 Kelvin, and <i>not</i> -10° Celsius. 137 * 138 * @return a measure equal to zero minus this measure 139 * @deprecated use unaryMinus() instead. This was renamed for consistency with other WPILib 140 * classes like Rotation2d 141 */ 142 @Deprecated(since = "2025", forRemoval = true) 143 default Measure<U> negate() { 144 return unaryMinus(); 145 } 146 147 /** 148 * Adds another measure of the same unit type to this one. 149 * 150 * @param other the measurement to add 151 * @return a measure of the sum of both measures 152 */ 153 Measure<U> plus(Measure<? extends U> other); 154 155 /** 156 * Subtracts another measure of the same unit type from this one. 157 * 158 * @param other the measurement to subtract 159 * @return a measure of the difference between the measures 160 */ 161 Measure<U> minus(Measure<? extends U> other); 162 163 /** 164 * Multiplies this measure by a scalar unitless multiplier. 165 * 166 * @param multiplier the scalar multiplication factor 167 * @return the scaled result 168 */ 169 Measure<U> times(double multiplier); 170 171 /** 172 * Multiplies this measure by a scalar dimensionless multiplier. 173 * 174 * @param multiplier the scalar multiplication factor 175 * @return the scaled result 176 */ 177 Measure<U> times(Dimensionless multiplier); 178 179 /** 180 * Generates a new measure that is equal to this measure multiplied by another. Some dimensional 181 * analysis is performed to reduce the units down somewhat; for example, multiplying a {@code 182 * Measure<Time>} by a {@code Measure<Velocity<Distance>>} will return just a {@code 183 * Measure<Distance>} instead of the naive {@code Measure<Mult<Time, Velocity<Distance>>}. This is 184 * not guaranteed to perform perfect dimensional analysis. 185 * 186 * @param multiplier the unit to multiply by 187 * @return the multiplicative unit 188 */ 189 default Measure<?> times(Measure<?> multiplier) { 190 final double baseUnitResult = baseUnitMagnitude() * multiplier.baseUnitMagnitude(); 191 192 // First try to eliminate any common units 193 if (unit() instanceof PerUnit<?, ?> ratio 194 && multiplier.baseUnit().equals(ratio.denominator().getBaseUnit())) { 195 // Per<N, D> * D -> yield N 196 // Case 1: denominator of the Per cancels out, return with just the units of the numerator 197 return ratio.numerator().ofBaseUnits(baseUnitResult); 198 } else if (multiplier.baseUnit() instanceof PerUnit<?, ?> ratio 199 && baseUnit().equals(ratio.denominator().getBaseUnit())) { 200 // D * Per<N, D) -> yield N 201 // Case 2: Same as Case 1, just flipped between this and the multiplier 202 return ratio.numerator().ofBaseUnits(baseUnitResult); 203 } else if (unit() instanceof PerUnit<?, ?> per 204 && multiplier.unit() instanceof PerUnit<?, ?> otherPer 205 && per.denominator().getBaseUnit().equals(otherPer.numerator().getBaseUnit()) 206 && per.numerator().getBaseUnit().equals(otherPer.denominator().getBaseUnit())) { 207 // multiplying eg meters per second * milliseconds per foot 208 // return a scalar 209 return Value.of(baseUnitResult); 210 } 211 212 // No common units to eliminate, is one of them dimensionless? 213 // Note that this must come *after* the multiplier cases, otherwise 214 // Per<U, Dimensionless> * Dimensionless will not return a U 215 if (multiplier.unit() instanceof DimensionlessUnit) { 216 // scalar multiplication of this 217 return times(multiplier.baseUnitMagnitude()); 218 } else if (unit() instanceof DimensionlessUnit) { 219 // scalar multiplication of multiplier 220 return multiplier.times(baseUnitMagnitude()); 221 } 222 223 if (multiplier instanceof Acceleration<?> acceleration) { 224 return times(acceleration); 225 } else if (multiplier instanceof Angle angle) { 226 return times(angle); 227 } else if (multiplier instanceof AngularAcceleration angularAcceleration) { 228 return times(angularAcceleration); 229 } else if (multiplier instanceof AngularMomentum angularMomentum) { 230 return times(angularMomentum); 231 } else if (multiplier instanceof AngularVelocity angularVelocity) { 232 return times(angularVelocity); 233 } else if (multiplier instanceof Current current) { 234 return times(current); 235 } else if (multiplier instanceof Dimensionless dimensionless) { 236 // n.b. this case should already be covered 237 return times(dimensionless); 238 } else if (multiplier instanceof Distance distance) { 239 return times(distance); 240 } else if (multiplier instanceof Energy energy) { 241 return times(energy); 242 } else if (multiplier instanceof Force force) { 243 return times(force); 244 } else if (multiplier instanceof Frequency frequency) { 245 return times(frequency); 246 } else if (multiplier instanceof LinearAcceleration linearAcceleration) { 247 return times(linearAcceleration); 248 } else if (multiplier instanceof LinearVelocity linearVelocity) { 249 return times(linearVelocity); 250 } else if (multiplier instanceof Mass mass) { 251 return times(mass); 252 } else if (multiplier instanceof MomentOfInertia momentOfInertia) { 253 return times(momentOfInertia); 254 } else if (multiplier instanceof Mult<?, ?> mult) { 255 return times(mult); 256 } else if (multiplier instanceof Per<?, ?> per) { 257 return times(per); 258 } else if (multiplier instanceof Power power) { 259 return times(power); 260 } else if (multiplier instanceof Temperature temperature) { 261 return times(temperature); 262 } else if (multiplier instanceof Time time) { 263 return times(time); 264 } else if (multiplier instanceof Torque torque) { 265 return times(torque); 266 } else if (multiplier instanceof Velocity<?> velocity) { 267 return times(velocity); 268 } else if (multiplier instanceof Voltage voltage) { 269 return times(voltage); 270 } else if (multiplier instanceof Resistance resistance) { 271 return times(resistance); 272 } else { 273 // Dimensional analysis fallthrough or a generic input measure type 274 // Do a basic unit multiplication 275 return MultUnit.combine(unit(), multiplier.unit()).ofBaseUnits(baseUnitResult); 276 } 277 } 278 279 /** 280 * Multiplies this measure by an acceleration and returns the resulting measure in the most 281 * appropriate unit. 282 * 283 * @param multiplier the measurement to multiply by. 284 * @return the multiplication result 285 */ 286 default Measure<?> times(Acceleration<?> multiplier) { 287 return MultUnit.combine(unit(), multiplier.unit()) 288 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 289 } 290 291 /** 292 * Multiplies this measure by an angle and returns the resulting measure in the most appropriate 293 * unit. 294 * 295 * @param multiplier the measurement to multiply by. 296 * @return the multiplication result 297 */ 298 default Measure<?> times(Angle multiplier) { 299 return MultUnit.combine(unit(), multiplier.unit()) 300 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 301 } 302 303 /** 304 * Multiplies this measure by an angular acceleration and returns the resulting measure in the 305 * most appropriate unit. 306 * 307 * @param multiplier the measurement to multiply by. 308 * @return the multiplication result 309 */ 310 default Measure<?> times(AngularAcceleration multiplier) { 311 return MultUnit.combine(unit(), multiplier.unit()) 312 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 313 } 314 315 /** 316 * Multiplies this measure by an angular momentum and returns the resulting measure in the most 317 * appropriate unit. 318 * 319 * @param multiplier the measurement to multiply by. 320 * @return the multiplication result 321 */ 322 default Measure<?> times(AngularMomentum multiplier) { 323 return MultUnit.combine(unit(), multiplier.unit()) 324 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 325 } 326 327 /** 328 * Multiplies this measure by an angular velocity and returns the resulting measure in the most 329 * appropriate unit. 330 * 331 * @param multiplier the measurement to multiply by. 332 * @return the multiplication result 333 */ 334 default Measure<?> times(AngularVelocity multiplier) { 335 return MultUnit.combine(unit(), multiplier.unit()) 336 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 337 } 338 339 /** 340 * Multiplies this measure by an electric current and returns the resulting measure in the most 341 * appropriate unit. 342 * 343 * @param multiplier the measurement to multiply by. 344 * @return the multiplication result 345 */ 346 default Measure<?> times(Current multiplier) { 347 return MultUnit.combine(unit(), multiplier.unit()) 348 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 349 } 350 351 /** 352 * Multiplies this measure by a distance and returns the resulting measure in the most appropriate 353 * unit. 354 * 355 * @param multiplier the measurement to multiply by. 356 * @return the multiplication result 357 */ 358 default Measure<?> times(Distance multiplier) { 359 return MultUnit.combine(unit(), multiplier.unit()) 360 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 361 } 362 363 /** 364 * Multiplies this measure by an energy and returns the resulting measure in the most appropriate 365 * unit. 366 * 367 * @param multiplier the measurement to multiply by. 368 * @return the multiplication result 369 */ 370 default Measure<?> times(Energy multiplier) { 371 return MultUnit.combine(unit(), multiplier.unit()) 372 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 373 } 374 375 /** 376 * Multiplies this measure by a force and returns the resulting measure in the most appropriate 377 * unit. 378 * 379 * @param multiplier the measurement to multiply by. 380 * @return the multiplication result 381 */ 382 default Measure<?> times(Force multiplier) { 383 return MultUnit.combine(unit(), multiplier.unit()) 384 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 385 } 386 387 /** 388 * Multiplies this measure by a frequency and returns the resulting measure in the most 389 * appropriate unit. This often - but not always - means implementations return a variation of a 390 * {@link Per} measure. 391 * 392 * @param multiplier the measurement to multiply by. 393 * @return the multiplication result 394 */ 395 default Measure<?> times(Frequency multiplier) { 396 return MultUnit.combine(unit(), multiplier.unit()) 397 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 398 } 399 400 /** 401 * Multiplies this measure by a linear acceleration and returns the resulting measure in the most 402 * appropriate unit. 403 * 404 * @param multiplier the measurement to multiply by. 405 * @return the multiplication result 406 */ 407 default Measure<?> times(LinearAcceleration multiplier) { 408 return MultUnit.combine(unit(), multiplier.unit()) 409 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 410 } 411 412 /** 413 * Multiplies this measure by a linear momentum and returns the resulting measure in the most 414 * appropriate unit. 415 * 416 * @param multiplier the measurement to multiply by. 417 * @return the multiplication result 418 */ 419 default Measure<?> times(LinearMomentum multiplier) { 420 return MultUnit.combine(unit(), multiplier.unit()) 421 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 422 } 423 424 /** 425 * Multiplies this measure by a linear velocity and returns the resulting measure in the most 426 * appropriate unit. 427 * 428 * @param multiplier the measurement to multiply by. 429 * @return the multiplication result 430 */ 431 default Measure<?> times(LinearVelocity multiplier) { 432 return MultUnit.combine(unit(), multiplier.unit()) 433 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 434 } 435 436 /** 437 * Multiplies this measure by a mass and returns the resulting measure in the most appropriate 438 * unit. 439 * 440 * @param multiplier the measurement to multiply by. 441 * @return the multiplication result 442 */ 443 default Measure<?> times(Mass multiplier) { 444 return MultUnit.combine(unit(), multiplier.unit()) 445 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 446 } 447 448 /** 449 * Multiplies this measure by a moment of intertia and returns the resulting measure in the most 450 * appropriate unit. 451 * 452 * @param multiplier the measurement to multiply by. 453 * @return the multiplication result 454 */ 455 default Measure<?> times(MomentOfInertia multiplier) { 456 return MultUnit.combine(unit(), multiplier.unit()) 457 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 458 } 459 460 /** 461 * Multiplies this measure by a generic multiplied measure and returns the resulting measure in 462 * the most appropriate unit. 463 * 464 * @param multiplier the measurement to multiply by. 465 * @return the multiplication result 466 */ 467 default Measure<?> times(Mult<?, ?> multiplier) { 468 return MultUnit.combine(unit(), multiplier.unit()) 469 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 470 } 471 472 /** 473 * Multiplies this measure by a power and returns the resulting measure in the most appropriate 474 * unit. 475 * 476 * @param multiplier the measurement to multiply by. 477 * @return the multiplication result 478 */ 479 default Measure<?> times(Power multiplier) { 480 return MultUnit.combine(unit(), multiplier.unit()) 481 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 482 } 483 484 /** 485 * Multiplies this measure by a generic ratio measurement and returns the resulting measure in the 486 * most appropriate unit. 487 * 488 * @param multiplier the measurement to multiply by. 489 * @return the multiplication result 490 */ 491 default Measure<?> times(Per<?, ?> multiplier) { 492 return MultUnit.combine(unit(), multiplier.unit()) 493 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 494 } 495 496 /** 497 * Multiplies this measure by a temperature and returns the resulting measure in the most 498 * appropriate unit. 499 * 500 * @param multiplier the measurement to multiply by. 501 * @return the multiplication result 502 */ 503 default Measure<?> times(Temperature multiplier) { 504 return MultUnit.combine(unit(), multiplier.unit()) 505 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 506 } 507 508 /** 509 * Multiplies this measure by a time and returns the resulting measure in the most appropriate 510 * unit. 511 * 512 * @param multiplier the measurement to multiply by. 513 * @return the multiplication result 514 */ 515 default Measure<?> times(Time multiplier) { 516 return MultUnit.combine(unit(), multiplier.unit()) 517 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 518 } 519 520 /** 521 * Multiplies this measure by a torque and returns the resulting measure in the most appropriate 522 * unit. 523 * 524 * @param multiplier the measurement to multiply by. 525 * @return the multiplication result 526 */ 527 default Measure<?> times(Torque multiplier) { 528 return MultUnit.combine(unit(), multiplier.unit()) 529 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 530 } 531 532 /** 533 * Multiplies this measure by a generic velocity and returns the resulting measure in the most 534 * appropriate unit. 535 * 536 * @param multiplier the measurement to multiply by. 537 * @return the multiplication result 538 */ 539 default Measure<?> times(Velocity<?> multiplier) { 540 return MultUnit.combine(unit(), multiplier.unit()) 541 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 542 } 543 544 /** 545 * Multiplies this measure by a voltage and returns the resulting measure in the most appropriate 546 * unit. 547 * 548 * @param multiplier the measurement to multiply by. 549 * @return the multiplication result 550 */ 551 default Measure<?> times(Voltage multiplier) { 552 return MultUnit.combine(unit(), multiplier.unit()) 553 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 554 } 555 556 /** 557 * Multiplies this measure by a resistance and returns the resulting measure in the most 558 * appropriate unit. 559 * 560 * @param multiplier the measurement to multiply by. 561 * @return the multiplication result 562 */ 563 default Measure<?> times(Resistance multiplier) { 564 return MultUnit.combine(unit(), multiplier.unit()) 565 .ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 566 } 567 568 /** 569 * Multiplies this measure by a conversion factor, returning the converted measurement. Unlike 570 * {@link #times(Per)}, this allows for basic unit cancellation to return measurements of a known 571 * dimension. 572 * 573 * @param conversionFactor the conversion factor by which to multiply 574 * @param <Other> the unit type to convert to 575 * @param <M> the concrete return unit type. <strong>Note: the conversion factor's numerator unit 576 * must return instances of this type from {@link Unit#ofBaseUnits(double)}}</strong> 577 * @return the converted result 578 */ 579 @SuppressWarnings("unchecked") 580 default <Other extends Unit, M extends Measure<Other>> M timesConversionFactor( 581 Measure<? extends PerUnit<Other, U>> conversionFactor) { 582 return (M) 583 conversionFactor 584 .unit() 585 .getBaseUnit() 586 .numerator() 587 .ofBaseUnits(baseUnitMagnitude() * conversionFactor.baseUnitMagnitude()); 588 } 589 590 /** 591 * Multiplies this measure by another measurement of the inverse unit type (eg Time multiplied by 592 * Frequency) and returns the resulting dimensionless measure. 593 * 594 * @param multiplier the measurement to multiply by. 595 * @return the multiplication result 596 */ 597 default Dimensionless timesInverse( 598 Measure<? extends PerUnit<DimensionlessUnit, ? extends U>> multiplier) { 599 return Value.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude()); 600 } 601 602 /** 603 * Multiplies this measure by another measurement of something over the unit type (eg Time 604 * multiplied by LinearVelocity) and returns the resulting measure of the ratio's dividend unit. 605 * 606 * @param ratio the measurement to multiply by. 607 * @param <Other> other unit that the results are in terms of 608 * @return the multiplication result 609 */ 610 default <Other extends Unit> Measure<Other> timesRatio( 611 Measure<? extends PerUnit<? extends Other, U>> ratio) { 612 return ImmutableMeasure.ofBaseUnits( 613 baseUnitMagnitude() * ratio.baseUnitMagnitude(), ratio.unit().numerator()); 614 } 615 616 /** 617 * Divides this measure by a unitless scalar and returns the result. 618 * 619 * @param divisor the measurement to divide by. 620 * @return the division result 621 */ 622 Measure<U> div(double divisor); 623 624 /** 625 * Divides this measure by a dimensionless scalar and returns the result. 626 * 627 * @param divisor the measurement to divide by. 628 * @return the division result 629 */ 630 Measure<U> div(Dimensionless divisor); 631 632 /** 633 * Divides this measurement by another measure and performs some dimensional analysis to reduce 634 * the units. 635 * 636 * @param divisor the unit to divide by 637 * @return the resulting measure 638 */ 639 default Measure<?> div(Measure<?> divisor) { 640 final double baseUnitResult = baseUnitMagnitude() / divisor.baseUnitMagnitude(); 641 642 if (unit().getBaseUnit().equals(divisor.unit().getBaseUnit())) { 643 // These are the same unit, return a dimensionless 644 return Value.ofBaseUnits(baseUnitResult); 645 } 646 647 if (divisor instanceof Dimensionless) { 648 // Dividing by a dimensionless, do a scalar division 649 return unit().ofBaseUnits(baseUnitResult); 650 } 651 652 // Numerator is a dimensionless 653 if (unit() instanceof DimensionlessUnit && divisor.unit() instanceof PerUnit<?, ?> ratio) { 654 // Dividing by a ratio, return its reciprocal scaled by this 655 return ratio.reciprocal().ofBaseUnits(baseUnitResult); 656 } 657 658 if (divisor.unit() instanceof PerUnit<?, ?> ratio 659 && ratio.numerator().getBaseUnit().equals(baseUnit())) { 660 // Numerator of the ratio cancels out 661 // N / (N / D) -> N * D / N -> D 662 // Note: all Velocity and Acceleration units inherit from PerUnit 663 return ratio.denominator().ofBaseUnits(baseUnitResult); 664 } 665 666 if (divisor.unit() instanceof TimeUnit time) { 667 // Dividing by a time, return a Velocity 668 return VelocityUnit.combine(unit(), time).ofBaseUnits(baseUnitResult); 669 } 670 671 if (divisor instanceof Acceleration<?> acceleration) { 672 return div(acceleration); 673 } else if (divisor instanceof Angle angle) { 674 return div(angle); 675 } else if (divisor instanceof AngularAcceleration angularAcceleration) { 676 return div(angularAcceleration); 677 } else if (divisor instanceof AngularMomentum angularMomentum) { 678 return div(angularMomentum); 679 } else if (divisor instanceof AngularVelocity angularVelocity) { 680 return div(angularVelocity); 681 } else if (divisor instanceof Current current) { 682 return div(current); 683 } else if (divisor instanceof Dimensionless dimensionless) { 684 // n.b. this case should already be covered 685 return div(dimensionless); 686 } else if (divisor instanceof Distance distance) { 687 return div(distance); 688 } else if (divisor instanceof Energy energy) { 689 return div(energy); 690 } else if (divisor instanceof Force force) { 691 return div(force); 692 } else if (divisor instanceof Frequency frequency) { 693 return div(frequency); 694 } else if (divisor instanceof LinearAcceleration linearAcceleration) { 695 return div(linearAcceleration); 696 } else if (divisor instanceof LinearVelocity linearVelocity) { 697 return div(linearVelocity); 698 } else if (divisor instanceof Mass mass) { 699 return div(mass); 700 } else if (divisor instanceof MomentOfInertia momentOfInertia) { 701 return div(momentOfInertia); 702 } else if (divisor instanceof Mult<?, ?> mult) { 703 return div(mult); 704 } else if (divisor instanceof Per<?, ?> per) { 705 return div(per); 706 } else if (divisor instanceof Power power) { 707 return div(power); 708 } else if (divisor instanceof Temperature temperature) { 709 return div(temperature); 710 } else if (divisor instanceof Time time) { 711 return div(time); 712 } else if (divisor instanceof Torque torque) { 713 return div(torque); 714 } else if (divisor instanceof Velocity<?> velocity) { 715 return div(velocity); 716 } else if (divisor instanceof Voltage voltage) { 717 return div(voltage); 718 } else if (divisor instanceof Resistance resistance) { 719 return div(resistance); 720 } else { 721 // Dimensional analysis fallthrough or a generic input measure type 722 // Do a basic unit multiplication 723 return PerUnit.combine(unit(), divisor.unit()).ofBaseUnits(baseUnitResult); 724 } 725 } 726 727 /** 728 * Divides this measure by a generic acceleration and returns the result in the most appropriate 729 * unit. 730 * 731 * @param divisor the measurement to divide by. 732 * @return the division result 733 */ 734 default Measure<?> div(Acceleration<?> divisor) { 735 return PerUnit.combine(unit(), divisor.unit()) 736 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 737 } 738 739 /** 740 * Divides this measure by an angle and returns the result in the most appropriate unit. 741 * 742 * @param divisor the measurement to divide by. 743 * @return the division result 744 */ 745 default Measure<?> div(Angle divisor) { 746 return PerUnit.combine(unit(), divisor.unit()) 747 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 748 } 749 750 /** 751 * Divides this measure by an angular acceleration and returns the result in the most appropriate 752 * unit. 753 * 754 * @param divisor the measurement to divide by. 755 * @return the division result 756 */ 757 default Measure<?> div(AngularAcceleration divisor) { 758 return PerUnit.combine(unit(), divisor.unit()) 759 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 760 } 761 762 /** 763 * Divides this measure by an angular momentum and returns the result in the most appropriate 764 * unit. 765 * 766 * @param divisor the measurement to divide by. 767 * @return the division result 768 */ 769 default Measure<?> div(AngularMomentum divisor) { 770 return PerUnit.combine(unit(), divisor.unit()) 771 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 772 } 773 774 /** 775 * Divides this measure by an angular velocity and returns the result in the most appropriate 776 * unit. 777 * 778 * @param divisor the measurement to divide by. 779 * @return the division result 780 */ 781 default Measure<?> div(AngularVelocity divisor) { 782 return PerUnit.combine(unit(), divisor.unit()) 783 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 784 } 785 786 /** 787 * Divides this measure by an electric current and returns the result in the most appropriate 788 * unit. 789 * 790 * @param divisor the measurement to divide by. 791 * @return the division result 792 */ 793 default Measure<?> div(Current divisor) { 794 return PerUnit.combine(unit(), divisor.unit()) 795 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 796 } 797 798 /** 799 * Divides this measure by a distance and returns the result in the most appropriate unit. 800 * 801 * @param divisor the measurement to divide by. 802 * @return the division result 803 */ 804 default Measure<?> div(Distance divisor) { 805 return PerUnit.combine(unit(), divisor.unit()) 806 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 807 } 808 809 /** 810 * Divides this measure by an energy and returns the result in the most appropriate unit. 811 * 812 * @param divisor the measurement to divide by. 813 * @return the division result 814 */ 815 default Measure<?> div(Energy divisor) { 816 return PerUnit.combine(unit(), divisor.unit()) 817 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 818 } 819 820 /** 821 * Divides this measure by a force and returns the result in the most appropriate unit. 822 * 823 * @param divisor the measurement to divide by. 824 * @return the division result 825 */ 826 default Measure<?> div(Force divisor) { 827 return PerUnit.combine(unit(), divisor.unit()) 828 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 829 } 830 831 /** 832 * Divides this measure by a frequency and returns the result in the most appropriate unit. 833 * 834 * @param divisor the measurement to divide by. 835 * @return the division result 836 */ 837 default Measure<?> div(Frequency divisor) { 838 return PerUnit.combine(unit(), divisor.unit()) 839 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 840 } 841 842 /** 843 * Divides this measure by a linear acceleration and returns the result in the most appropriate 844 * unit. 845 * 846 * @param divisor the measurement to divide by. 847 * @return the division result 848 */ 849 default Measure<?> div(LinearAcceleration divisor) { 850 return PerUnit.combine(unit(), divisor.unit()) 851 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 852 } 853 854 /** 855 * Divides this measure by a linear momentum and returns the result in the most appropriate unit. 856 * 857 * @param divisor the measurement to divide by. 858 * @return the division result 859 */ 860 default Measure<?> div(LinearMomentum divisor) { 861 return PerUnit.combine(unit(), divisor.unit()) 862 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 863 } 864 865 /** 866 * Divides this measure by a linear velocity and returns the result in the most appropriate unit. 867 * 868 * @param divisor the measurement to divide by. 869 * @return the division result 870 */ 871 default Measure<?> div(LinearVelocity divisor) { 872 return PerUnit.combine(unit(), divisor.unit()) 873 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 874 } 875 876 /** 877 * Divides this measure by a mass and returns the result in the most appropriate unit. 878 * 879 * @param divisor the measurement to divide by. 880 * @return the division result 881 */ 882 default Measure<?> div(Mass divisor) { 883 return PerUnit.combine(unit(), divisor.unit()) 884 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 885 } 886 887 /** 888 * Divides this measure by a moment of inertia and returns the result in the most appropriate 889 * unit. 890 * 891 * @param divisor the measurement to divide by. 892 * @return the division result 893 */ 894 default Measure<?> div(MomentOfInertia divisor) { 895 return PerUnit.combine(unit(), divisor.unit()) 896 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 897 } 898 899 /** 900 * Divides this measure by a generic multiplication and returns the result in the most appropriate 901 * unit. 902 * 903 * @param divisor the measurement to divide by. 904 * @return the division result 905 */ 906 default Measure<?> div(Mult<?, ?> divisor) { 907 return PerUnit.combine(unit(), divisor.unit()) 908 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 909 } 910 911 /** 912 * Divides this measure by a power and returns the result in the most appropriate unit. 913 * 914 * @param divisor the measurement to divide by. 915 * @return the division result 916 */ 917 default Measure<?> div(Power divisor) { 918 return PerUnit.combine(unit(), divisor.unit()) 919 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 920 } 921 922 /** 923 * Divides this measure by a generic ratio and returns the result in the most appropriate unit. 924 * 925 * @param divisor the measurement to divide by. 926 * @return the division result 927 */ 928 default Measure<?> div(Per<?, ?> divisor) { 929 return PerUnit.combine(unit(), divisor.unit()) 930 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 931 } 932 933 /** 934 * Divides this measure by a temperature and returns the result in the most appropriate unit. 935 * 936 * @param divisor the measurement to divide by. 937 * @return the division result 938 */ 939 default Measure<?> div(Temperature divisor) { 940 return PerUnit.combine(unit(), divisor.unit()) 941 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 942 } 943 944 /** 945 * Divides this measure by a time and returns the result in the most appropriate unit. This will 946 * often - but not always - result in a {@link Per} type like {@link LinearVelocity} or {@link 947 * Acceleration}. 948 * 949 * @param divisor the measurement to divide by. 950 * @return the division result 951 */ 952 default Measure<?> div(Time divisor) { 953 return VelocityUnit.combine(unit(), divisor.unit()) 954 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 955 } 956 957 /** 958 * Divides this measure by a torque and returns the result in the most appropriate unit. 959 * 960 * @param divisor the measurement to divide by. 961 * @return the division result 962 */ 963 default Measure<?> div(Torque divisor) { 964 return PerUnit.combine(unit(), divisor.unit()) 965 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 966 } 967 968 /** 969 * Divides this measure by a generic velocity and returns the result in the most appropriate unit. 970 * 971 * @param divisor the measurement to divide by. 972 * @return the division result 973 */ 974 default Measure<?> div(Velocity<?> divisor) { 975 return PerUnit.combine(unit(), divisor.unit()) 976 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 977 } 978 979 /** 980 * Divides this measure by a voltage and returns the result in the most appropriate unit. 981 * 982 * @param divisor the measurement to divide by. 983 * @return the division result 984 */ 985 default Measure<?> div(Voltage divisor) { 986 return PerUnit.combine(unit(), divisor.unit()) 987 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 988 } 989 990 /** 991 * Divides this measure by a resistance and returns the result in the most appropriate unit. 992 * 993 * @param divisor the measurement to divide by. 994 * @return the division result 995 */ 996 default Measure<?> div(Resistance divisor) { 997 return PerUnit.combine(unit(), divisor.unit()) 998 .ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude()); 999 } 1000 1001 /** 1002 * Divides this measure by an acceleration unit and returns the result in the most appropriate 1003 * unit. This is equivalent to {@code div(divisorUnit.of(1))}. 1004 * 1005 * @param divisorUnit the unit to divide by. 1006 * @return the division result 1007 */ 1008 default Measure<?> per(AccelerationUnit<?> divisorUnit) { 1009 return div(divisorUnit.one()); 1010 } 1011 1012 /** 1013 * Divides this measure by an angle unit and returns the result in the most appropriate unit. This 1014 * is equivalent to {@code div(divisorUnit.of(1))}. 1015 * 1016 * @param divisorUnit the unit to divide by. 1017 * @return the division result 1018 */ 1019 default Measure<?> per(AngleUnit divisorUnit) { 1020 return div(divisorUnit.one()); 1021 } 1022 1023 /** 1024 * Divides this measure by an angular acceleration unit and returns the result in the most 1025 * appropriate unit. This is equivalent to {@code div(divisorUnit.of(1))}. 1026 * 1027 * @param divisorUnit the unit to divide by. 1028 * @return the division result 1029 */ 1030 default Measure<?> per(AngularAccelerationUnit divisorUnit) { 1031 return div(divisorUnit.one()); 1032 } 1033 1034 /** 1035 * Divides this measure by an angular momentum unit and returns the result in the most appropriate 1036 * unit. This is equivalent to {@code div(divisorUnit.of(1))}. 1037 * 1038 * @param divisorUnit the unit to divide by. 1039 * @return the division result 1040 */ 1041 default Measure<?> per(AngularMomentumUnit divisorUnit) { 1042 return div(divisorUnit.one()); 1043 } 1044 1045 /** 1046 * Divides this measure by an angular velocity unit and returns the result in the most appropriate 1047 * unit. This is equivalent to {@code div(divisorUnit.of(1))}. 1048 * 1049 * @param divisorUnit the unit to divide by. 1050 * @return the division result 1051 */ 1052 default Measure<?> per(AngularVelocityUnit divisorUnit) { 1053 return div(divisorUnit.one()); 1054 } 1055 1056 /** 1057 * Divides this measure by a unit of electric current and returns the result in the most 1058 * appropriate unit. This is equivalent to {@code div(divisorUnit.of(1))}. 1059 * 1060 * @param divisorUnit the unit to divide by. 1061 * @return the division result 1062 */ 1063 default Measure<?> per(CurrentUnit divisorUnit) { 1064 return div(divisorUnit.one()); 1065 } 1066 1067 /** 1068 * Divides this measure by a distance unit and returns the result in the most appropriate unit. 1069 * This is equivalent to {@code div(divisorUnit.of(1))}. 1070 * 1071 * @param divisorUnit the unit to divide by. 1072 * @return the division result 1073 */ 1074 default Measure<?> per(DistanceUnit divisorUnit) { 1075 return div(divisorUnit.one()); 1076 } 1077 1078 /** 1079 * Divides this measure by an energy unit and returns the result in the most appropriate unit. 1080 * This is equivalent to {@code div(divisorUnit.of(1))}. 1081 * 1082 * @param divisorUnit the unit to divide by. 1083 * @return the division result 1084 */ 1085 default Measure<?> per(EnergyUnit divisorUnit) { 1086 return div(divisorUnit.one()); 1087 } 1088 1089 /** 1090 * Divides this measure by a force unit and returns the result in the most appropriate unit. This 1091 * is equivalent to {@code div(divisorUnit.of(1))}. 1092 * 1093 * @param divisorUnit the unit to divide by. 1094 * @return the division result 1095 */ 1096 default Measure<?> per(ForceUnit divisorUnit) { 1097 return div(divisorUnit.one()); 1098 } 1099 1100 /** 1101 * Divides this measure by a frequency unit and returns the result in the most appropriate unit. 1102 * This is equivalent to {@code div(divisorUnit.of(1))}. 1103 * 1104 * @param divisorUnit the unit to divide by. 1105 * @return the division result 1106 */ 1107 default Measure<?> per(FrequencyUnit divisorUnit) { 1108 return div(divisorUnit.one()); 1109 } 1110 1111 /** 1112 * Divides this measure by a linear acceleration unit and returns the result in the most 1113 * appropriate unit. This is equivalent to {@code div(divisorUnit.of(1))}. 1114 * 1115 * @param divisorUnit the unit to divide by. 1116 * @return the division result 1117 */ 1118 default Measure<?> per(LinearAccelerationUnit divisorUnit) { 1119 return div(divisorUnit.one()); 1120 } 1121 1122 /** 1123 * Divides this measure by a linear momentum unit and returns the result in the most appropriate 1124 * unit. This is equivalent to {@code div(divisorUnit.of(1))}. 1125 * 1126 * @param divisorUnit the unit to divide by. 1127 * @return the division result 1128 */ 1129 default Measure<?> per(LinearMomentumUnit divisorUnit) { 1130 return div(divisorUnit.one()); 1131 } 1132 1133 /** 1134 * Divides this measure by a linear velocity unit and returns the result in the most appropriate 1135 * unit. This is equivalent to {@code div(divisorUnit.of(1))}. 1136 * 1137 * @param divisorUnit the unit to divide by. 1138 * @return the division result 1139 */ 1140 default Measure<?> per(LinearVelocityUnit divisorUnit) { 1141 return div(divisorUnit.one()); 1142 } 1143 1144 /** 1145 * Divides this measure by a mass unit and returns the result in the most appropriate unit. This 1146 * is equivalent to {@code div(divisorUnit.of(1))}. 1147 * 1148 * @param divisorUnit the unit to divide by. 1149 * @return the division result 1150 */ 1151 default Measure<?> per(MassUnit divisorUnit) { 1152 return div(divisorUnit.one()); 1153 } 1154 1155 /** 1156 * Divides this measure by a moment of inertia unit and returns the result in the most appropriate 1157 * unit. This is equivalent to {@code div(divisorUnit.of(1))}. 1158 * 1159 * @param divisorUnit the unit to divide by. 1160 * @return the division result 1161 */ 1162 default Measure<?> per(MomentOfInertiaUnit divisorUnit) { 1163 return div(divisorUnit.one()); 1164 } 1165 1166 /** 1167 * Divides this measure by a generic compound unit and returns the result in the most appropriate 1168 * unit. This is equivalent to {@code div(divisorUnit.of(1))}. 1169 * 1170 * @param divisorUnit the unit to divide by. 1171 * @return the division result 1172 */ 1173 default Measure<?> per(MultUnit<?, ?> divisorUnit) { 1174 return div(divisorUnit.one()); 1175 } 1176 1177 /** 1178 * Divides this measure by a power unit and returns the result in the most appropriate unit. This 1179 * is equivalent to {@code div(divisorUnit.of(1))}. 1180 * 1181 * @param divisorUnit the unit to divide by. 1182 * @return the division result 1183 */ 1184 default Measure<?> per(PowerUnit divisorUnit) { 1185 return div(divisorUnit.one()); 1186 } 1187 1188 /** 1189 * Divides this measure by a generic ratio unit and returns the result in the most appropriate 1190 * unit. This is equivalent to {@code div(divisorUnit.of(1))}. 1191 * 1192 * @param divisorUnit the unit to divide by. 1193 * @return the division result 1194 */ 1195 default Measure<?> per(PerUnit<?, ?> divisorUnit) { 1196 return div(divisorUnit.one()); 1197 } 1198 1199 /** 1200 * Divides this measure by a temperature unit and returns the result in the most appropriate unit. 1201 * This is equivalent to {@code div(divisorUnit.of(1))}. 1202 * 1203 * @param divisorUnit the unit to divide by. 1204 * @return the division result 1205 */ 1206 default Measure<?> per(TemperatureUnit divisorUnit) { 1207 return div(divisorUnit.one()); 1208 } 1209 1210 /** 1211 * Divides this measure by a time period and returns the result in the most appropriate unit. This 1212 * is equivalent to {@code div(period.of(1))}. 1213 * 1214 * @param period the time period measurement to divide by. 1215 * @return the division result 1216 */ 1217 default Measure<?> per(TimeUnit period) { 1218 return div(period.of(1)); 1219 } 1220 1221 /** 1222 * Divides this measure by a torque unit and returns the result in the most appropriate unit. This 1223 * is equivalent to {@code div(divisorUnit.of(1))}. 1224 * 1225 * @param divisorUnit the unit to divide by. 1226 * @return the division result 1227 */ 1228 default Measure<?> per(TorqueUnit divisorUnit) { 1229 return div(divisorUnit.one()); 1230 } 1231 1232 /** 1233 * Divides this measure by a velocity unit and returns the result in the most appropriate unit. 1234 * This is equivalent to {@code div(divisorUnit.of(1))}. 1235 * 1236 * @param divisorUnit the unit to divide by. 1237 * @return the division result 1238 */ 1239 default Measure<?> per(VelocityUnit<?> divisorUnit) { 1240 return div(divisorUnit.one()); 1241 } 1242 1243 /** 1244 * Divides this measure by a voltage unit and returns the result in the most appropriate unit. 1245 * This is equivalent to {@code div(divisorUnit.of(1))}. 1246 * 1247 * @param divisorUnit the unit to divide by. 1248 * @return the division result 1249 */ 1250 default Measure<?> per(VoltageUnit divisorUnit) { 1251 return div(divisorUnit.one()); 1252 } 1253 1254 /** 1255 * Divides this measure by a resistance unit and returns the result in the most appropriate unit. 1256 * This is equivalent to {@code div(divisorUnit.of(1))}. 1257 * 1258 * @param divisorUnit the unit to divide by. 1259 * @return the division result 1260 */ 1261 default Measure<?> per(ResistanceUnit divisorUnit) { 1262 return div(divisorUnit.one()); 1263 } 1264 1265 /** 1266 * Divides this measure by a unitless scalar and returns the result. 1267 * 1268 * @param divisor the measurement to divide by. 1269 * @return the division result 1270 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1271 */ 1272 @Deprecated(since = "2025", forRemoval = true) 1273 default Measure<U> divide(double divisor) { 1274 return div(divisor); 1275 } 1276 1277 /** 1278 * Divides this measure by a dimensionless scalar and returns the result. 1279 * 1280 * @param divisor the measurement to divide by. 1281 * @return the division result 1282 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1283 */ 1284 @Deprecated(since = "2025", forRemoval = true) 1285 default Measure<U> divide(Dimensionless divisor) { 1286 return div(divisor); 1287 } 1288 1289 /** 1290 * Divides this measurement by another measure and performs some dimensional analysis to reduce 1291 * the units. 1292 * 1293 * @param divisor the unit to divide by 1294 * @return the resulting measure 1295 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1296 */ 1297 @Deprecated(since = "2025", forRemoval = true) 1298 default Measure<?> divide(Measure<?> divisor) { 1299 return div(divisor); 1300 } 1301 1302 /** 1303 * Divides this measure by a generic acceleration and returns the result in the most appropriate 1304 * unit. 1305 * 1306 * @param divisor the measurement to divide by. 1307 * @return the division result 1308 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1309 */ 1310 @Deprecated(since = "2025", forRemoval = true) 1311 default Measure<?> divide(Acceleration<?> divisor) { 1312 return div(divisor); 1313 } 1314 1315 /** 1316 * Divides this measure by an angle and returns the result in the most appropriate unit. 1317 * 1318 * @param divisor the measurement to divide by. 1319 * @return the division result 1320 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1321 */ 1322 @Deprecated(since = "2025", forRemoval = true) 1323 default Measure<?> divide(Angle divisor) { 1324 return div(divisor); 1325 } 1326 1327 /** 1328 * Divides this measure by an angular acceleration and returns the result in the most appropriate 1329 * unit. 1330 * 1331 * @param divisor the measurement to divide by. 1332 * @return the division result 1333 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1334 */ 1335 @Deprecated(since = "2025", forRemoval = true) 1336 default Measure<?> divide(AngularAcceleration divisor) { 1337 return div(divisor); 1338 } 1339 1340 /** 1341 * Divides this measure by an angular momentum and returns the result in the most appropriate 1342 * unit. 1343 * 1344 * @param divisor the measurement to divide by. 1345 * @return the division result 1346 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1347 */ 1348 @Deprecated(since = "2025", forRemoval = true) 1349 default Measure<?> divide(AngularMomentum divisor) { 1350 return div(divisor); 1351 } 1352 1353 /** 1354 * Divides this measure by an angular velocity and returns the result in the most appropriate 1355 * unit. 1356 * 1357 * @param divisor the measurement to divide by. 1358 * @return the division result 1359 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1360 */ 1361 @Deprecated(since = "2025", forRemoval = true) 1362 default Measure<?> divide(AngularVelocity divisor) { 1363 return div(divisor); 1364 } 1365 1366 /** 1367 * Divides this measure by an electric current and returns the result in the most appropriate 1368 * unit. 1369 * 1370 * @param divisor the measurement to divide by. 1371 * @return the division result 1372 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1373 */ 1374 @Deprecated(since = "2025", forRemoval = true) 1375 default Measure<?> divide(Current divisor) { 1376 return div(divisor); 1377 } 1378 1379 /** 1380 * Divides this measure by a distance and returns the result in the most appropriate unit. 1381 * 1382 * @param divisor the measurement to divide by. 1383 * @return the division result 1384 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1385 */ 1386 @Deprecated(since = "2025", forRemoval = true) 1387 default Measure<?> divide(Distance divisor) { 1388 return div(divisor); 1389 } 1390 1391 /** 1392 * Divides this measure by an energy and returns the result in the most appropriate unit. 1393 * 1394 * @param divisor the measurement to divide by. 1395 * @return the division result 1396 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1397 */ 1398 @Deprecated(since = "2025", forRemoval = true) 1399 default Measure<?> divide(Energy divisor) { 1400 return div(divisor); 1401 } 1402 1403 /** 1404 * Divides this measure by a force and returns the result in the most appropriate unit. 1405 * 1406 * @param divisor the measurement to divide by. 1407 * @return the division result 1408 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1409 */ 1410 @Deprecated(since = "2025", forRemoval = true) 1411 default Measure<?> divide(Force divisor) { 1412 return div(divisor); 1413 } 1414 1415 /** 1416 * Divides this measure by a frequency and returns the result in the most appropriate unit. 1417 * 1418 * @param divisor the measurement to divide by. 1419 * @return the division result 1420 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1421 */ 1422 @Deprecated(since = "2025", forRemoval = true) 1423 default Measure<?> divide(Frequency divisor) { 1424 return div(divisor); 1425 } 1426 1427 /** 1428 * Divides this measure by a linear acceleration and returns the result in the most appropriate 1429 * unit. 1430 * 1431 * @param divisor the measurement to divide by. 1432 * @return the division result 1433 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1434 */ 1435 @Deprecated(since = "2025", forRemoval = true) 1436 default Measure<?> divide(LinearAcceleration divisor) { 1437 return div(divisor); 1438 } 1439 1440 /** 1441 * Divides this measure by a linear momentum and returns the result in the most appropriate unit. 1442 * 1443 * @param divisor the measurement to divide by. 1444 * @return the division result 1445 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1446 */ 1447 @Deprecated(since = "2025", forRemoval = true) 1448 default Measure<?> divide(LinearMomentum divisor) { 1449 return div(divisor); 1450 } 1451 1452 /** 1453 * Divides this measure by a linear velocity and returns the result in the most appropriate unit. 1454 * 1455 * @param divisor the measurement to divide by. 1456 * @return the division result 1457 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1458 */ 1459 @Deprecated(since = "2025", forRemoval = true) 1460 default Measure<?> divide(LinearVelocity divisor) { 1461 return div(divisor); 1462 } 1463 1464 /** 1465 * Divides this measure by a mass and returns the result in the most appropriate unit. 1466 * 1467 * @param divisor the measurement to divide by. 1468 * @return the division result 1469 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1470 */ 1471 @Deprecated(since = "2025", forRemoval = true) 1472 default Measure<?> divide(Mass divisor) { 1473 return div(divisor); 1474 } 1475 1476 /** 1477 * Divides this measure by a moment of inertia and returns the result in the most appropriate 1478 * unit. 1479 * 1480 * @param divisor the measurement to divide by. 1481 * @return the division result 1482 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1483 */ 1484 @Deprecated(since = "2025", forRemoval = true) 1485 default Measure<?> divide(MomentOfInertia divisor) { 1486 return div(divisor); 1487 } 1488 1489 /** 1490 * Divides this measure by a generic multiplication and returns the result in the most appropriate 1491 * unit. 1492 * 1493 * @param divisor the measurement to divide by. 1494 * @return the division result 1495 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1496 */ 1497 @Deprecated(since = "2025", forRemoval = true) 1498 default Measure<?> divide(Mult<?, ?> divisor) { 1499 return div(divisor); 1500 } 1501 1502 /** 1503 * Divides this measure by a power and returns the result in the most appropriate unit. 1504 * 1505 * @param divisor the measurement to divide by. 1506 * @return the division result 1507 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1508 */ 1509 @Deprecated(since = "2025", forRemoval = true) 1510 default Measure<?> divide(Power divisor) { 1511 return div(divisor); 1512 } 1513 1514 /** 1515 * Divides this measure by a generic ratio and returns the result in the most appropriate unit. 1516 * 1517 * @param divisor the measurement to divide by. 1518 * @return the division result 1519 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1520 */ 1521 @Deprecated(since = "2025", forRemoval = true) 1522 default Measure<?> divide(Per<?, ?> divisor) { 1523 return div(divisor); 1524 } 1525 1526 /** 1527 * Divides this measure by a temperature and returns the result in the most appropriate unit. 1528 * 1529 * @param divisor the measurement to divide by. 1530 * @return the division result 1531 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1532 */ 1533 @Deprecated(since = "2025", forRemoval = true) 1534 default Measure<?> divide(Temperature divisor) { 1535 return div(divisor); 1536 } 1537 1538 /** 1539 * Divides this measure by a time and returns the result in the most appropriate unit. This will 1540 * often - but not always - result in a {@link Per} type like {@link LinearVelocity} or {@link 1541 * Acceleration}. 1542 * 1543 * @param divisor the measurement to divide by. 1544 * @return the division result 1545 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1546 */ 1547 @Deprecated(since = "2025", forRemoval = true) 1548 default Measure<?> divide(Time divisor) { 1549 return div(divisor); 1550 } 1551 1552 /** 1553 * Divides this measure by a torque and returns the result in the most appropriate unit. 1554 * 1555 * @param divisor the measurement to divide by. 1556 * @return the division result 1557 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1558 */ 1559 @Deprecated(since = "2025", forRemoval = true) 1560 default Measure<?> divide(Torque divisor) { 1561 return div(divisor); 1562 } 1563 1564 /** 1565 * Divides this measure by a generic velocity and returns the result in the most appropriate unit. 1566 * 1567 * @param divisor the measurement to divide by. 1568 * @return the division result 1569 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1570 */ 1571 @Deprecated(since = "2025", forRemoval = true) 1572 default Measure<?> divide(Velocity<?> divisor) { 1573 return div(divisor); 1574 } 1575 1576 /** 1577 * Divides this measure by a voltage and returns the result in the most appropriate unit. 1578 * 1579 * @param divisor the measurement to divide by. 1580 * @return the division result 1581 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1582 */ 1583 @Deprecated(since = "2025", forRemoval = true) 1584 default Measure<?> divide(Voltage divisor) { 1585 return div(divisor); 1586 } 1587 1588 /** 1589 * Divides this measure by a resistance and returns the result in the most appropriate unit. 1590 * 1591 * @param divisor the measurement to divide by. 1592 * @return the division result 1593 * @deprecated use div instead. This was renamed for consistency with other languages like Kotlin 1594 */ 1595 @Deprecated(since = "2025", forRemoval = true) 1596 default Measure<?> divide(Resistance divisor) { 1597 return div(divisor); 1598 } 1599 1600 /** 1601 * Divides this measure by a ratio in terms of this measurement's unit to another unit, returning 1602 * a measurement in terms of the other unit. 1603 * 1604 * @param divisor the measurement to divide by. 1605 * @param <Other> the other unit that results are in terms of 1606 * @return the division result 1607 */ 1608 // eg Dimensionless / (Dimensionless / Time) -> Time 1609 default <Other extends Unit> Measure<Other> divideRatio( 1610 Measure<? extends PerUnit<? extends U, Other>> divisor) { 1611 return ImmutableMeasure.ofBaseUnits( 1612 baseUnitMagnitude() / divisor.baseUnitMagnitude(), divisor.unit().denominator()); 1613 } 1614 1615 /** 1616 * Checks if this measure is near another measure of the same unit. Provide a variance threshold 1617 * for use for a +/- scalar, such as 0.05 for +/- 5%. 1618 * 1619 * <pre> 1620 * Inches.of(11).isNear(Inches.of(10), 0.1) // true 1621 * Inches.of(12).isNear(Inches.of(10), 0.1) // false 1622 * </pre> 1623 * 1624 * @param other the other measurement to compare against 1625 * @param varianceThreshold the acceptable variance threshold, in terms of an acceptable +/- error 1626 * range multiplier. Checking if a value is within 10% means a value of 0.1 should be passed; 1627 * checking if a value is within 1% means a value of 0.01 should be passed, and so on. 1628 * @return true if this unit is near the other measure, otherwise false 1629 */ 1630 default boolean isNear(Measure<?> other, double varianceThreshold) { 1631 if (!this.unit().getBaseUnit().equivalent(other.unit().getBaseUnit())) { 1632 return false; // Disjoint units, not compatible 1633 } 1634 1635 // abs so negative inputs are calculated correctly 1636 var tolerance = Math.abs(other.baseUnitMagnitude() * varianceThreshold); 1637 1638 return Math.abs(this.baseUnitMagnitude() - other.baseUnitMagnitude()) <= tolerance; 1639 } 1640 1641 /** 1642 * Checks if this measure is near another measure of the same unit, with a specified tolerance of 1643 * the same unit. 1644 * 1645 * <pre> 1646 * Meters.of(1).isNear(Meters.of(1.2), Millimeters.of(300)) // true 1647 * Degrees.of(90).isNear(Rotations.of(0.5), Degrees.of(45)) // false 1648 * </pre> 1649 * 1650 * @param other the other measure to compare against. 1651 * @param tolerance the tolerance allowed in which the two measures are defined as near each 1652 * other. 1653 * @return true if this unit is near the other measure, otherwise false. 1654 */ 1655 default boolean isNear(Measure<U> other, Measure<U> tolerance) { 1656 return Math.abs(this.baseUnitMagnitude() - other.baseUnitMagnitude()) 1657 <= Math.abs(tolerance.baseUnitMagnitude()); 1658 } 1659 1660 /** 1661 * Checks if this measure is equivalent to another measure of the same unit. 1662 * 1663 * @param other the measure to compare to 1664 * @return true if this measure is equivalent, false otherwise 1665 */ 1666 default boolean isEquivalent(Measure<?> other) { 1667 // Return false for disjoint units that aren't compatible 1668 return this.unit().getBaseUnit().equals(other.unit().getBaseUnit()) 1669 && Math.abs(baseUnitMagnitude() - other.baseUnitMagnitude()) <= EQUIVALENCE_THRESHOLD; 1670 } 1671 1672 /** {@inheritDoc} */ 1673 @Override 1674 default int compareTo(Measure<U> o) { 1675 return Double.compare(this.baseUnitMagnitude(), o.baseUnitMagnitude()); 1676 } 1677 1678 /** 1679 * Checks if this measure is greater than another measure of the same unit. 1680 * 1681 * @param o the other measure to compare to 1682 * @return true if this measure has a greater equivalent magnitude, false otherwise 1683 */ 1684 default boolean gt(Measure<U> o) { 1685 return compareTo(o) > 0; 1686 } 1687 1688 /** 1689 * Checks if this measure is greater than or equivalent to another measure of the same unit. 1690 * 1691 * @param o the other measure to compare to 1692 * @return true if this measure has an equal or greater equivalent magnitude, false otherwise 1693 */ 1694 default boolean gte(Measure<U> o) { 1695 return compareTo(o) > 0 || isEquivalent(o); 1696 } 1697 1698 /** 1699 * Checks if this measure is less than another measure of the same unit. 1700 * 1701 * @param o the other measure to compare to 1702 * @return true if this measure has a lesser equivalent magnitude, false otherwise 1703 */ 1704 default boolean lt(Measure<U> o) { 1705 return compareTo(o) < 0; 1706 } 1707 1708 /** 1709 * Checks if this measure is less than or equivalent to another measure of the same unit. 1710 * 1711 * @param o the other measure to compare to 1712 * @return true if this measure has an equal or lesser equivalent magnitude, false otherwise 1713 */ 1714 default boolean lte(Measure<U> o) { 1715 return compareTo(o) < 0 || isEquivalent(o); 1716 } 1717 1718 /** 1719 * Returns the measure with the absolute value closest to positive infinity. 1720 * 1721 * @param <U> the type of the units of the measures 1722 * @param measures the set of measures to compare 1723 * @return the measure with the greatest positive magnitude, or null if no measures were provided 1724 */ 1725 @SafeVarargs 1726 static <U extends Unit> Measure<U> max(Measure<U>... measures) { 1727 if (measures.length == 0) { 1728 return null; // nothing to compare 1729 } 1730 1731 Measure<U> max = null; 1732 for (Measure<U> measure : measures) { 1733 if (max == null || measure.gt(max)) { 1734 max = measure; 1735 } 1736 } 1737 1738 return max; 1739 } 1740 1741 /** 1742 * Returns the measure with the absolute value closest to negative infinity. 1743 * 1744 * @param <U> the type of the units of the measures 1745 * @param measures the set of measures to compare 1746 * @return the measure with the greatest negative magnitude 1747 */ 1748 @SafeVarargs 1749 static <U extends Unit> Measure<U> min(Measure<U>... measures) { 1750 if (measures.length == 0) { 1751 return null; // nothing to compare 1752 } 1753 1754 Measure<U> max = null; 1755 for (Measure<U> measure : measures) { 1756 if (max == null || measure.lt(max)) { 1757 max = measure; 1758 } 1759 } 1760 1761 return max; 1762 } 1763 1764 /** 1765 * Returns a string representation of this measurement in a shorthand form. The symbol of the 1766 * backing unit is used, rather than the full name, and the magnitude is represented in scientific 1767 * notation. 1768 * 1769 * @return the short form representation of this measurement 1770 */ 1771 default String toShortString() { 1772 // eg 1.234e+04 V/m (1234 Volt per Meter in long form) 1773 return String.format("%.3e %s", magnitude(), unit().symbol()); 1774 } 1775 1776 /** 1777 * Returns a string representation of this measurement in a longhand form. The name of the backing 1778 * unit is used, rather than its symbol, and the magnitude is represented in a full string, not 1779 * scientific notation. (Very large values may be represented in scientific notation, however) 1780 * 1781 * @return the long form representation of this measurement 1782 */ 1783 default String toLongString() { 1784 // eg 1234 Volt per Meter (1.234e+04 V/m in short form) 1785 return String.format("%s %s", magnitude(), unit().name()); 1786 } 1787}