|
10 | 10 | //
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 |
| - |
14 | 13 | open class NSMeasurement : NSObject, NSCopying, NSSecureCoding {
|
15 | 14 | open private(set) var unit: Unit
|
16 | 15 | open private(set) var doubleValue: Double
|
17 | 16 |
|
18 |
| - @available(*, unavailable) |
19 |
| - public convenience override init() { fatalError("Measurements must be constructed with a value and unit") } |
20 |
| - |
21 | 17 | public init(doubleValue: Double, unit: Unit) {
|
22 |
| - self.doubleValue = doubleValue |
23 | 18 | self.unit = unit
|
| 19 | + self.doubleValue = doubleValue |
24 | 20 | }
|
25 | 21 |
|
26 |
| - open func canBeConverted(to unit: Unit) -> Bool { NSUnimplemented() } |
| 22 | + open func canBeConverted(to unit: Unit) -> Bool { |
| 23 | + return self.unit is Dimension && unit is Dimension && type(of: unit).isSubclass(of: type(of: self.unit)) |
| 24 | + } |
27 | 25 |
|
28 |
| - open func converting(to unit: Unit) -> Measurement<Unit> { NSUnimplemented() } |
| 26 | + open func converting(to otherUnit: Unit) -> Measurement<Unit> { |
| 27 | + precondition(canBeConverted(to: otherUnit)) |
| 28 | + |
| 29 | + if unit.isEqual(otherUnit) { |
| 30 | + return Measurement(value: doubleValue, unit: otherUnit) |
| 31 | + } else { |
| 32 | + let dimensionUnit = unit as! Dimension |
| 33 | + |
| 34 | + let valueInTermsOfBase = dimensionUnit.converter.baseUnitValue(fromValue: doubleValue) |
| 35 | + if otherUnit.isEqual(type(of: dimensionUnit).baseUnit()) { |
| 36 | + return Measurement(value: valueInTermsOfBase, unit: otherUnit) |
| 37 | + } else { |
| 38 | + let otherDimensionUnit = otherUnit as! Dimension |
| 39 | + |
| 40 | + let otherValueFromTermsOfBase = otherDimensionUnit.converter.value(fromBaseUnitValue: valueInTermsOfBase) |
| 41 | + return Measurement(value: otherValueFromTermsOfBase, unit: otherUnit) |
| 42 | + } |
| 43 | + } |
| 44 | + } |
29 | 45 |
|
30 |
| - open func adding(_ measurement: Measurement<Unit>) -> Measurement<Unit> { NSUnimplemented() } |
| 46 | + open func adding(_ rhs: Measurement<Unit>) -> Measurement<Unit> { |
| 47 | + precondition(unit is Dimension) |
| 48 | + precondition(rhs.unit is Dimension) |
| 49 | + |
| 50 | + let dimensionUnit = unit as! Dimension |
| 51 | + let rhsDimensionUnit = rhs.unit as! Dimension |
| 52 | + |
| 53 | + if unit.isEqual(rhs.unit) { |
| 54 | + return Measurement(value: doubleValue + rhs.value, unit: unit) |
| 55 | + } else { |
| 56 | + let lhsValueInTermsOfBase = dimensionUnit.converter.baseUnitValue(fromValue: doubleValue) |
| 57 | + let rhsValueInTermsOfBase = rhsDimensionUnit.converter.baseUnitValue(fromValue: rhs.value) |
| 58 | + return Measurement(value: lhsValueInTermsOfBase + rhsValueInTermsOfBase, unit: type(of: dimensionUnit).baseUnit()) |
| 59 | + } |
| 60 | + } |
31 | 61 |
|
32 |
| - open func subtracting(_ measurement: Measurement<Unit>) -> Measurement<Unit> { NSUnimplemented() } |
| 62 | + open func subtracting(_ rhs: Measurement<Unit>) -> Measurement<Unit> { |
| 63 | + precondition(unit is Dimension) |
| 64 | + precondition(rhs.unit is Dimension) |
| 65 | + |
| 66 | + let dimensionUnit = unit as! Dimension |
| 67 | + let rhsDimensionUnit = rhs.unit as! Dimension |
| 68 | + |
| 69 | + if unit.isEqual(rhs.unit) { |
| 70 | + return Measurement(value: doubleValue - rhs.value, unit: unit) |
| 71 | + } else { |
| 72 | + let lhsValueInTermsOfBase = dimensionUnit.converter.baseUnitValue(fromValue: doubleValue) |
| 73 | + let rhsValueInTermsOfBase = rhsDimensionUnit.converter.baseUnitValue(fromValue: rhs.value) |
| 74 | + return Measurement(value: lhsValueInTermsOfBase - rhsValueInTermsOfBase, unit: type(of: dimensionUnit).baseUnit()) |
| 75 | + } |
| 76 | + } |
33 | 77 |
|
34 |
| - open func copy(with zone: NSZone? = nil) -> Any { NSUnimplemented() } |
| 78 | + open func copy(with zone: NSZone? = nil) -> Any { |
| 79 | + return self |
| 80 | + } |
35 | 81 |
|
36 | 82 | open class var supportsSecureCoding: Bool { return true }
|
37 | 83 |
|
|
0 commit comments