56 _predicates = predicates;
67 + (CPPredicate)notPredicateWithSubpredicate:(CPPredicate)predicate
69 return [[
self alloc] initWithType:CPNotPredicateType subpredicates:[CPArray arrayWithObject:predicate]];
77 + (CPPredicate)andPredicateWithSubpredicates:(CPArray)subpredicates
79 return [[
self alloc] initWithType:CPAndPredicateType subpredicates:subpredicates];
87 + (CPPredicate)orPredicateWithSubpredicates:(CPArray)predicates
89 return [[
self alloc] initWithType:CPOrPredicateType subpredicates:predicates];
106 - (CPArray)subpredicates
111 - (CPPredicate)predicateWithSubstitutionVariables:(
CPDictionary)variables
113 var subp = [CPArray array],
114 count = [subp count],
117 for (; i < count; i++)
119 var p = [subp objectAtIndex:i],
120 sp = [p predicateWithSubstitutionVariables:variables];
131 args = [CPArray array],
132 count = [_predicates count],
136 return @"TRUEPREDICATE";
138 for (; i < count; i++)
140 var subpredicate = [_predicates objectAtIndex:i],
141 precedence = [subpredicate predicateFormat];
143 if ([subpredicate isKindOfClass:[
CPCompoundPredicate class]] && [[subpredicate subpredicates] count]> 1 && [subpredicate compoundPredicateType] != _type)
144 precedence = [
CPString stringWithFormat:@"(%s)",precedence];
146 if (precedence != nil)
147 [args addObject:precedence];
156 var count = [args count];
157 for (var j = 1; j < count; j++)
158 result +=
" AND " + [args objectAtIndex:j];
162 var count = [args count];
163 for (var j = 1; j < count; j++)
164 result +=
" OR " + [args objectAtIndex:j];
171 - (BOOL)evaluateWithObject:(
id)object
176 - (BOOL)evaluateWithObject:(
id)object substitutionVariables:(
CPDictionary)variables
179 count = [_predicates count],
185 for (; i < count; i++)
187 var predicate = [_predicates objectAtIndex:i];
191 case CPNotPredicateType:
return ![predicate evaluateWithObject:object substitutionVariables:variables];
194 result = [predicate evaluateWithObject:object substitutionVariables:variables];
196 result = result && [predicate evaluateWithObject:object substitutionVariables:variables];
201 case CPOrPredicateType:
if ([predicate evaluateWithObject:
object substitutionVariables:variables])
212 if (
self === anObject)
215 if (anObject === nil || anObject.isa !==
self.isa || _type !== [anObject compoundPredicateType] || ![_predicates isEqualToArray:[anObject subpredicates]])
230 _predicates = [coder decodeObjectForKey:@"CPCompoundPredicateSubpredicates"];
231 _type = [coder decodeIntForKey:@"CPCompoundPredicateType"];
239 [coder encodeObject:_predicates forKey:@"CPCompoundPredicateSubpredicates"];
240 [coder encodeInt:_type forKey:@"CPCompoundPredicateType"];