Skip to content

Commit

Permalink
[wpiunits] Overload Measure.per(Time) to return Measure<Velocity> (wp…
Browse files Browse the repository at this point in the history
…ilibsuite#6018)

As opposed to returning Measure<Per<U, Time>>
Now matches the overload on Unit
  • Loading branch information
SamCarlberg authored Dec 8, 2023
1 parent a71adef commit ddf79a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
15 changes: 15 additions & 0 deletions wpiunits/src/main/java/edu/wpi/first/units/Measure.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,21 @@ default <U2 extends Unit<U2>> Measure<Per<U, U2>> per(U2 denominator) {
return newUnit.of(magnitude());
}

/**
* Creates a velocity measure equivalent to this one per a unit of time.
*
* <pre>
* Radians.of(3.14).per(Second) // Velocity&lt;Angle&gt; equivalent to RadiansPerSecond.of(3.14)
* </pre>
*
* @param time the unit of time
* @return the velocity measure
*/
default Measure<Velocity<U>> per(Time time) {
var newUnit = unit().per(time);
return newUnit.of(magnitude());
}

/**
* Adds another measure to this one. The resulting measure has the same unit as this one.
*
Expand Down
12 changes: 11 additions & 1 deletion wpiunits/src/test/java/edu/wpi/first/units/MeasureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void testAs() {
}

@Test
void testPerUnitTime() {
void testPerMeasureTime() {
var measure = Units.Kilograms.of(144);
var dt = Units.Milliseconds.of(53);

Expand All @@ -89,6 +89,16 @@ void testPerUnitTime() {
assertEquals(Units.Kilograms.per(Units.Milliseconds), result.unit());
}

@Test
void testPerUnitTime() {
var measure = Units.Kilograms.of(144);
var result = measure.per(Units.Millisecond);

assertEquals(Velocity.class, result.unit().getClass());
assertEquals(144_000.0, result.baseUnitMagnitude(), 1e-5);
assertEquals(Units.Kilograms.per(Units.Milliseconds), result.unit());
}

@Test
void testTimesMeasure() {
var m1 = Units.Volts.of(1.567);
Expand Down

0 comments on commit ddf79a2

Please sign in to comment.