forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSMeasurement.swift
95 lines (76 loc) · 3.85 KB
/
NSMeasurement.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
open class NSMeasurement : NSObject, NSCopying, NSSecureCoding {
open private(set) var unit: Unit
open private(set) var doubleValue: Double
public init(doubleValue: Double, unit: Unit) {
self.unit = unit
self.doubleValue = doubleValue
}
open func canBeConverted(to unit: Unit) -> Bool {
return self.unit is Dimension && unit is Dimension && type(of: unit).isSubclass(of: type(of: self.unit))
}
open func converting(to otherUnit: Unit) -> Measurement<Unit> {
precondition(canBeConverted(to: otherUnit))
if unit.isEqual(otherUnit) {
return Measurement(value: doubleValue, unit: otherUnit)
} else {
let dimensionUnit = unit as! Dimension
let valueInTermsOfBase = dimensionUnit.converter.baseUnitValue(fromValue: doubleValue)
if otherUnit.isEqual(type(of: dimensionUnit).baseUnit()) {
return Measurement(value: valueInTermsOfBase, unit: otherUnit)
} else {
let otherDimensionUnit = otherUnit as! Dimension
let otherValueFromTermsOfBase = otherDimensionUnit.converter.value(fromBaseUnitValue: valueInTermsOfBase)
return Measurement(value: otherValueFromTermsOfBase, unit: otherUnit)
}
}
}
open func adding(_ rhs: Measurement<Unit>) -> Measurement<Unit> {
precondition(unit is Dimension)
precondition(rhs.unit is Dimension)
let dimensionUnit = unit as! Dimension
let rhsDimensionUnit = rhs.unit as! Dimension
if unit.isEqual(rhs.unit) {
return Measurement(value: doubleValue + rhs.value, unit: unit)
} else {
let lhsValueInTermsOfBase = dimensionUnit.converter.baseUnitValue(fromValue: doubleValue)
let rhsValueInTermsOfBase = rhsDimensionUnit.converter.baseUnitValue(fromValue: rhs.value)
return Measurement(value: lhsValueInTermsOfBase + rhsValueInTermsOfBase, unit: type(of: dimensionUnit).baseUnit())
}
}
open func subtracting(_ rhs: Measurement<Unit>) -> Measurement<Unit> {
precondition(unit is Dimension)
precondition(rhs.unit is Dimension)
let dimensionUnit = unit as! Dimension
let rhsDimensionUnit = rhs.unit as! Dimension
if unit.isEqual(rhs.unit) {
return Measurement(value: doubleValue - rhs.value, unit: unit)
} else {
let lhsValueInTermsOfBase = dimensionUnit.converter.baseUnitValue(fromValue: doubleValue)
let rhsValueInTermsOfBase = rhsDimensionUnit.converter.baseUnitValue(fromValue: rhs.value)
return Measurement(value: lhsValueInTermsOfBase - rhsValueInTermsOfBase, unit: type(of: dimensionUnit).baseUnit())
}
}
open func copy(with zone: NSZone? = nil) -> Any {
return self
}
open class var supportsSecureCoding: Bool { return true }
open func encode(with aCoder: NSCoder) { NSUnimplemented() }
public required init?(coder aDecoder: NSCoder) { NSUnimplemented() }
}
extension NSMeasurement : _StructTypeBridgeable {
public typealias _StructType = Measurement<Unit>
public func _bridgeToSwift() -> Measurement<Unit> {
return _StructType._unconditionallyBridgeFromObjectiveC(self)
}
}