]> err.no Git - sope/blob - libFoundation/Foundation/NSCompoundPredicate.m
fixed some NGMail framework build issue
[sope] / libFoundation / Foundation / NSCompoundPredicate.m
1 /* 
2    NSCompoundPredicate.m
3
4    Copyright (C) 2005, Helge Hess
5    All rights reserved.
6
7    Author: Helge Hess <helge.hess@opengroupware.org>
8
9    This file is part of libFoundation.
10
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
15    documentation.
16
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.
23 */
24
25 #include "NSCompoundPredicate.h"
26 #include "NSArray.h"
27 #include "NSCoder.h"
28 #include "common.h"
29
30 @implementation NSCompoundPredicate
31
32 + (NSPredicate *)andPredicateWithSubpredicates:(NSArray *)_subs
33 {
34     return [[[self alloc] initWithType:NSAndPredicateType subpredicates:_subs]
35                autorelease];
36 }
37 + (NSPredicate *)orPredicateWithSubpredicates:(NSArray *)_subs
38 {
39     return [[[self alloc] initWithType:NSOrPredicateType subpredicates:_subs]
40                autorelease];
41 }
42 + (NSPredicate *)notPredicateWithSubpredicates:(NSArray *)_subs
43 {
44     return [[[self alloc] initWithType:NSNotPredicateType subpredicates:_subs] 
45                autorelease];
46 }
47
48 - (id)initWithType:(NSCompoundPredicateType)_type subpredicates:(NSArray *)_s
49 {
50     if ((self = [super init]) != nil) {
51     }
52     return self;
53 }
54 - (id)init
55 {
56     return [self initWithType:NSNotPredicateType subpredicates:nil];
57 }
58
59 - (void)dealloc
60 {
61     [self->subs release];
62     [super dealloc];
63 }
64
65 /* accessors */
66
67 - (NSCompoundPredicateType)compoundPredicateType
68 {
69     return self->type;
70 }
71
72 - (NSArray *)subpredicates
73 {
74     return self->subs;
75 }
76
77 /* evaluation */
78
79 - (BOOL)evaluateWithObject:(id)_object
80 {
81     unsigned i, count;
82     
83     for (i = 0, count = [self->subs count]; i < count; i++) {
84         BOOL ok;
85         
86         ok = [[self->subs objectAtIndex:i] evaluateWithObject:_object];
87         
88         /* Note: we treat NOT as a "AND (NOT x)*" */
89         if (self->type == NSNotPredicateType)
90             ok = ok ? NO : YES;
91         
92         if (self->type == NSOrPredicateType) {
93             if (ok) return YES; /* short circuit */
94         }
95         else { /* AND or AND-NOT */
96             if (!ok) return NO; /* short circuit */
97         }
98     }
99     
100     return YES; /* TOD: empty == YES? */
101 }
102
103 /* NSCoding */
104
105 - (void)encodeWithCoder:(NSCoder *)aCoder
106 {
107     [super encodeWithCoder:aCoder];
108     [aCoder encodeValueOfObjCType:@encode(int) at:&(self->type)];
109     [aCoder encodeObject:self->subs];
110 }
111 - (id)initWithCoder:(NSCoder*)aDecoder
112 {
113     if ((self = [super initWithCoder:aDecoder]) != nil) {
114         [aDecoder decodeValueOfObjCType:@encode(int) at:&(self->type)];
115         self->subs = [[aDecoder decodeObject] retain];
116     }
117     return self;
118 }
119
120 @end /* NSCompoundPredicate */
121
122 /*
123   Local Variables:
124   c-basic-offset: 4
125   tab-width: 8
126   End:
127 */