]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/NSArray+Utilities.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1174 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / SOGo / NSArray+Utilities.m
1 /* NSArray+Utilities.m - this file is part of SOGo
2  *
3  * Copyright (C) 2006 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
7  * This file is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #import <Foundation/NSString.h>
24
25 #import "NSArray+Utilities.h"
26
27 @implementation NSArray (SOGoArrayUtilities)
28
29 - (NSArray *) stringsWithFormat: (NSString *) format
30 {
31   NSMutableArray *formattedStrings;
32   NSEnumerator *objects;
33   id currentObject;
34
35   formattedStrings = [NSMutableArray arrayWithCapacity: [self count]];
36
37   objects = [self objectEnumerator];
38   currentObject = [objects nextObject];
39   while (currentObject)
40     {
41       [formattedStrings
42         addObject: [NSString stringWithFormat: format, currentObject]];
43       currentObject = [objects nextObject];
44     }
45
46   return formattedStrings;
47 }
48
49 - (NSArray *) keysWithFormat: (NSString *) format
50 {
51   NSMutableArray *formattedStrings;
52   NSEnumerator *objects;
53   id currentObject;
54
55   formattedStrings = [NSMutableArray arrayWithCapacity: [self count]];
56
57   objects = [self objectEnumerator];
58   currentObject = [objects nextObject];
59   while (currentObject)
60     {
61       [formattedStrings addObject: [currentObject keysWithFormat: format]];
62       currentObject = [objects nextObject];
63     }
64
65   return formattedStrings;
66 }
67
68 - (NSArray *) objectsForKey: (NSString *) key
69 {
70   NSMutableArray *objectsForKey;
71   unsigned int count, max;
72   id value;
73
74   max = [self count];
75   objectsForKey = [NSMutableArray arrayWithCapacity: max];
76
77   for (count = 0; count < max; count++)
78     {
79       value = [[self objectAtIndex: count] objectForKey: key];
80       [objectsForKey addObject: value];
81     }
82
83   return objectsForKey;
84 }
85
86 - (NSArray *) flattenedArray
87 {
88   NSMutableArray *flattenedArray;
89   NSEnumerator *objects;
90   id currentObject;
91
92   flattenedArray = [NSMutableArray array];
93   objects = [self objectEnumerator];
94   currentObject = [objects nextObject];
95   while (currentObject)
96     {
97       [flattenedArray addObjectsFromArray: currentObject];
98       currentObject = [objects nextObject];
99     }
100
101   return flattenedArray;
102 }
103
104 - (void) makeObjectsPerform: (SEL) selector
105                  withObject: (id) object1
106                  withObject: (id) object2
107 {
108   int count, max;
109
110   max = [self count];
111   for (count = 0; count < max; count++)
112     [[self objectAtIndex: count] performSelector: selector
113                                  withObject: object1
114                                  withObject: object2];
115 }
116
117 - (NSString *) jsonRepresentation
118 {
119   id currentElement;
120   NSMutableArray *jsonElements;
121   NSEnumerator *elements;
122   NSString *representation;
123
124   jsonElements = [NSMutableArray new];
125
126   elements = [self objectEnumerator];
127   currentElement = [elements nextObject];
128   while (currentElement)
129     {
130       [jsonElements addObject: [currentElement jsonRepresentation]];
131       currentElement = [elements nextObject];
132     }
133   representation = [NSString stringWithFormat: @"[%@]",
134                              [jsonElements componentsJoinedByString: @", "]];
135   [jsonElements release];
136
137   return representation;
138 }
139
140 - (BOOL) containsCaseInsensitiveString: (NSString *) match
141 {
142   BOOL response;
143   NSString *currentString, *cmpObject;
144   NSEnumerator *objects;
145
146   response = NO;
147
148   cmpObject = [match lowercaseString];
149   objects = [self objectEnumerator];
150   currentString = [objects nextObject];
151   while (currentString && !response)
152     if ([[currentString lowercaseString] isEqualToString: cmpObject])
153       response = YES;
154     else
155       currentString = [objects nextObject];
156
157   return response;
158 }
159
160 @end
161
162 @implementation NSMutableArray (SOGoArrayUtilities)
163
164 - (void) addObjectUniquely: (id) object
165 {
166   if (![self containsObject: object])
167     [self addObject: object];
168 }
169
170 - (void) addRange: (NSRange) newRange
171 {
172   [self addObject: NSStringFromRange (newRange)];
173 }
174
175 - (BOOL) hasRangeIntersection: (NSRange) testRange
176 {
177   NSEnumerator *ranges;
178   NSString *currentRangeString;
179   NSRange currentRange;
180   BOOL response;
181
182   response = NO;
183
184   ranges = [self objectEnumerator];
185   currentRangeString = [ranges nextObject];
186   while (!response && currentRangeString)
187     {
188       currentRange = NSRangeFromString (currentRangeString);
189       if (NSLocationInRange (testRange.location, currentRange)
190           || NSLocationInRange (NSMaxRange (testRange), currentRange))
191         response = YES;
192       else
193         currentRangeString = [ranges nextObject];
194     }
195
196   return response;
197 }
198
199 @end
200