Source

cocoa-stuff / NSArray+ArrayTools.m

/* Copyright (c) 2008 Sven Weidauer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the 
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 */

#import "NSArray+ArrayTools.h"


@interface TransformingArray : NSArray {
	NSArray *baseArray;
	id object;
	SEL selector;
}

@property (retain) NSArray *baseArray;
@property (retain) id object;
@property SEL selector;

- (id) transformItem: (id) item;

@end

@implementation TransformingArray

@synthesize object, selector, baseArray;

- (id) objectAtIndex: (NSUInteger) index;
{
	id item = [baseArray objectAtIndex: index];
	return [self transformItem: item];
}

- (NSUInteger) count;
{
	return [baseArray count];
}

- (id) transformItem: (id) item
{
	id result = nil;
	if ([object respondsToSelector: selector]) {
		result = [object performSelector: selector withObject: item];
	} 
	
	if (nil == result) result = [NSNull null];
	return result;
}

@end

@interface PrependStringArray : TransformingArray {
}
@end

@implementation  PrependStringArray

- (id) transformItem: (id) item;
{
	if ([object isKindOfClass: [NSString class]]) return [object stringByAppendingFormat: @"%@", item];
	else return [NSNull null];
}

@end

@interface TransformingArray2 : TransformingArray {
}
@end

@implementation TransformingArray2 

- (id) transformItem: (id) item
{
	id result = nil;
	
	if ([item respondsToSelector: selector]) {
		if (nil == object) result = [item performSelector: selector];
		else result = [item performSelector: selector withObject: object];
	}
	
	if (nil == result) result = [NSNull null];
	return result;
}

@end


@implementation NSArray (ArrayToolsAdditions) 

- (NSIndexSet *) indexSetOfAllObjects;
{
	NSRange range;
	range.location = 0;
	range.length = [self count];
	return [NSIndexSet indexSetWithIndexesInRange: range];
}

- (NSIndexSet *) indexSetOfObjectsKindOf: (Class) class;
{
	NSMutableIndexSet *indices = [NSMutableIndexSet indexSet];
	NSInteger index = 0;
	for (id object in self) {
		if ([object isKindOfClass: class]) [indices addIndex: index];
		++index;
	}
	return [[indices copy] autorelease];
}

- (NSArray *) arrayByPrependingStringsWithString: (NSString *) string;
{
	TransformingArray *newArray = [[[PrependStringArray alloc] init] autorelease];
	newArray.baseArray = self;
	newArray.object = string;
	return newArray;
}

- (NSArray *) arrayByPerformingSelector: (SEL) selector withItemsOnObject: (id) object;
{
	TransformingArray *newArray = [[[TransformingArray alloc] init] autorelease];
	newArray.baseArray = self;
	newArray.object = object;
	newArray.selector = selector;
	return newArray;
}

- (NSArray *) arrayByPerformingSelector: (SEL) selector;
{
	return [self arrayByPerformingSelector: selector withObject: nil];
}

- (NSArray *) arrayByPerformingSelector: (SEL) selector withObject: (id) object;
{
	TransformingArray2 *newArray = [[[TransformingArray2 alloc] init] autorelease];
	newArray.baseArray = self;
	newArray.object = object;
	newArray.selector = selector;
	return newArray;
}

- (NSArray *) arrayByAppendingPathComponentToStrings: (NSString *) pathComponent;
{
	return [self arrayByPerformingSelector: @selector( stringByAppendingPathComponent: ) withObject: pathComponent];
}

@end