forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnit.swift
2612 lines (2140 loc) · 79.2 KB
/
Unit.swift
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
/*
NSUnitConverter describes how to convert a unit to and from the base unit of its dimension. Use the NSUnitConverter protocol to implement new ways of converting a unit.
*/
open class UnitConverter : NSObject, @unchecked Sendable {
/*
The following methods perform conversions to and from the base unit of a unit class's dimension. Each unit is defined against the base unit for the dimension to which the unit belongs.
These methods are implemented differently depending on the type of conversion. The default implementation in NSUnitConverter simply returns the value.
These methods exist for the sole purpose of creating custom conversions for units in order to support converting a value from one kind of unit to another in the same dimension. NSUnitConverter is an abstract class that is meant to be subclassed. There is no need to call these methods directly to do a conversion -- the correct way to convert a measurement is to use [NSMeasurement measurementByConvertingToUnit:]. measurementByConvertingToUnit: uses the following 2 methods internally to perform the conversion.
When creating a custom unit converter, you must override these two methods to implement the conversion to and from a value in terms of a unit and the corresponding value in terms of the base unit of that unit's dimension in order for conversion to work correctly.
*/
/*
This method takes a value in terms of a unit and returns the corresponding value in terms of the base unit of the original unit's dimension.
@param value Value in terms of the unit class
@return Value in terms of the base unit
*/
open func baseUnitValue(fromValue value: Double) -> Double {
return value
}
/*
This method takes in a value in terms of the base unit of a unit's dimension and returns the equivalent value in terms of the unit.
@param baseUnitValue Value in terms of the base unit
@return Value in terms of the unit class
*/
open func value(fromBaseUnitValue baseUnitValue: Double) -> Double {
return baseUnitValue
}
}
open class UnitConverterLinear : UnitConverter, NSSecureCoding, @unchecked Sendable {
open private(set) var coefficient: Double
open private(set) var constant: Double
public convenience init(coefficient: Double) {
self.init(coefficient: coefficient, constant: 0)
}
public init(coefficient: Double, constant: Double) {
self.coefficient = coefficient
self.constant = constant
}
open override func baseUnitValue(fromValue value: Double) -> Double {
return value * coefficient + constant
}
open override func value(fromBaseUnitValue baseUnitValue: Double) -> Double {
return (baseUnitValue - constant) / coefficient
}
public required convenience init?(coder aDecoder: NSCoder) {
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
let coefficient = aDecoder.decodeDouble(forKey: "NS.coefficient")
let constant = aDecoder.decodeDouble(forKey: "NS.constant")
self.init(coefficient: coefficient, constant: constant)
}
open func encode(with aCoder: NSCoder) {
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(self.coefficient, forKey:"NS.coefficient")
aCoder.encode(self.constant, forKey:"NS.constant")
}
public static var supportsSecureCoding: Bool { return true }
open override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? UnitConverterLinear else {
return false
}
if self === other {
return true
}
return self.coefficient == other.coefficient
&& self.constant == other.constant
}
}
// This must be named with a NS prefix because it can be sometimes encoded by Darwin, and we need to match the name in the archive.
internal class NSUnitConverterReciprocal : UnitConverter, NSSecureCoding, @unchecked Sendable {
private var reciprocal: Double
init(reciprocal: Double) {
self.reciprocal = reciprocal
}
override func baseUnitValue(fromValue value: Double) -> Double {
return reciprocal / value
}
override func value(fromBaseUnitValue baseUnitValue: Double) -> Double {
return reciprocal / baseUnitValue
}
required convenience init?(coder aDecoder: NSCoder) {
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
let reciprocal = aDecoder.decodeDouble(forKey: "NS.reciprocal")
self.init(reciprocal: reciprocal)
}
func encode(with aCoder: NSCoder) {
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(self.reciprocal, forKey:"NS.reciprocal")
}
static var supportsSecureCoding: Bool { return true }
open override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? NSUnitConverterReciprocal else {
return false
}
if self === other {
return true
}
return self.reciprocal == other.reciprocal
}
}
/*
NSUnit is the base class for all unit types (dimensional and dimensionless).
*/
open class Unit : NSObject, NSCopying, NSSecureCoding, @unchecked Sendable {
public let symbol: String
public required init(symbol: String) {
self.symbol = symbol
}
open func copy(with zone: NSZone?) -> Any {
return self
}
public required init?(coder aDecoder: NSCoder) {
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
guard let symbol = aDecoder.decodeObject(forKey: "NS.symbol") as? String
else { return nil }
self.symbol = symbol
}
open func encode(with aCoder: NSCoder) {
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(self.symbol._bridgeToObjectiveC(), forKey:"NS.symbol")
}
public static var supportsSecureCoding: Bool { return true }
open override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? Unit else {
return false
}
if self === other {
return true
}
return self.symbol == other.symbol
}
}
open class Dimension : Unit, @unchecked Sendable {
public let converter: UnitConverter
public required init(symbol: String, converter: UnitConverter) {
self.converter = converter
super.init(symbol: symbol)
}
/*
This class method returns an instance of the dimension class that represents the base unit of that dimension.
e.g.
NSUnitSpeed *metersPerSecond = [NSUnitSpeed baseUnit];
*/
open class func baseUnit() -> Self {
fatalError("*** You must override baseUnit in your class to define its base unit.")
}
public required init?(coder aDecoder: NSCoder) {
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
guard
let symbol = aDecoder.decodeObject(of: NSString.self, forKey: "NS.symbol")?._swiftObject,
let converter = aDecoder.decodeObject(of: [UnitConverterLinear.self, NSUnitConverterReciprocal.self], forKey: "NS.converter") as? UnitConverter
else { return nil }
self.converter = converter
super.init(symbol: symbol)
}
public required init(symbol: String) {
let T = type(of: self)
fatalError("\(T) must be initialized with designated initializer \(T).init(symbol: String, converter: UnitConverter)")
}
open override func encode(with aCoder: NSCoder) {
super.encode(with: aCoder)
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(self.converter, forKey:"NS.converter")
}
open override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? Dimension else {
return false
}
if self === other {
return true
}
return super.isEqual(object) && self.converter == other.converter
}
}
public final class UnitAcceleration : Dimension, @unchecked Sendable {
/*
Base unit - metersPerSecondSquared
*/
private struct Symbol {
static let metersPerSecondSquared = "m/s²"
static let gravity = "g"
}
private struct Coefficient {
static let metersPerSecondSquared = 1.0
static let gravity = 9.81
}
private convenience init(symbol: String, coefficient: Double) {
self.init(symbol: symbol, converter: UnitConverterLinear(coefficient: coefficient))
}
public class var metersPerSecondSquared: UnitAcceleration {
get {
return UnitAcceleration(symbol: Symbol.metersPerSecondSquared, coefficient: Coefficient.metersPerSecondSquared)
}
}
public class var gravity: UnitAcceleration {
get {
return UnitAcceleration(symbol: Symbol.gravity, coefficient: Coefficient.gravity)
}
}
public override class func baseUnit() -> UnitAcceleration {
return .metersPerSecondSquared
}
public override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? UnitAcceleration else {
return false
}
if self === other {
return true
}
return super.isEqual(object)
}
}
public final class UnitAngle : Dimension, @unchecked Sendable {
/*
Base unit - degrees
*/
private struct Symbol {
static let degrees = "°"
static let arcMinutes = "ʹ"
static let arcSeconds = "ʹʹ"
static let radians = "rad"
static let gradians = "grad"
static let revolutions = "rev"
}
private struct Coefficient {
static let degrees = 1.0
static let arcMinutes = 1.0 / 60.0
static let arcSeconds = 1.0 / 3600.0
static let radians = 180.0 / .pi
static let gradians = 0.9
static let revolutions = 360.0
}
private convenience init(symbol: String, coefficient: Double) {
self.init(symbol: symbol, converter: UnitConverterLinear(coefficient: coefficient))
}
public class var degrees: UnitAngle {
get {
return UnitAngle(symbol: Symbol.degrees, coefficient: Coefficient.degrees)
}
}
public class var arcMinutes: UnitAngle {
get {
return UnitAngle(symbol: Symbol.arcMinutes, coefficient: Coefficient.arcMinutes)
}
}
public class var arcSeconds: UnitAngle {
get {
return UnitAngle(symbol: Symbol.arcSeconds, coefficient: Coefficient.arcSeconds)
}
}
public class var radians: UnitAngle {
get {
return UnitAngle(symbol: Symbol.radians, coefficient: Coefficient.radians)
}
}
public class var gradians: UnitAngle {
get {
return UnitAngle(symbol: Symbol.gradians, coefficient: Coefficient.gradians)
}
}
public class var revolutions: UnitAngle {
get {
return UnitAngle(symbol: Symbol.revolutions, coefficient: Coefficient.revolutions)
}
}
public override class func baseUnit() -> UnitAngle {
return .degrees
}
public override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? UnitAngle else {
return false
}
if self === other {
return true
}
return super.isEqual(object)
}
}
public final class UnitArea : Dimension, @unchecked Sendable {
/*
Base unit - squareMeters
*/
private struct Symbol {
static let squareMegameters = "Mm²"
static let squareKilometers = "km²"
static let squareMeters = "m²"
static let squareCentimeters = "cm²"
static let squareMillimeters = "mm²"
static let squareMicrometers = "µm²"
static let squareNanometers = "nm²"
static let squareInches = "in²"
static let squareFeet = "ft²"
static let squareYards = "yd²"
static let squareMiles = "mi²"
static let acres = "ac"
static let ares = "a"
static let hectares = "ha"
}
private struct Coefficient {
static let squareMegameters = 1e12
static let squareKilometers = 1e6
static let squareMeters = 1.0
static let squareCentimeters = 1e-4
static let squareMillimeters = 1e-6
static let squareMicrometers = 1e-12
static let squareNanometers = 1e-18
static let squareInches = 0.00064516
static let squareFeet = 0.092903
static let squareYards = 0.836127
static let squareMiles = 2.59e+6
static let acres = 4046.86
static let ares = 100.0
static let hectares = 10000.0
}
private convenience init(symbol: String, coefficient: Double) {
self.init(symbol: symbol, converter: UnitConverterLinear(coefficient: coefficient))
}
public class var squareMegameters: UnitArea {
get {
return UnitArea(symbol: Symbol.squareMegameters, coefficient: Coefficient.squareMegameters)
}
}
public class var squareKilometers: UnitArea {
get {
return UnitArea(symbol: Symbol.squareKilometers, coefficient: Coefficient.squareKilometers)
}
}
public class var squareMeters: UnitArea {
get {
return UnitArea(symbol: Symbol.squareMeters, coefficient: Coefficient.squareMeters)
}
}
public class var squareCentimeters: UnitArea {
get {
return UnitArea(symbol: Symbol.squareCentimeters, coefficient: Coefficient.squareCentimeters)
}
}
public class var squareMillimeters: UnitArea {
get {
return UnitArea(symbol: Symbol.squareMillimeters, coefficient: Coefficient.squareMillimeters)
}
}
public class var squareMicrometers: UnitArea {
get {
return UnitArea(symbol: Symbol.squareMicrometers, coefficient: Coefficient.squareMicrometers)
}
}
public class var squareNanometers: UnitArea {
get {
return UnitArea(symbol: Symbol.squareNanometers, coefficient: Coefficient.squareNanometers)
}
}
public class var squareInches: UnitArea {
get {
return UnitArea(symbol: Symbol.squareInches, coefficient: Coefficient.squareInches)
}
}
public class var squareFeet: UnitArea {
get {
return UnitArea(symbol: Symbol.squareFeet, coefficient: Coefficient.squareFeet)
}
}
public class var squareYards: UnitArea {
get {
return UnitArea(symbol: Symbol.squareYards, coefficient: Coefficient.squareYards)
}
}
public class var squareMiles: UnitArea {
get {
return UnitArea(symbol: Symbol.squareMiles, coefficient: Coefficient.squareMiles)
}
}
public class var acres: UnitArea {
get {
return UnitArea(symbol: Symbol.acres, coefficient: Coefficient.acres)
}
}
public class var ares: UnitArea {
get {
return UnitArea(symbol: Symbol.ares, coefficient: Coefficient.ares)
}
}
public class var hectares: UnitArea {
get {
return UnitArea(symbol: Symbol.hectares, coefficient: Coefficient.hectares)
}
}
public override class func baseUnit() -> UnitArea {
return .squareMeters
}
public override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? UnitArea else {
return false
}
if self === other {
return true
}
return super.isEqual(object)
}
}
public final class UnitConcentrationMass : Dimension, @unchecked Sendable {
/*
Base unit - gramsPerLiter
*/
private struct Symbol {
static let gramsPerLiter = "g/L"
static let milligramsPerDeciliter = "mg/dL"
static let millimolesPerLiter = "mmol/L"
}
private struct Coefficient {
static let gramsPerLiter = 1.0
static let milligramsPerDeciliter = 0.01
static let millimolesPerLiter = 18.0
}
private convenience init(symbol: String, coefficient: Double) {
self.init(symbol: symbol, converter: UnitConverterLinear(coefficient: coefficient))
}
public class var gramsPerLiter: UnitConcentrationMass {
get {
return UnitConcentrationMass(symbol: Symbol.gramsPerLiter, coefficient: Coefficient.gramsPerLiter)
}
}
public class var milligramsPerDeciliter: UnitConcentrationMass {
get {
return UnitConcentrationMass(symbol: Symbol.milligramsPerDeciliter, coefficient: Coefficient.milligramsPerDeciliter)
}
}
public class func millimolesPerLiter(withGramsPerMole gramsPerMole: Double) -> UnitConcentrationMass {
return UnitConcentrationMass(symbol: Symbol.millimolesPerLiter, coefficient: Coefficient.millimolesPerLiter * gramsPerMole)
}
public override class func baseUnit() -> UnitConcentrationMass {
return UnitConcentrationMass.gramsPerLiter
}
public override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? UnitConcentrationMass else {
return false
}
if self === other {
return true
}
return super.isEqual(object)
}
}
public final class UnitDispersion : Dimension, @unchecked Sendable {
/*
Base unit - partsPerMillion
*/
private struct Symbol {
static let partsPerMillion = "ppm"
}
private struct Coefficient {
static let partsPerMillion = 1.0
}
private convenience init(symbol: String, coefficient: Double) {
self.init(symbol: symbol, converter: UnitConverterLinear(coefficient: coefficient))
}
public class var partsPerMillion: UnitDispersion {
get {
return UnitDispersion(symbol: Symbol.partsPerMillion, coefficient: Coefficient.partsPerMillion)
}
}
public override class func baseUnit() -> UnitDispersion {
return .partsPerMillion
}
public override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? UnitDispersion else {
return false
}
if self === other {
return true
}
return super.isEqual(object)
}
}
public final class UnitDuration : Dimension, @unchecked Sendable {
/*
Base unit - seconds
*/
private struct Symbol {
static let picoseconds = "ps"
static let nanoseconds = "ns"
static let microseconds = "µs"
static let milliseconds = "ms"
static let seconds = "s"
static let minutes = "m"
static let hours = "h"
}
private struct Coefficient {
static let picoseconds = 1e-12
static let nanoseconds = 1e-9
static let microseconds = 1e-6
static let milliseconds = 1e-3
static let seconds = 1.0
static let minutes = 60.0
static let hours = 3600.0
}
private convenience init(symbol: String, coefficient: Double) {
self.init(symbol: symbol, converter: UnitConverterLinear(coefficient: coefficient))
}
public class var picoseconds: UnitDuration {
get {
return UnitDuration(symbol: Symbol.picoseconds, coefficient: Coefficient.picoseconds)
}
}
public class var nanoseconds: UnitDuration {
get {
return UnitDuration(symbol: Symbol.nanoseconds, coefficient: Coefficient.nanoseconds)
}
}
public class var microseconds: UnitDuration {
get {
return UnitDuration(symbol: Symbol.microseconds, coefficient: Coefficient.microseconds)
}
}
public class var milliseconds: UnitDuration {
get {
return UnitDuration(symbol: Symbol.milliseconds, coefficient: Coefficient.milliseconds)
}
}
public class var seconds: UnitDuration {
get {
return UnitDuration(symbol: Symbol.seconds, coefficient: Coefficient.seconds)
}
}
public class var minutes: UnitDuration {
get {
return UnitDuration(symbol: Symbol.minutes, coefficient: Coefficient.minutes)
}
}
public class var hours: UnitDuration {
get {
return UnitDuration(symbol: Symbol.hours, coefficient: Coefficient.hours)
}
}
public override class func baseUnit() -> UnitDuration {
return .seconds
}
public override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? UnitDuration else {
return false
}
if self === other {
return true
}
return super.isEqual(object)
}
}
public final class UnitElectricCharge : Dimension, @unchecked Sendable {
/*
Base unit - coulombs
*/
private struct Symbol {
static let coulombs = "C"
static let megaampereHours = "MAh"
static let kiloampereHours = "kAh"
static let ampereHours = "Ah"
static let milliampereHours = "mAh"
static let microampereHours = "µAh"
}
private struct Coefficient {
static let coulombs = 1.0
static let megaampereHours = 3.6e9
static let kiloampereHours = 3600000.0
static let ampereHours = 3600.0
static let milliampereHours = 3.6
static let microampereHours = 0.0036
}
private convenience init(symbol: String, coefficient: Double) {
self.init(symbol: symbol, converter: UnitConverterLinear(coefficient: coefficient))
}
public class var coulombs: UnitElectricCharge {
get {
return UnitElectricCharge(symbol: Symbol.coulombs, coefficient: Coefficient.coulombs)
}
}
public class var megaampereHours: UnitElectricCharge {
get {
return UnitElectricCharge(symbol: Symbol.megaampereHours, coefficient: Coefficient.megaampereHours)
}
}
public class var kiloampereHours: UnitElectricCharge {
get {
return UnitElectricCharge(symbol: Symbol.kiloampereHours, coefficient: Coefficient.kiloampereHours)
}
}
public class var ampereHours: UnitElectricCharge {
get {
return UnitElectricCharge(symbol: Symbol.ampereHours, coefficient: Coefficient.ampereHours)
}
}
public class var milliampereHours: UnitElectricCharge {
get {
return UnitElectricCharge(symbol: Symbol.milliampereHours, coefficient: Coefficient.milliampereHours)
}
}
public class var microampereHours: UnitElectricCharge {
get {
return UnitElectricCharge(symbol: Symbol.microampereHours, coefficient: Coefficient.microampereHours)
}
}
public override class func baseUnit() -> UnitElectricCharge {
return .coulombs
}
public override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? UnitElectricCharge else {
return false
}
if self === other {
return true
}
return super.isEqual(object)
}
}
public final class UnitElectricCurrent : Dimension, @unchecked Sendable {
/*
Base unit - amperes
*/
private struct Symbol {
static let megaamperes = "MA"
static let kiloamperes = "kA"
static let amperes = "A"
static let milliamperes = "mA"
static let microamperes = "µA"
}
private struct Coefficient {
static let megaamperes = 1e6
static let kiloamperes = 1e3
static let amperes = 1.0
static let milliamperes = 1e-3
static let microamperes = 1e-6
}
private convenience init(symbol: String, coefficient: Double) {
self.init(symbol: symbol, converter: UnitConverterLinear(coefficient: coefficient))
}
public class var megaamperes: UnitElectricCurrent {
get {
return UnitElectricCurrent(symbol: Symbol.megaamperes, coefficient: Coefficient.megaamperes)
}
}
public class var kiloamperes: UnitElectricCurrent {
get {
return UnitElectricCurrent(symbol: Symbol.kiloamperes, coefficient: Coefficient.kiloamperes)
}
}
public class var amperes: UnitElectricCurrent {
get {
return UnitElectricCurrent(symbol: Symbol.amperes, coefficient: Coefficient.amperes)
}
}
public class var milliamperes: UnitElectricCurrent {
get {
return UnitElectricCurrent(symbol: Symbol.milliamperes, coefficient: Coefficient.milliamperes)
}
}
public class var microamperes: UnitElectricCurrent {
get {
return UnitElectricCurrent(symbol: Symbol.microamperes, coefficient: Coefficient.microamperes)
}
}
public override class func baseUnit() -> UnitElectricCurrent {
return .amperes
}
public override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? UnitElectricCurrent else {
return false
}
if self === other {
return true
}
return super.isEqual(object)
}
}
public final class UnitElectricPotentialDifference : Dimension, @unchecked Sendable {
/*
Base unit - volts
*/
private struct Symbol {
static let megavolts = "MV"
static let kilovolts = "kV"
static let volts = "V"
static let millivolts = "mV"
static let microvolts = "µV"
}
private struct Coefficient {
static let megavolts = 1e6
static let kilovolts = 1e3
static let volts = 1.0
static let millivolts = 1e-3
static let microvolts = 1e-6
}
private convenience init(symbol: String, coefficient: Double) {
self.init(symbol: symbol, converter: UnitConverterLinear(coefficient: coefficient))
}
public class var megavolts: UnitElectricPotentialDifference {
get {
return UnitElectricPotentialDifference(symbol: Symbol.megavolts, coefficient: Coefficient.megavolts)
}
}
public class var kilovolts: UnitElectricPotentialDifference {
get {
return UnitElectricPotentialDifference(symbol: Symbol.kilovolts, coefficient: Coefficient.kilovolts)
}
}
public class var volts: UnitElectricPotentialDifference {
get {
return UnitElectricPotentialDifference(symbol: Symbol.volts, coefficient: Coefficient.volts)
}
}
public class var millivolts: UnitElectricPotentialDifference {
get {
return UnitElectricPotentialDifference(symbol: Symbol.millivolts, coefficient: Coefficient.millivolts)
}
}
public class var microvolts: UnitElectricPotentialDifference {
get {
return UnitElectricPotentialDifference(symbol: Symbol.microvolts, coefficient: Coefficient.microvolts)
}
}
public override class func baseUnit() -> UnitElectricPotentialDifference {
return .volts
}
public override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? UnitElectricPotentialDifference else {
return false
}
if self === other {
return true
}
return super.isEqual(object)
}
}
public final class UnitElectricResistance : Dimension, @unchecked Sendable {
/*
Base unit - ohms
*/
private struct Symbol {
static let megaohms = "MΩ"
static let kiloohms = "kΩ"
static let ohms = "Ω"
static let milliohms = "mΩ"
static let microohms = "µΩ"
}
private struct Coefficient {
static let megaohms = 1e6
static let kiloohms = 1e3
static let ohms = 1.0
static let milliohms = 1e-3
static let microohms = 1e-6
}
private convenience init(symbol: String, coefficient: Double) {
self.init(symbol: symbol, converter: UnitConverterLinear(coefficient: coefficient))
}
public class var megaohms: UnitElectricResistance {
get {
return UnitElectricResistance(symbol: Symbol.megaohms, coefficient: Coefficient.megaohms)
}
}
public class var kiloohms: UnitElectricResistance {
get {
return UnitElectricResistance(symbol: Symbol.kiloohms, coefficient: Coefficient.kiloohms)
}
}
public class var ohms: UnitElectricResistance {
get {
return UnitElectricResistance(symbol: Symbol.ohms, coefficient: Coefficient.ohms)
}
}
public class var milliohms: UnitElectricResistance {
get {
return UnitElectricResistance(symbol: Symbol.milliohms, coefficient: Coefficient.milliohms)
}
}
public class var microohms: UnitElectricResistance {
get {
return UnitElectricResistance(symbol: Symbol.microohms, coefficient: Coefficient.microohms)
}
}
public override class func baseUnit() -> UnitElectricResistance {
return .ohms
}
public override func isEqual(_ object: Any?) -> Bool {