]> err.no Git - sope/blob - sope-core/NGExtensions/NGCalendarDateRange.m
-containsDateRange: convenience method added
[sope] / sope-core / NGExtensions / NGCalendarDateRange.m
1 /*
2   Copyright (C) 2004 Marcus Mueller
3
4   This file is part of OpenGroupware.org.
5
6   OGo 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   OGo 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 OGo; 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   return ([self->startDate earlierDate:date] == self->startDate && 
130           [self->endDate compare:date] == NSOrderedDescending) ? YES : NO;
131 }
132
133 - (BOOL)containsDateRange:(NGCalendarDateRange *)_range {
134   NSComparisonResult result;
135
136   result = [self->startDate compare:[_range startDate]];
137   if (!((result == NSOrderedSame) || (result == NSOrderedAscending)))
138     return NO;
139   result = [self->endDate compare:[_range endDate]];
140   if (result == NSOrderedAscending)
141     return NO;
142   return YES;
143 }
144
145
146 /* comparison */
147
148 - (BOOL)isEqual:(id)other {
149   if (other == nil)
150     return NO;
151   if (other == self)
152     return YES;
153   
154   if ([other isKindOfClass:self->isa] == NO)
155     return NO;
156   
157   return ([self->startDate isEqual:[other startDate]] && 
158           [self->endDate isEqual:[other endDate]]) ? YES : NO;
159 }
160
161 - (unsigned)hash {
162   return [self->startDate hash] ^ [self->endDate hash];
163 }
164
165 - (NSComparisonResult)compare:(NGCalendarDateRange *)other {
166   return [self->startDate compare:[other startDate]];
167 }
168
169 /* description */
170
171 - (NSString *)description {
172   NSMutableString *description;
173     
174   description = [NSMutableString stringWithCapacity:64];
175
176   [description appendFormat:@"<%@[0x%x]: startDate:%@ endDate: ", 
177                  NSStringFromClass(self->isa), self, self->startDate];
178   
179   if ([self->startDate isEqual:self->endDate])
180     [description appendString:@"== startDate"];
181   else
182     [description appendFormat:@"%@", self->endDate];
183   [description appendString:@">"];
184   return description;
185 }
186
187 @end /* NGCalendarDateRange */
188
189 @implementation NSArray(NGCalendarDateRanges)
190
191 - (NSArray *)arrayByCreatingDateRangesFromObjectsWithStartDateKey:(NSString *)s
192   andEndDateKey:(NSString *)e
193 {
194   NSMutableArray *ma;
195   unsigned i, count;
196   
197   count = [self count];
198   ma    = [NSMutableArray arrayWithCapacity:count];
199   for (i = 0; i < count; i++) {
200     NGCalendarDateRange *daterange;
201     NSCalendarDate *start, *end;
202     id object;
203     
204     object = [self objectAtIndex:i];
205     start  = [object valueForKey:s];
206     end    = [object valueForKey:e];
207     
208     /* skip invalid data */
209     if (![start isNotNull]) continue;
210     if (![end   isNotNull]) continue;
211     
212     daterange =
213       [[NGCalendarDateRange alloc] initWithStartDate:start endDate:end];
214     if (daterange) [ma addObject:daterange];
215     [daterange release];
216   }
217   return ma;
218 }
219
220 - (BOOL)dateRangeArrayContainsDate:(NSCalendarDate *)_date {
221   unsigned i, count;
222   
223   if (_date == nil) 
224     return NO;
225   if ((count = [self count]) == 0)
226     return NO;
227
228   for (i = 0; i < count; i++) {
229     if ([[self objectAtIndex:i] containsDate:_date])
230       return YES;
231   }
232   return NO;
233 }
234 - (unsigned)indexOfFirstIntersectingDateRange:(NGCalendarDateRange *)_range {
235   unsigned i, count;
236   
237   if (_range == nil)
238     return NO;
239   
240   if ((count = [self count]) == 0)
241     return NSNotFound;
242
243   for (i = 0; i < count; i++) {
244     if ([[self objectAtIndex:i] doesIntersectWithDateRange:_range])
245       return i;
246   }
247   return NSNotFound;
248 }
249
250 - (NSArray *)arrayByCompactingContainedDateRanges {
251   // TODO: this is a candidate for unit testing ...
252   // TODO: pretty "slow" algorithm, improve
253   NSMutableArray *ma;
254   unsigned i, count;
255   
256   count = [self count];
257   if (count < 2)
258     return [[self copy] autorelease];
259   
260   ma = [NSMutableArray arrayWithCapacity:count];
261   [ma addObject:[self objectAtIndex:0]]; /* add first range */
262   
263   for (i = 1; i < count; i++) {
264     NGCalendarDateRange *rangeToAdd;
265     NGCalendarDateRange *availRange;
266     NGCalendarDateRange *newRange;
267     unsigned idx;
268     
269     rangeToAdd = [self objectAtIndex:i];
270     idx = [ma indexOfFirstIntersectingDateRange:rangeToAdd];
271     
272     if (idx == NSNotFound) {
273       /* range not yet covered in array */
274       [ma addObject:rangeToAdd];
275       continue;
276     }
277     
278     /* union old range and replace the entry */
279     
280     availRange = [ma objectAtIndex:idx];
281     newRange   = [availRange unionDateRange:rangeToAdd];
282     
283     [ma replaceObjectAtIndex:idx withObject:newRange];
284   }
285   /* Note: we might want to join ranges up to some "closeness" (eg 1s)? */
286   return [ma sortedArrayUsingSelector:@selector(compare:)];
287 }
288
289 @end /* NSArray(NGCalendarDateRanges) */