-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDefinedUnit.swift
More file actions
34 lines (31 loc) · 1.24 KB
/
DefinedUnit.swift
File metadata and controls
34 lines (31 loc) · 1.24 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
/// A predefined unit, which has an identifying symbol, defined quantity, and conversion information.
struct DefinedUnit: Hashable, Sendable {
let name: String
let symbol: String
let dimension: [Quantity: Fraction]
let coefficient: Double
let constant: Double
init(name: String, symbol: String, dimension: [Quantity: Fraction], coefficient: Double = 1, constant: Double = 0) throws {
guard !symbol.isEmpty else {
throw UnitError.invalidSymbol(message: "Symbol cannot be empty")
}
for operatorSymbol in OperatorSymbols.allCases {
guard !symbol.contains(operatorSymbol.rawValue) else {
throw UnitError.invalidSymbol(message: "'\(name)' Symbol cannot contain '\(operatorSymbol.rawValue)'")
}
}
guard !symbol.contains(" ") else {
throw UnitError.invalidSymbol(message: "'\(name)' Symbol cannot contain spaces")
}
self.name = name
self.symbol = symbol
self.dimension = dimension
self.coefficient = coefficient
self.constant = constant
}
}
extension DefinedUnit: Equatable {
public static func == (lhs: DefinedUnit, rhs: DefinedUnit) -> Bool {
return lhs.symbol == rhs.symbol
}
}