Skip to content
This repository
tree: 10f24c24d4
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 280 lines (244 sloc) 11.384 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 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
//
// NSPredicate+SKAdditions.m
// StackKit
/**
Copyright (c) 2011 Dave DeLong
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**/

#import "NSPredicate+SKAdditions.h"
#import "SKFunctions.h"

@implementation NSPredicate (SKAdditions)

- (BOOL) isComparisonPredicateWithLeftKeyPaths:(NSArray *)leftKeyPaths operator:(NSPredicateOperatorType)operator rightExpressionType:(NSExpressionType)rightType {
if ([self isKindOfClass:[NSComparisonPredicate class]] == NO) { return NO; }
NSComparisonPredicate * comp = (NSComparisonPredicate *)self;

NSExpression * left = [comp leftExpression];
if ([left expressionType] != NSKeyPathExpressionType) { return NO; }

NSString * leftKeyPath = [left keyPath];
if ([leftKeyPaths containsObject:leftKeyPath] == NO) { return NO; }

//pass SKAnyPredicateOperator to ignore operator
if (operator != SKAnyPredicateOperator) {
if ([comp predicateOperatorType] != operator) { return NO; }
}

NSExpression * right = [comp rightExpression];
if ([right expressionType] != rightType) { return NO; }

return YES;
}

- (BOOL) isPredicateWithConstantValueEqualToLeftKeyPath:(NSString *)leftKeyPath {
return [self isComparisonPredicateWithLeftKeyPaths:[NSArray arrayWithObject:leftKeyPath]
operator:NSEqualToPredicateOperatorType
rightExpressionType:NSConstantValueExpressionType];
}

- (NSArray *) subPredicatesWithLeftExpression:(NSExpression *)left {
if ([self isKindOfClass:[NSCompoundPredicate class]]) {
NSCompoundPredicate * compound = (NSCompoundPredicate *)self;
NSArray * subPredicates = [compound subpredicates];

NSMutableArray * matches = [NSMutableArray array];
for (NSPredicate * subPredicate in subPredicates) {
NSArray * subPredicateMatches = [subPredicate subPredicatesWithLeftExpression:left];
[matches addObjectsFromArray:subPredicateMatches];
}
return matches;
} else if ([self isKindOfClass:[NSComparisonPredicate class]]) {
NSComparisonPredicate * comparison = (NSComparisonPredicate *)self;
NSExpression * leftExpression = [comparison leftExpression];
if ([leftExpression isEqual:left]) {
return [NSArray arrayWithObject:self];
}
}
return nil;
}

- (NSArray *) subPredicatesWithLeftKeyPath:(NSString *)left {
return [self subPredicatesWithLeftExpression:[NSExpression expressionForKeyPath:left]];
}

- (NSPredicate *) subPredicateForLeftExpression:(NSExpression *)left {
NSArray * matches = [self subPredicatesWithLeftExpression:left];
if ([matches count] > 0) {
return [matches objectAtIndex:0];
}
return nil;
}

- (NSPredicate *) subPredicateForLeftKeyPath:(NSString *)left {
return [self subPredicateForLeftExpression:[NSExpression expressionForKeyPath:left]];
}

- (id) constantValueForLeftExpression:(NSExpression *)left {
NSComparisonPredicate * comparison = (NSComparisonPredicate *)[self subPredicateForLeftExpression:left];
if (comparison == nil) { return nil; }
if ([[comparison rightExpression] expressionType] != NSConstantValueExpressionType) { return nil; }

return [[comparison rightExpression] constantValue];
}

- (id) constantValueForLeftKeyPath:(NSString *)left {
return [self constantValueForLeftExpression:[NSExpression expressionForKeyPath:left]];
}

- (id) constantValueForOperator:(NSPredicateOperatorType)operator {
if ([self isKindOfClass:[NSComparisonPredicate class]] == NO) { return nil; }
NSComparisonPredicate * comparison = (NSComparisonPredicate *)self;
if ([comparison predicateOperatorType] != operator) { return nil; }
NSExpression * rightExpression = [comparison rightExpression];
if ([rightExpression expressionType] != NSConstantValueExpressionType) { return nil; }
return [rightExpression constantValue];
}

- (id) constantValueForOneOfOperators:(NSArray *)operators {
for (NSNumber * op in operators) {
NSPredicateOperatorType operator = [op unsignedIntegerValue];
id value = [self constantValueForOperator:operator];
if (value) { return value; }
}
return nil;
}

- (NSPredicate *) predicateByRemovingSubPredicateWithLeftExpression:(NSExpression *)left {
if ([self isKindOfClass:[NSCompoundPredicate class]]) {
NSCompoundPredicate * compound = (NSCompoundPredicate *)self;
NSMutableArray * newSubpredicates = [NSMutableArray array];
NSArray * currentSubpredicates = [compound subpredicates];

for (NSPredicate * subPredicate in currentSubpredicates) {
NSPredicate * newVersion = [subPredicate predicateByRemovingSubPredicateWithLeftExpression:left];
if (newVersion != nil) {
[newSubpredicates addObject:newVersion];
}
}

if ([newSubpredicates count] > 0) {
NSCompoundPredicate * newCompound = [[NSCompoundPredicate alloc] initWithType:[compound compoundPredicateType] subpredicates:newSubpredicates];
return [newCompound autorelease];
} else {
return nil;
}
} else if ([self isKindOfClass:[NSComparisonPredicate class]]) {
NSComparisonPredicate * comparison = (NSComparisonPredicate *)self;
NSExpression * leftExpression = [comparison leftExpression];
if ([leftExpression isEqual:left]) {
return nil;
}
return comparison;
}
return nil;
}

- (NSPredicate *) predicateByReplacingLeftKeyPathsFromMapping:(NSDictionary *)mapping {
if ([self isKindOfClass:[NSComparisonPredicate class]]) {
NSComparisonPredicate * compP = (NSComparisonPredicate *)self;
NSExpression * left = [compP leftExpression];
if ([left expressionType] == NSKeyPathExpressionType) {
NSString * keyPath = [left keyPath];
NSString * mappedKeyPath = [mapping objectForKey:keyPath];
if (mappedKeyPath != nil) {
NSComparisonPredicate * translated = [[NSComparisonPredicate alloc] initWithLeftExpression:[NSExpression expressionForKeyPath:mappedKeyPath]
rightExpression:[compP rightExpression]
modifier:[compP comparisonPredicateModifier]
type:[compP predicateOperatorType]
options:[compP options]];
return [translated autorelease];
}
}
} else if ([self isKindOfClass:[NSCompoundPredicate class]]) {
NSCompoundPredicate * compoundP = (NSCompoundPredicate *)self;
NSArray * subpredicates = [compoundP subpredicates];
NSMutableArray * translated = [NSMutableArray array];
for (NSPredicate * subpredicate in subpredicates) {
NSPredicate * newSubpredicate = [subpredicate predicateByReplacingLeftKeyPathsFromMapping:mapping];
if (newSubpredicate != nil) {
[translated addObject:newSubpredicate];
}
}
if ([translated count] == 0) { return nil; }
NSCompoundPredicate * newCompound = [[NSCompoundPredicate alloc] initWithType:[compoundP compoundPredicateType]
subpredicates:translated];
return [newCompound autorelease];
} else {
return nil;
}
return self;
}

- (BOOL) isSimpleAndPredicate {
if ([self isKindOfClass:[NSCompoundPredicate class]] == NO) { return NO; }
NSCompoundPredicate * compound = (NSCompoundPredicate *)self;
if ([compound compoundPredicateType] != NSAndPredicateType) { return NO; }

NSArray * subpredicates = [compound subpredicates];
BOOL ok = YES;
for (NSPredicate * subpredicate in subpredicates) {
ok &= [subpredicate isKindOfClass:[NSComparisonPredicate class]];
}

return ok;
}

- (SKRange) rangeOfConstantValuesForLeftKeyPath:(NSString *)left {
SKRange result = SKRangeNotFound;
if ([self isComparisonPredicateWithLeftKeyPaths:[NSArray arrayWithObject:left]
operator:SKAnyPredicateOperator
rightExpressionType:NSConstantValueExpressionType]) {
NSComparisonPredicate * comparison = (NSComparisonPredicate *)self;

id upperValue = [comparison constantValueForOneOfOperators:[NSArray arrayWithObjects:
[NSNumber numberWithUnsignedInteger:NSLessThanPredicateOperatorType],
[NSNumber numberWithUnsignedInteger:NSLessThanOrEqualToPredicateOperatorType],
nil]];
if (upperValue) {
result.upper = upperValue;
}

id lowerValue = [comparison constantValueForOneOfOperators:[NSArray arrayWithObjects:
[NSNumber numberWithUnsignedInteger:NSGreaterThanPredicateOperatorType],
[NSNumber numberWithUnsignedInteger:NSGreaterThanOrEqualToPredicateOperatorType],
nil]];
if (lowerValue) {
result.lower = lowerValue;
}
} else if ([self isKindOfClass:[NSCompoundPredicate class]]) {
NSCompoundPredicate * compound = (NSCompoundPredicate *)self;
NSArray * subpredicates = [compound subPredicatesWithLeftKeyPath:left];
for (NSPredicate * predicate in subpredicates) {
SKRange subpredicateRange = [predicate rangeOfConstantValuesForLeftKeyPath:left];
if (result.lower == SKNotFound && subpredicateRange.lower != SKNotFound) {
result.lower = subpredicateRange.lower;
}
if (result.upper == SKNotFound && subpredicateRange.upper != SKNotFound) {
result.upper = subpredicateRange.upper;
}
if (result.lower != SKNotFound && result.upper != SKNotFound) { break; }
}
}
return result;
}

- (NSSet *) leftKeyPaths {
NSMutableSet * paths = [NSMutableSet set];
if ([self isKindOfClass:[NSCompoundPredicate class]]) {
NSCompoundPredicate * compound = (NSCompoundPredicate *)self;
for (NSPredicate * subpredicate in [compound subpredicates]) {
[paths unionSet:[subpredicate leftKeyPaths]];
}
} else {
NSComparisonPredicate * comparison = (NSComparisonPredicate *)self;
if ([[comparison leftExpression] expressionType] == NSKeyPathExpressionType) {
[paths addObject:[[comparison leftExpression] keyPath]];
}
}
return paths;
}

//this has to return an NSNumber. If it returned a BOOL, then we'd get an EXC_BAD_ACCESS when using it in our FUNCTION() predicate
- (NSNumber *) sk_matchesRecognizedKeyPathsAndOperators:(NSDictionary *)keyPaths {
BOOL matches = YES;
for (NSString * keyPath in keyPaths) {
NSArray * allowedOperatorsForKeyPath = [keyPaths objectForKey:keyPath];
NSArray * subPredicatesUsingKeyPath = [self subPredicatesWithLeftKeyPath:keyPath];
for (NSComparisonPredicate * subPredicate in subPredicatesUsingKeyPath) {
NSPredicateOperatorType operator = [subPredicate predicateOperatorType];
NSNumber * boxed = [NSNumber numberWithUnsignedInteger:operator];
if ([allowedOperatorsForKeyPath containsObject:boxed] == NO) {
matches = NO;
break;
}
}
if (matches == NO) { break; }
}
return [NSNumber numberWithBool:matches];
}

@end
Something went wrong with that request. Please try again.