4 Copyright (C) 2005, Helge Hess
7 Author: Helge Hess <helge.hess@opengroupware.org>
9 This file is part of libFoundation.
11 Permission to use, copy, modify, and distribute this software and its
12 documentation for any purpose and without fee is hereby granted, provided
13 that the above copyright notice appear in all copies and that both that
14 copyright notice and this permission notice appear in supporting
17 We disclaim all warranties with regard to this software, including all
18 implied warranties of merchantability and fitness, in no event shall
19 we be liable for any special, indirect or consequential damages or any
20 damages whatsoever resulting from loss of use, data or profits, whether in
21 an action of contract, negligence or other tortious action, arising out of
22 or in connection with the use or performance of this software.
25 #include "NSCompoundPredicate.h"
30 @implementation NSCompoundPredicate
32 + (NSPredicate *)andPredicateWithSubpredicates:(NSArray *)_subs
34 return [[[self alloc] initWithType:NSAndPredicateType subpredicates:_subs]
37 + (NSPredicate *)orPredicateWithSubpredicates:(NSArray *)_subs
39 return [[[self alloc] initWithType:NSOrPredicateType subpredicates:_subs]
42 + (NSPredicate *)notPredicateWithSubpredicates:(NSArray *)_subs
44 return [[[self alloc] initWithType:NSNotPredicateType subpredicates:_subs]
48 - (id)initWithType:(NSCompoundPredicateType)_type subpredicates:(NSArray *)_s
50 if ((self = [super init]) != nil) {
56 return [self initWithType:NSNotPredicateType subpredicates:nil];
67 - (NSCompoundPredicateType)compoundPredicateType
72 - (NSArray *)subpredicates
79 - (BOOL)evaluateWithObject:(id)_object
83 for (i = 0, count = [self->subs count]; i < count; i++) {
86 ok = [[self->subs objectAtIndex:i] evaluateWithObject:_object];
88 /* Note: we treat NOT as a "AND (NOT x)*" */
89 if (self->type == NSNotPredicateType)
92 if (self->type == NSOrPredicateType) {
93 if (ok) return YES; /* short circuit */
95 else { /* AND or AND-NOT */
96 if (!ok) return NO; /* short circuit */
100 return YES; /* TOD: empty == YES? */
105 - (void)encodeWithCoder:(NSCoder *)aCoder
107 [super encodeWithCoder:aCoder];
108 [aCoder encodeValueOfObjCType:@encode(int) at:&(self->type)];
109 [aCoder encodeObject:self->subs];
111 - (id)initWithCoder:(NSCoder*)aDecoder
113 if ((self = [super initWithCoder:aDecoder]) != nil) {
114 [aDecoder decodeValueOfObjCType:@encode(int) at:&(self->type)];
115 self->subs = [[aDecoder decodeObject] retain];
120 @end /* NSCompoundPredicate */