forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSSortDescriptor.swift
124 lines (101 loc) · 4.87 KB
/
NSSortDescriptor.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
// 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
//
import CoreFoundation
// In swift-corelibs-foundation, key-value coding is not available. Since encoding and decoding a NSSortDescriptor requires interpreting key paths, NSSortDescriptor does not conform to NSCoding or NSSecureCoding in swift-corelibs-foundation only.
open class NSSortDescriptor: NSObject, NSCopying {
open override func copy() -> Any {
return copy(with: nil)
}
open func copy(with zone: NSZone? = nil) -> Any {
return self
}
@available(*, unavailable, message: "Key-value coding is not available in swift-corelibs-foundation. Use Swift key paths instead with init(keyPath:ascending:) or init(keyPath:ascending:comparator:)", renamed: "init(keyPath:ascending:)")
public init(key: String?, ascending: Bool) { NSUnsupported() }
@available(*, unavailable, message: "Key-value coding is not available in swift-corelibs-foundation. Use Swift key paths instead with init(keyPath:ascending:) or init(keyPath:ascending:comparator:)", renamed: "init(keyPath:ascending:)")
public init(key: String?, ascending: Bool, comparator cmptr: Comparator) { NSUnsupported() }
@available(*, unavailable, message: "Key-value coding is not available in swift-corelibs-foundation. Use .keyPath instead.", renamed: "keyPath")
open var key: String? { NSUnsupported() }
@available(*, unavailable, message: "Sort descriptors cannot be decoded from archives in swift-corelibs-foundation.")
open func allowEvaluation() {}
// MARK: Available parts.
public init<Root, Value: Comparable>(keyPath: KeyPath<Root, Value>, ascending: Bool) {
self.keyPath = keyPath
self.ascending = ascending
self.lens = { ($0 as! Root)[keyPath: keyPath] }
self.comparator = { (a, b) -> ComparisonResult in
let valueA = a as! Value
let valueB = b as! Value
if valueA < valueB {
return .orderedAscending
} else if valueB < valueA {
return .orderedDescending
} else {
return .orderedSame
}
}
self.reversedSortDescriptorProducer = { return NSSortDescriptor(keyPath: keyPath, ascending: !ascending) }
}
public init<Root, Value>(keyPath: KeyPath<Root, Value>, ascending: Bool, comparator cmptr: @escaping Comparator) {
self.keyPath = keyPath
self.ascending = ascending
self.lens = { ($0 as! Root)[keyPath: keyPath] }
self.comparator = cmptr
self.reversedSortDescriptorProducer = { return NSSortDescriptor(keyPath: keyPath, ascending: !ascending, comparator: cmptr) }
}
private let lens: (Any) -> Any
private let reversedSortDescriptorProducer: () -> NSSortDescriptor
open private(set) var ascending: Bool
open private(set) var keyPath: AnyKeyPath
open private(set) var comparator: Comparator
// primitive - override this method if you want to perform comparisons differently (not key based for example)
open func compare(_ object1: Any, to object2: Any) -> ComparisonResult {
let lhs = lens(object1)
let rhs = lens(object2)
let result = comparator(lhs, rhs)
let actualResult: ComparisonResult
if ascending {
actualResult = result
} else {
switch result {
case .orderedAscending: actualResult = .orderedDescending
case .orderedDescending: actualResult = .orderedAscending
case .orderedSame: actualResult = .orderedSame
}
}
return actualResult
}
open var reversedSortDescriptor: Any {
return reversedSortDescriptorProducer()
}
}
extension NSNumber: Comparable {
public static func < (lhs: NSNumber, rhs: NSNumber) -> Bool {
return lhs.compare(rhs) == .orderedAscending
}
}
extension NSString: Comparable {
public static func < (lhs: NSString, rhs: NSString) -> Bool {
return lhs.compare(rhs._swiftObject) == .orderedAscending
}
}
extension NSDateInterval: Comparable {
public static func < (lhs: NSDateInterval, rhs: NSDateInterval) -> Bool {
return lhs.compare(rhs._swiftObject) == .orderedAscending
}
}
extension NSDate: Comparable {
public static func < (lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs._swiftObject) == .orderedAscending
}
}
extension NSIndexPath: Comparable {
public static func < (lhs: NSIndexPath, rhs: NSIndexPath) -> Bool {
return lhs.compare(rhs._swiftObject) == .orderedAscending
}
}