OSDN Git Service

ApplicationDirecroriesの中のアプリケーションに依存する部分を分離した
[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             
67         case .and: return NSCompoundPredicate(andPredicateWithSubpredicates: p)
68             
69         case .or:  return NSCompoundPredicate(orPredicateWithSubpredicates: p)
70             
71         case .not: return NSCompoundPredicate(notPredicateWithSubpredicate: self.compound(predicates: p))
72             
73         }
74     }
75 }
76
77 public extension NSPredicate {
78     
79     public static func not(_ predicate: NSPredicate) -> NSPredicate {
80         
81         return NSCompoundPredicate(notPredicateWithSubpredicate: predicate)
82     }
83     
84     public static func and(_ predicates: [NSPredicate]) -> NSPredicate {
85         
86         return NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
87     }
88     
89     public static func or(_ predicates: [NSPredicate]) -> NSPredicate {
90         
91         return NSCompoundPredicate(orPredicateWithSubpredicates: predicates)
92     }
93     
94     public static func `true`(_ property: String) -> NSPredicate {
95         
96         return NSPredicate(format: "%K = TRUE", property)
97     }
98     
99     public static func `false`(_ property: String) -> NSPredicate {
100         
101         return NSPredicate(format: "%K != TRUE", property)
102     }
103     
104     public static func isNil(_ property: String) -> NSPredicate {
105         
106         return NSPredicate(format: "%K = NULL", property)
107     }
108     
109     public static func isNotNil(_ property: String) -> NSPredicate {
110         
111         return NSPredicate(format: "%K != NULL", property)
112     }
113     
114     //
115     public func and(_ predicate: NSPredicate) -> NSPredicate {
116         
117         return self.compound(predicates: [predicate], type: .and)
118     }
119     
120     public func or(_ predicate: NSPredicate) -> NSPredicate {
121         
122         return self.compound(predicates: [predicate], type: .or)
123     }
124 }
125
126 public extension NSPredicate {
127     
128     public static var empty: NSPredicate {
129         
130         return NSPredicate(value: true)
131     }
132 }
133
134 //
135 //
136 //
137 //precedencegroup PredicateAddPrecedence {
138 //    associativity: right
139 //    higherThan: AssignmentPrecedence
140 //}
141 //precedencegroup PredicatePrecedence {
142 //    higherThan: PredicateAddPrecedence
143 //}
144 //
145 //infix operator |==| : PredicatePrecedence
146 //infix operator |!=| : PredicatePrecedence
147 //
148 //infix operator |<*| : PredicatePrecedence
149 //
150 //infix operator |+| : PredicateAddPrecedence
151 //infix operator ||| : PredicateAddPrecedence
152 //prefix operator |!|
153 //
154 //public extension NSPredicate {
155 //
156 //    static func |+| (lhs: NSPredicate, rhs: NSPredicate) -> NSPredicate {
157 //
158 //        return .and([lhs, rhs])
159 //    }
160 //
161 //    static func ||| (lhs: NSPredicate, rhs: NSPredicate) -> NSPredicate {
162 //
163 //        return .or([lhs, rhs])
164 //    }
165 //
166 //    static prefix func |!| (lhs: NSPredicate) -> NSPredicate {
167 //
168 //        return .not(lhs)
169 //    }
170 //}
171 //
172 //public struct PredicateKey {
173 //
174 //    let name: String
175 //
176 //    init(_ name: String) {
177 //
178 //        self.name = name
179 //    }
180 //
181 //    static func |==| (lhs: PredicateKey, rhs: Any) -> NSPredicate {
182 //
183 //        return NSPredicate(lhs.name, equal: rhs)
184 //    }
185 //
186 //    static func |!=| (lhs: PredicateKey, rhs: Any) -> NSPredicate {
187 //
188 //        return NSPredicate(lhs.name, notEqual: rhs)
189 //    }
190 //
191 //    static func |<*| (lhs: PredicateKey, rhs: [Any]) -> NSPredicate {
192 //
193 //        return NSPredicate(lhs.name, valuesIn: rhs)
194 //    }
195 //}