-
Notifications
You must be signed in to change notification settings - Fork 702
Expand file tree
/
Copy pathMeasure.java
More file actions
1804 lines (1650 loc) · 62 KB
/
Measure.java
File metadata and controls
1804 lines (1650 loc) · 62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.units;
import static edu.wpi.first.units.Units.Value;
import edu.wpi.first.units.measure.Acceleration;
import edu.wpi.first.units.measure.Angle;
import edu.wpi.first.units.measure.AngularAcceleration;
import edu.wpi.first.units.measure.AngularMomentum;
import edu.wpi.first.units.measure.AngularVelocity;
import edu.wpi.first.units.measure.Current;
import edu.wpi.first.units.measure.Dimensionless;
import edu.wpi.first.units.measure.Distance;
import edu.wpi.first.units.measure.Energy;
import edu.wpi.first.units.measure.Force;
import edu.wpi.first.units.measure.Frequency;
import edu.wpi.first.units.measure.LinearAcceleration;
import edu.wpi.first.units.measure.LinearMomentum;
import edu.wpi.first.units.measure.LinearVelocity;
import edu.wpi.first.units.measure.Mass;
import edu.wpi.first.units.measure.MomentOfInertia;
import edu.wpi.first.units.measure.Mult;
import edu.wpi.first.units.measure.Per;
import edu.wpi.first.units.measure.Power;
import edu.wpi.first.units.measure.Resistance;
import edu.wpi.first.units.measure.Temperature;
import edu.wpi.first.units.measure.Time;
import edu.wpi.first.units.measure.Torque;
import edu.wpi.first.units.measure.Velocity;
import edu.wpi.first.units.measure.Voltage;
/**
* A measure holds the magnitude and unit of some dimension, such as distance, time, or speed. Two
* measures with the same <i>unit</i> and <i>magnitude</i> are effectively equivalent objects.
*
* @param <U> the unit type of the measure
*/
public interface Measure<U extends Unit> extends Comparable<Measure<U>> {
/**
* The threshold for two measures to be considered equivalent if converted to the same unit. This
* is only needed due to floating-point error.
*/
double EQUIVALENCE_THRESHOLD = 1e-12;
/**
* Gets the unitless magnitude of this measure.
*
* @return the magnitude in terms of {@link #unit() the unit}.
*/
double magnitude();
/**
* Gets the magnitude of this measure in terms of the base unit. If the unit is the base unit for
* its system of measure, then the value will be equivalent to {@link #magnitude()}.
*
* @return the magnitude in terms of the base unit
*/
double baseUnitMagnitude();
/**
* Gets the units of this measure.
*
* @return the unit
*/
U unit();
/**
* Converts this measure to a measure with a different unit of the same type, eg minutes to
* seconds. Converting to the same unit is equivalent to calling {@link #magnitude()}.
*
* <pre>
* Meters.of(12).in(Feet) // 39.3701
* Seconds.of(15).in(Minutes) // 0.25
* </pre>
*
* @param unit the unit to convert this measure to
* @return the value of this measure in the given unit
*/
default double in(U unit) {
if (this.unit().equals(unit)) {
return magnitude();
} else {
return unit.fromBaseUnits(baseUnitMagnitude());
}
}
/**
* A convenience method to get the base unit of the measurement. Equivalent to {@code
* unit().getBaseUnit()}.
*
* @return the base unit of measure.
*/
@SuppressWarnings("unchecked")
default U baseUnit() {
return (U) unit().getBaseUnit();
}
/**
* Absolute value of measure.
*
* @param unit unit to use
* @return the absolute value of this measure in the given unit
*/
default double abs(U unit) {
return Math.abs(this.in(unit));
}
/**
* Take the sign of another measure.
*
* @param other measure from which to take sign
* @param unit unit to use
* @return the value of the measure in the given unit with the sign of the provided measure
*/
default double copySign(Measure<U> other, U unit) {
return Math.copySign(this.in(unit), other.in(unit));
}
/**
* Returns an immutable copy of this measure. The copy can be used freely and is guaranteed never
* to change.
*
* @return the copied measure
*/
Measure<U> copy();
/**
* Returns a mutable copy of this measure. It will be initialized to the current state of this
* measure, but can be changed over time without needing to allocate new measurement objects.
*
* @return the copied measure
*/
MutableMeasure<U, ?, ?> mutableCopy();
/**
* Returns a measure equivalent to this one equal to zero minus its current value. For non-linear
* unit types like temperature, the zero point is treated as the zero value of the base unit (eg
* Kelvin). In effect, this means code like {@code Celsius.of(10).unaryMinus()} returns a value
* equivalent to -10 Kelvin, and <i>not</i> -10° Celsius.
*
* @return a measure equal to zero minus this measure
*/
Measure<U> unaryMinus();
/**
* Returns a measure equivalent to this one equal to zero minus its current value. For non-linear
* unit types like temperature, the zero point is treated as the zero value of the base unit (eg
* Kelvin). In effect, this means code like {@code Celsius.of(10).negate()} returns a value
* equivalent to -10 Kelvin, and <i>not</i> -10° Celsius.
*
* @return a measure equal to zero minus this measure
* @deprecated use unaryMinus() instead. This was renamed for consistency with other WPILib
* classes like Rotation2d
*/
@Deprecated(since = "2025", forRemoval = true)
default Measure<U> negate() {
return unaryMinus();
}
/**
* Adds another measure of the same unit type to this one.
*
* @param other the measurement to add
* @return a measure of the sum of both measures
*/
Measure<U> plus(Measure<? extends U> other);
/**
* Subtracts another measure of the same unit type from this one.
*
* @param other the measurement to subtract
* @return a measure of the difference between the measures
*/
Measure<U> minus(Measure<? extends U> other);
/**
* Multiplies this measure by a scalar unitless multiplier.
*
* @param multiplier the scalar multiplication factor
* @return the scaled result
*/
Measure<U> times(double multiplier);
/**
* Multiplies this measure by a scalar dimensionless multiplier.
*
* @param multiplier the scalar multiplication factor
* @return the scaled result
*/
Measure<U> times(Dimensionless multiplier);
/**
* Generates a new measure that is equal to this measure multiplied by another. Some dimensional
* analysis is performed to reduce the units down somewhat; for example, multiplying a {@code
* Measure<Time>} by a {@code Measure<Velocity<Distance>>} will return just a {@code
* Measure<Distance>} instead of the naive {@code Measure<Mult<Time, Velocity<Distance>>}. This is
* not guaranteed to perform perfect dimensional analysis.
*
* @param multiplier the unit to multiply by
* @return the multiplicative unit
*/
default Measure<?> times(Measure<?> multiplier) {
final double baseUnitResult = baseUnitMagnitude() * multiplier.baseUnitMagnitude();
// First try to eliminate any common units
if (unit() instanceof PerUnit<?, ?> ratio
&& multiplier.baseUnit().equals(ratio.denominator().getBaseUnit())) {
// Per<N, D> * D -> yield N
// Case 1: denominator of the Per cancels out, return with just the units of the numerator
return ratio.numerator().ofBaseUnits(baseUnitResult);
} else if (multiplier.baseUnit() instanceof PerUnit<?, ?> ratio
&& baseUnit().equals(ratio.denominator().getBaseUnit())) {
// D * Per<N, D) -> yield N
// Case 2: Same as Case 1, just flipped between this and the multiplier
return ratio.numerator().ofBaseUnits(baseUnitResult);
} else if (unit() instanceof PerUnit<?, ?> per
&& multiplier.unit() instanceof PerUnit<?, ?> otherPer
&& per.denominator().getBaseUnit().equals(otherPer.numerator().getBaseUnit())
&& per.numerator().getBaseUnit().equals(otherPer.denominator().getBaseUnit())) {
// multiplying eg meters per second * milliseconds per foot
// return a scalar
return Value.of(baseUnitResult);
}
// No common units to eliminate, is one of them dimensionless?
// Note that this must come *after* the multiplier cases, otherwise
// Per<U, Dimensionless> * Dimensionless will not return a U
if (multiplier.unit() instanceof DimensionlessUnit) {
// scalar multiplication of this
return times(multiplier.baseUnitMagnitude());
} else if (unit() instanceof DimensionlessUnit) {
// scalar multiplication of multiplier
return multiplier.times(baseUnitMagnitude());
}
if (multiplier instanceof Acceleration<?> acceleration) {
return times(acceleration);
} else if (multiplier instanceof Angle angle) {
return times(angle);
} else if (multiplier instanceof AngularAcceleration angularAcceleration) {
return times(angularAcceleration);
} else if (multiplier instanceof AngularMomentum angularMomentum) {
return times(angularMomentum);
} else if (multiplier instanceof AngularVelocity angularVelocity) {
return times(angularVelocity);
} else if (multiplier instanceof Current current) {
return times(current);
} else if (multiplier instanceof Dimensionless dimensionless) {
// n.b. this case should already be covered
return times(dimensionless);
} else if (multiplier instanceof Distance distance) {
return times(distance);
} else if (multiplier instanceof Energy energy) {
return times(energy);
} else if (multiplier instanceof Force force) {
return times(force);
} else if (multiplier instanceof Frequency frequency) {
return times(frequency);
} else if (multiplier instanceof LinearAcceleration linearAcceleration) {
return times(linearAcceleration);
} else if (multiplier instanceof LinearVelocity linearVelocity) {
return times(linearVelocity);
} else if (multiplier instanceof Mass mass) {
return times(mass);
} else if (multiplier instanceof MomentOfInertia momentOfInertia) {
return times(momentOfInertia);
} else if (multiplier instanceof Mult<?, ?> mult) {
return times(mult);
} else if (multiplier instanceof Per<?, ?> per) {
return times(per);
} else if (multiplier instanceof Power power) {
return times(power);
} else if (multiplier instanceof Temperature temperature) {
return times(temperature);
} else if (multiplier instanceof Time time) {
return times(time);
} else if (multiplier instanceof Torque torque) {
return times(torque);
} else if (multiplier instanceof Velocity<?> velocity) {
return times(velocity);
} else if (multiplier instanceof Voltage voltage) {
return times(voltage);
} else if (multiplier instanceof Resistance resistance) {
return times(resistance);
} else {
// Dimensional analysis fallthrough or a generic input measure type
// Do a basic unit multiplication
return MultUnit.combine(unit(), multiplier.unit()).ofBaseUnits(baseUnitResult);
}
}
/**
* Multiplies this measure by an acceleration and returns the resulting measure in the most
* appropriate unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Acceleration<?> multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by an angle and returns the resulting measure in the most appropriate
* unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Angle multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by an angular acceleration and returns the resulting measure in the
* most appropriate unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(AngularAcceleration multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by an angular momentum and returns the resulting measure in the most
* appropriate unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(AngularMomentum multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by an angular velocity and returns the resulting measure in the most
* appropriate unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(AngularVelocity multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by an electric current and returns the resulting measure in the most
* appropriate unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Current multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a distance and returns the resulting measure in the most appropriate
* unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Distance multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by an energy and returns the resulting measure in the most appropriate
* unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Energy multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a force and returns the resulting measure in the most appropriate
* unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Force multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a frequency and returns the resulting measure in the most
* appropriate unit. This often - but not always - means implementations return a variation of a
* {@link Per} measure.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Frequency multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a linear acceleration and returns the resulting measure in the most
* appropriate unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(LinearAcceleration multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a linear momentum and returns the resulting measure in the most
* appropriate unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(LinearMomentum multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a linear velocity and returns the resulting measure in the most
* appropriate unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(LinearVelocity multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a mass and returns the resulting measure in the most appropriate
* unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Mass multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a moment of intertia and returns the resulting measure in the most
* appropriate unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(MomentOfInertia multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a generic multiplied measure and returns the resulting measure in
* the most appropriate unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Mult<?, ?> multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a power and returns the resulting measure in the most appropriate
* unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Power multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a generic ratio measurement and returns the resulting measure in the
* most appropriate unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Per<?, ?> multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a temperature and returns the resulting measure in the most
* appropriate unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Temperature multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a time and returns the resulting measure in the most appropriate
* unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Time multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a torque and returns the resulting measure in the most appropriate
* unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Torque multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a generic velocity and returns the resulting measure in the most
* appropriate unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Velocity<?> multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a voltage and returns the resulting measure in the most appropriate
* unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Voltage multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a resistance and returns the resulting measure in the most
* appropriate unit.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Measure<?> times(Resistance multiplier) {
return MultUnit.combine(unit(), multiplier.unit())
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by a conversion factor, returning the converted measurement. Unlike
* {@link #times(Per)}, this allows for basic unit cancellation to return measurements of a known
* dimension.
*
* @param conversionFactor the conversion factor by which to multiply
* @param <Other> the unit type to convert to
* @param <M> the concrete return unit type. <strong>Note: the conversion factor's numerator unit
* must return instances of this type from {@link Unit#ofBaseUnits(double)}}</strong>
* @return the converted result
*/
@SuppressWarnings("unchecked")
default <Other extends Unit, M extends Measure<Other>> M timesConversionFactor(
Measure<? extends PerUnit<Other, U>> conversionFactor) {
return (M)
conversionFactor
.unit()
.getBaseUnit()
.numerator()
.ofBaseUnits(baseUnitMagnitude() * conversionFactor.baseUnitMagnitude());
}
/**
* Multiplies this measure by another measurement of the inverse unit type (eg Time multiplied by
* Frequency) and returns the resulting dimensionless measure.
*
* @param multiplier the measurement to multiply by.
* @return the multiplication result
*/
default Dimensionless timesInverse(
Measure<? extends PerUnit<DimensionlessUnit, ? extends U>> multiplier) {
return Value.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
}
/**
* Multiplies this measure by another measurement of something over the unit type (eg Time
* multiplied by LinearVelocity) and returns the resulting measure of the ratio's dividend unit.
*
* @param ratio the measurement to multiply by.
* @param <Other> other unit that the results are in terms of
* @return the multiplication result
*/
default <Other extends Unit> Measure<Other> timesRatio(
Measure<? extends PerUnit<? extends Other, U>> ratio) {
return ImmutableMeasure.ofBaseUnits(
baseUnitMagnitude() * ratio.baseUnitMagnitude(), ratio.unit().numerator());
}
/**
* Divides this measure by a unitless scalar and returns the result.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
Measure<U> div(double divisor);
/**
* Divides this measure by a dimensionless scalar and returns the result.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
Measure<U> div(Dimensionless divisor);
/**
* Divides this measurement by another measure and performs some dimensional analysis to reduce
* the units.
*
* @param divisor the unit to divide by
* @return the resulting measure
*/
default Measure<?> div(Measure<?> divisor) {
final double baseUnitResult = baseUnitMagnitude() / divisor.baseUnitMagnitude();
if (unit().getBaseUnit().equals(divisor.unit().getBaseUnit())) {
// These are the same unit, return a dimensionless
return Value.ofBaseUnits(baseUnitResult);
}
if (divisor instanceof Dimensionless) {
// Dividing by a dimensionless, do a scalar division
return unit().ofBaseUnits(baseUnitResult);
}
// Numerator is a dimensionless
if (unit() instanceof DimensionlessUnit && divisor.unit() instanceof PerUnit<?, ?> ratio) {
// Dividing by a ratio, return its reciprocal scaled by this
return ratio.reciprocal().ofBaseUnits(baseUnitResult);
}
if (divisor.unit() instanceof PerUnit<?, ?> ratio
&& ratio.numerator().getBaseUnit().equals(baseUnit())) {
// Numerator of the ratio cancels out
// N / (N / D) -> N * D / N -> D
// Note: all Velocity and Acceleration units inherit from PerUnit
return ratio.denominator().ofBaseUnits(baseUnitResult);
}
if (divisor.unit() instanceof TimeUnit time) {
// Dividing by a time, return a Velocity
return VelocityUnit.combine(unit(), time).ofBaseUnits(baseUnitResult);
}
if (divisor instanceof Acceleration<?> acceleration) {
return div(acceleration);
} else if (divisor instanceof Angle angle) {
return div(angle);
} else if (divisor instanceof AngularAcceleration angularAcceleration) {
return div(angularAcceleration);
} else if (divisor instanceof AngularMomentum angularMomentum) {
return div(angularMomentum);
} else if (divisor instanceof AngularVelocity angularVelocity) {
return div(angularVelocity);
} else if (divisor instanceof Current current) {
return div(current);
} else if (divisor instanceof Dimensionless dimensionless) {
// n.b. this case should already be covered
return div(dimensionless);
} else if (divisor instanceof Distance distance) {
return div(distance);
} else if (divisor instanceof Energy energy) {
return div(energy);
} else if (divisor instanceof Force force) {
return div(force);
} else if (divisor instanceof Frequency frequency) {
return div(frequency);
} else if (divisor instanceof LinearAcceleration linearAcceleration) {
return div(linearAcceleration);
} else if (divisor instanceof LinearVelocity linearVelocity) {
return div(linearVelocity);
} else if (divisor instanceof Mass mass) {
return div(mass);
} else if (divisor instanceof MomentOfInertia momentOfInertia) {
return div(momentOfInertia);
} else if (divisor instanceof Mult<?, ?> mult) {
return div(mult);
} else if (divisor instanceof Per<?, ?> per) {
return div(per);
} else if (divisor instanceof Power power) {
return div(power);
} else if (divisor instanceof Temperature temperature) {
return div(temperature);
} else if (divisor instanceof Time time) {
return div(time);
} else if (divisor instanceof Torque torque) {
return div(torque);
} else if (divisor instanceof Velocity<?> velocity) {
return div(velocity);
} else if (divisor instanceof Voltage voltage) {
return div(voltage);
} else if (divisor instanceof Resistance resistance) {
return div(resistance);
} else {
// Dimensional analysis fallthrough or a generic input measure type
// Do a basic unit multiplication
return PerUnit.combine(unit(), divisor.unit()).ofBaseUnits(baseUnitResult);
}
}
/**
* Divides this measure by a generic acceleration and returns the result in the most appropriate
* unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(Acceleration<?> divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by an angle and returns the result in the most appropriate unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(Angle divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by an angular acceleration and returns the result in the most appropriate
* unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(AngularAcceleration divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by an angular momentum and returns the result in the most appropriate
* unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(AngularMomentum divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by an angular velocity and returns the result in the most appropriate
* unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(AngularVelocity divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by an electric current and returns the result in the most appropriate
* unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(Current divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a distance and returns the result in the most appropriate unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(Distance divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by an energy and returns the result in the most appropriate unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(Energy divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a force and returns the result in the most appropriate unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(Force divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a frequency and returns the result in the most appropriate unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(Frequency divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a linear acceleration and returns the result in the most appropriate
* unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(LinearAcceleration divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a linear momentum and returns the result in the most appropriate unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(LinearMomentum divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a linear velocity and returns the result in the most appropriate unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(LinearVelocity divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a mass and returns the result in the most appropriate unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(Mass divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a moment of inertia and returns the result in the most appropriate
* unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(MomentOfInertia divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a generic multiplication and returns the result in the most appropriate
* unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(Mult<?, ?> divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a power and returns the result in the most appropriate unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(Power divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a generic ratio and returns the result in the most appropriate unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(Per<?, ?> divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a temperature and returns the result in the most appropriate unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(Temperature divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a time and returns the result in the most appropriate unit. This will
* often - but not always - result in a {@link Per} type like {@link LinearVelocity} or {@link
* Acceleration}.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(Time divisor) {
return VelocityUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a torque and returns the result in the most appropriate unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(Torque divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a generic velocity and returns the result in the most appropriate unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/
default Measure<?> div(Velocity<?> divisor) {
return PerUnit.combine(unit(), divisor.unit())
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
}
/**
* Divides this measure by a voltage and returns the result in the most appropriate unit.
*
* @param divisor the measurement to divide by.
* @return the division result
*/