added iOS source code
[wl-app.git] / iOS / Pods / RealmSwift / RealmSwift / SortDescriptor.swift
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2015 Realm Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 ////////////////////////////////////////////////////////////////////////////
18
19 import Foundation
20 import Realm
21
22 /**
23  A `SortDescriptor` stores a key path and a sort order for use with `sorted(sortDescriptors:)`. It is similar to
24  `NSSortDescriptor`, but supports only the subset of functionality which can be efficiently run by Realm's query engine.
25  */
26 public struct SortDescriptor {
27
28     // MARK: Properties
29
30     /// The key path which the sort descriptor orders results by.
31     public let keyPath: String
32
33     /// Whether this descriptor sorts in ascending or descending order.
34     public let ascending: Bool
35
36     /// Converts the receiver to an `RLMSortDescriptor`.
37     internal var rlmSortDescriptorValue: RLMSortDescriptor {
38         return RLMSortDescriptor(keyPath: keyPath, ascending: ascending)
39     }
40
41     // MARK: Initializers
42
43     /**
44      Creates a sort descriptor with the given key path and sort order values.
45
46      - parameter keyPath:   The key path which the sort descriptor orders results by.
47      - parameter ascending: Whether the descriptor sorts in ascending or descending order.
48      */
49     public init(keyPath: String, ascending: Bool = true) {
50         self.keyPath = keyPath
51         self.ascending = ascending
52     }
53
54     // MARK: Functions
55
56     /// Returns a copy of the sort descriptor with the sort order reversed.
57     public func reversed() -> SortDescriptor {
58         return SortDescriptor(keyPath: keyPath, ascending: !ascending)
59     }
60 }
61
62 // MARK: CustomStringConvertible
63
64 extension SortDescriptor: CustomStringConvertible {
65     /// A human-readable description of the sort descriptor.
66     public var description: String {
67         let direction = ascending ? "ascending" : "descending"
68         return "SortDescriptor(keyPath: \(keyPath), direction: \(direction))"
69     }
70 }
71
72 // MARK: Equatable
73
74 extension SortDescriptor: Equatable {
75     /// Returns whether the two sort descriptors are equal.
76     public static func == (lhs: SortDescriptor, rhs: SortDescriptor) -> Bool {
77         return lhs.keyPath == rhs.keyPath &&
78             lhs.ascending == lhs.ascending
79     }
80 }
81
82 // MARK: StringLiteralConvertible
83
84 extension SortDescriptor: ExpressibleByStringLiteral {
85
86     public typealias UnicodeScalarLiteralType = StringLiteralType
87     public typealias ExtendedGraphemeClusterLiteralType = StringLiteralType
88
89     /**
90      Creates a `SortDescriptor` out of a Unicode scalar literal.
91
92      - parameter unicodeScalarLiteral: Property name literal.
93     */
94     public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) {
95         self.init(keyPath: value)
96     }
97
98     /**
99      Creates a `SortDescriptor` out of a character literal.
100
101      - parameter extendedGraphemeClusterLiteral: Property name literal.
102      */
103     public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) {
104         self.init(keyPath: value)
105     }
106
107     /**
108      Creates a `SortDescriptor` out of a string literal.
109
110      - parameter stringLiteral: Property name literal.
111      */
112     public init(stringLiteral value: StringLiteralType) {
113         self.init(keyPath: value)
114     }
115 }
116
117 // MARK: Unavailable
118
119 extension SortDescriptor {
120     @available(*, unavailable, renamed: "init(keyPath:ascending:)")
121     public init(property: String, ascending: Bool = true) { fatalError() }
122
123     @available(*, unavailable, renamed: "keyPath")
124     public var property: String { fatalError() }
125 }