OSDN Git Service

関数名を変更
[kcd/KCD.git] / KCD / NSPredicateExtension.swift
1 //
2 //  NSPredicateExtension.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/10/07.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Foundation
10
11 public extension NSPredicate {
12     
13     private convenience init(expression property: String, _ operation: String, _ value: Any) {
14         
15         self.init(format: "%K \(operation) %@", argumentArray: [property, value])
16     }
17     
18 }
19
20 public extension NSPredicate {
21     
22     public convenience init(_ property: String, equal value: Any) {
23         
24         self.init(expression: property, "=", value)
25     }
26     
27     public convenience init(_ property: String, notEqual value: Any) {
28         
29         self.init(expression: property, "!=", value)
30     }
31     
32     public convenience init(_ property: String, equalOrGreaterThan value: Any) {
33         
34         self.init(expression: property, ">=", value)
35     }
36     
37     public convenience init(_ property: String, equalOrLessThan value: Any) {
38         
39         self.init(expression: property, "<=", value)
40     }
41     
42     public convenience init(_ property: String, greaterThan value: Any) {
43         
44         self.init(expression: property, ">", value)
45     }
46     
47     public convenience init(_ property: String, lessThan value: Any) {
48         
49         self.init(expression: property, "<", value)
50     }
51     
52     public convenience init(_ property: String, valuesIn values: [Any]) {
53         
54         self.init(format: "%K IN %@", argumentArray: [property, values])
55     }
56     
57 }
58
59 public extension NSPredicate {
60     
61     public func compound(predicates: [NSPredicate], type: NSCompoundPredicate.LogicalType = .and) -> NSPredicate {
62         
63         let p = [self] + predicates
64         
65         switch type {
66         case .and: return NSCompoundPredicate(andPredicateWithSubpredicates: p)
67         case .or:  return NSCompoundPredicate(orPredicateWithSubpredicates: p)
68         case .not: return NSCompoundPredicate(notPredicateWithSubpredicate: self.compound(predicates: p))
69         }
70     }
71 }
72
73 public extension NSPredicate {
74     
75     public static func not(_ predicate: NSPredicate) -> NSPredicate {
76         
77         return NSCompoundPredicate(notPredicateWithSubpredicate: predicate)
78     }
79     
80     public static func and(_ predicates: [NSPredicate]) -> NSPredicate {
81         
82         return NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
83     }
84     
85     public static func or(_ predicates: [NSPredicate]) -> NSPredicate {
86         
87         return NSCompoundPredicate(orPredicateWithSubpredicates: predicates)
88     }
89     
90     public static func `true`(_ property: String) -> NSPredicate {
91         
92         return NSPredicate(format: "%K = TRUE", property)
93     }
94     
95     public static func `false`(_ property: String) -> NSPredicate {
96         
97         return NSPredicate(format: "%K != TRUE", property)
98     }
99     
100     public static func isNil(_ property: String) -> NSPredicate {
101         
102         return NSPredicate(format: "%K = NULL", property)
103     }
104     
105     public static func isNotNil(_ property: String) -> NSPredicate {
106         
107         return NSPredicate(format: "%K != NULL", property)
108     }
109     
110     //
111     public func and(_ predicate: NSPredicate) -> NSPredicate {
112         
113         return self.compound(predicates: [predicate], type: .and)
114     }
115     
116     public func or(_ predicate: NSPredicate) -> NSPredicate {
117         
118         return self.compound(predicates: [predicate], type: .or)
119     }
120 }
121
122 public extension NSPredicate {
123     
124     public static var empty: NSPredicate {
125         
126         return NSPredicate(value: true)
127     }
128 }
129
130 //
131 //
132 //
133 //precedencegroup PredicateAddPrecedence {
134 //    associativity: right
135 //    higherThan: AssignmentPrecedence
136 //}
137 //precedencegroup PredicatePrecedence {
138 //    higherThan: PredicateAddPrecedence
139 //}
140 //
141 //infix operator |==| : PredicatePrecedence
142 //infix operator |!=| : PredicatePrecedence
143 //
144 //infix operator |<*| : PredicatePrecedence
145 //
146 //infix operator |+| : PredicateAddPrecedence
147 //infix operator ||| : PredicateAddPrecedence
148 //prefix operator |!|
149 //
150 //public extension NSPredicate {
151 //
152 //    static func |+| (lhs: NSPredicate, rhs: NSPredicate) -> NSPredicate {
153 //
154 //        return .and([lhs, rhs])
155 //    }
156 //
157 //    static func ||| (lhs: NSPredicate, rhs: NSPredicate) -> NSPredicate {
158 //
159 //        return .or([lhs, rhs])
160 //    }
161 //
162 //    static prefix func |!| (lhs: NSPredicate) -> NSPredicate {
163 //
164 //        return .not(lhs)
165 //    }
166 //}
167 //
168 //public struct PredicateKey {
169 //
170 //    let name: String
171 //
172 //    init(_ name: String) {
173 //
174 //        self.name = name
175 //    }
176 //
177 //    static func |==| (lhs: PredicateKey, rhs: Any) -> NSPredicate {
178 //
179 //        return NSPredicate(lhs.name, equal: rhs)
180 //    }
181 //
182 //    static func |!=| (lhs: PredicateKey, rhs: Any) -> NSPredicate {
183 //
184 //        return NSPredicate(lhs.name, notEqual: rhs)
185 //    }
186 //
187 //    static func |<*| (lhs: PredicateKey, rhs: [Any]) -> NSPredicate {
188 //
189 //        return NSPredicate(lhs.name, valuesIn: rhs)
190 //    }
191 //}