]> err.no Git - sope/blob - sope-core/NGExtensions/NGCalendarDateRange.m
Fixed minor Xcode problems reported by Stephane Corthesy. Also bumped all appropriate...
[sope] / sope-core / NGExtensions / NGCalendarDateRange.m
1 /*
2   Copyright (C) 2004 Marcus Mueller
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "NGCalendarDateRange.h"
23 #include <NGExtensions/NSCalendarDate+misc.h>
24 #include <NGExtensions/NSNull+misc.h>
25 #include "common.h"
26
27 @implementation NGCalendarDateRange
28
29 + (id)calendarDateRangeWithStartDate:(NSCalendarDate *)start
30   endDate:(NSCalendarDate *)end
31 {
32     return [[[self alloc] initWithStartDate:start endDate:end] autorelease];
33 }
34
35 - (id)initWithStartDate:(NSCalendarDate *)start endDate:(NSCalendarDate *)end {
36   NSAssert(start != nil, @"startDate MUST NOT be nil!");
37   NSAssert(end   != nil, @"endDate MUST NOT be nil!");
38   
39   if ((self = [super init])) {
40     if ([start compare:end] == NSOrderedAscending) {
41       self->startDate = [start copy];
42       self->endDate   = [end   copy];
43     }
44     else {
45       self->startDate = [end   copy];
46       self->endDate   = [start copy];
47     }
48   }
49   return self;
50 }
51
52 /* NSCopying */
53
54 - (id)copyWithZone:(NSZone *)zone {
55   /* object is immutable */
56   return [self retain];
57 }
58
59 /* accessors */
60
61 - (NSCalendarDate *)startDate {
62   return self->startDate;
63 }
64
65 - (NSCalendarDate *)endDate {
66   return self->endDate;
67 }
68
69 - (NGCalendarDateRange *)intersectionDateRange:(NGCalendarDateRange *)other {
70   NSCalendarDate *a, *b, *c, *d;
71     
72   if ([self compare:other] == NSOrderedAscending) {
73     a = self->startDate;
74     b = self->endDate;
75     c = [other startDate];
76     d = [other endDate];
77   }
78   else {
79     a = [other startDate];
80     b = [other endDate];
81     c = self->startDate;
82     d = self->endDate;
83   }
84   // [a;b[ ?< [c;d[
85   if ([b compare:c] == NSOrderedAscending)
86     return nil; // no intersection
87   // b ?< d
88   if ([b compare:d] == NSOrderedAscending) {
89     // c !< b  && b !< d -> [c;b[
90     if([c compare:b] == NSOrderedSame)
91       return nil; // no real range, thus return nil!
92     else
93       return [NGCalendarDateRange calendarDateRangeWithStartDate:c endDate:b];
94   }
95   if([c compare:d] == NSOrderedSame)
96     return nil; // no real range, thus return nil!
97   // b !> d -> [c;d[
98   return [NGCalendarDateRange calendarDateRangeWithStartDate:c endDate:d];
99 }
100
101 - (BOOL)doesIntersectWithDateRange:(NGCalendarDateRange *)_other {
102   // TODO: improve
103   if (_other == nil) return NO;
104   return [self intersectionDateRange:_other] != nil ? YES : NO;
105 }
106
107 - (NGCalendarDateRange *)unionDateRange:(NGCalendarDateRange *)other {
108   NSCalendarDate *a, *b, *c, *d;
109     
110   if ([self compare:other] == NSOrderedAscending) {
111     a = self->startDate;
112     b = self->endDate;
113     c = [other startDate];
114     d = [other endDate];
115   }
116   else {
117     a = [other startDate];
118     b = [other endDate];
119     c = self->startDate;
120     d = self->endDate;
121   }
122   if ([b compare:d] == NSOrderedAscending)
123     return [NGCalendarDateRange calendarDateRangeWithStartDate:a endDate:d];
124   
125   return [NGCalendarDateRange calendarDateRangeWithStartDate:a endDate:b];
126 }
127
128 - (BOOL)containsDate:(NSCalendarDate *)_date {
129   NSComparisonResult result;
130   
131   result = [self->startDate compare:_date];
132   if (!((result == NSOrderedSame) || (result == NSOrderedAscending)))
133     return NO;
134   result = [self->endDate compare:_date];
135   if (result == NSOrderedAscending)
136     return NO;
137   return YES;
138 }
139
140 - (BOOL)containsDateRange:(NGCalendarDateRange *)_range {
141   NSComparisonResult result;
142
143   result = [self->startDate compare:[_range startDate]];
144   if (!((result == NSOrderedSame) || (result == NSOrderedAscending)))
145     return NO;
146   result = [self->endDate compare:[_range endDate]];
147   if (result == NSOrderedAscending)
148     return NO;
149   return YES;
150 }
151
152 - (NSTimeInterval)duration {
153   return [self->endDate timeIntervalSinceDate:self->startDate];
154 }
155
156 /* comparison */
157
158 - (BOOL)isEqual:(id)other {
159   if (other == nil)
160     return NO;
161   if (other == self)
162     return YES;
163   
164   if ([other isKindOfClass:self->isa] == NO)
165     return NO;
166   
167   return ([self->startDate isEqual:[other startDate]] && 
168           [self->endDate isEqual:[other endDate]]) ? YES : NO;
169 }
170
171 - (unsigned)hash {
172   return [self->startDate hash] ^ [self->endDate hash];
173 }
174
175 - (NSComparisonResult)compare:(NGCalendarDateRange *)other {
176   return [self->startDate compare:[other startDate]];
177 }
178
179 /* description */
180
181 - (NSString *)description {
182   NSMutableString *description;
183     
184   description = [NSMutableString stringWithCapacity:64];
185
186   [description appendFormat:@"<%@[0x%x]: startDate:%@ endDate: ", 
187                  NSStringFromClass(self->isa), self, self->startDate];
188   
189   if ([self->startDate isEqual:self->endDate])
190     [description appendString:@"== startDate"];
191   else
192     [description appendFormat:@"%@", self->endDate];
193   [description appendString:@">"];
194   return description;
195 }
196
197 @end /* NGCalendarDateRange */
198
199 @implementation NSArray(NGCalendarDateRanges)
200
201 - (NSArray *)arrayByCreatingDateRangesFromObjectsWithStartDateKey:(NSString *)s
202   andEndDateKey:(NSString *)e
203 {
204   NSMutableArray *ma;
205   unsigned i, count;
206   
207   count = [self count];
208   ma    = [NSMutableArray arrayWithCapacity:count];
209   for (i = 0; i < count; i++) {
210     NGCalendarDateRange *daterange;
211     NSCalendarDate *start, *end;
212     id object;
213     
214     object = [self objectAtIndex:i];
215     start  = [object valueForKey:s];
216     end    = [object valueForKey:e];
217     
218     /* skip invalid data */
219     if (![start isNotNull]) continue;
220     if (![end   isNotNull]) continue;
221     
222     daterange =
223       [[NGCalendarDateRange alloc] initWithStartDate:start endDate:end];
224     if (daterange) [ma addObject:daterange];
225     [daterange release];
226   }
227   return ma;
228 }
229
230 - (BOOL)dateRangeArrayContainsDate:(NSCalendarDate *)_date {
231   unsigned i, count;
232   
233   if (_date == nil) 
234     return NO;
235   if ((count = [self count]) == 0)
236     return NO;
237
238   for (i = 0; i < count; i++) {
239     if ([[self objectAtIndex:i] containsDate:_date])
240       return YES;
241   }
242   return NO;
243 }
244 - (unsigned)indexOfFirstIntersectingDateRange:(NGCalendarDateRange *)_range {
245   unsigned i, count;
246   
247   if (_range == nil)
248     return NO;
249   
250   if ((count = [self count]) == 0)
251     return NSNotFound;
252
253   for (i = 0; i < count; i++) {
254     if ([[self objectAtIndex:i] doesIntersectWithDateRange:_range])
255       return i;
256   }
257   return NSNotFound;
258 }
259
260 - (NSArray *)arrayByCompactingContainedDateRanges {
261   // TODO: this is a candidate for unit testing ...
262   // TODO: pretty "slow" algorithm, improve
263   NSMutableArray *ma;
264   unsigned i, count;
265   
266   count = [self count];
267   if (count < 2)
268     return [[self copy] autorelease];
269   
270   ma = [NSMutableArray arrayWithCapacity:count];
271   [ma addObject:[self objectAtIndex:0]]; /* add first range */
272   
273   for (i = 1; i < count; i++) {
274     NGCalendarDateRange *rangeToAdd;
275     NGCalendarDateRange *availRange;
276     NGCalendarDateRange *newRange;
277     unsigned idx;
278     
279     rangeToAdd = [self objectAtIndex:i];
280     idx = [ma indexOfFirstIntersectingDateRange:rangeToAdd];
281     
282     if (idx == NSNotFound) {
283       /* range not yet covered in array */
284       [ma addObject:rangeToAdd];
285       continue;
286     }
287     
288     /* union old range and replace the entry */
289     
290     availRange = [ma objectAtIndex:idx];
291     newRange   = [availRange unionDateRange:rangeToAdd];
292     
293     [ma replaceObjectAtIndex:idx withObject:newRange];
294   }
295   /* Note: we might want to join ranges up to some "closeness" (eg 1s)? */
296   return [ma sortedArrayUsingSelector:@selector(compare:)];
297 }
298
299 @end /* NSArray(NGCalendarDateRanges) */