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