/*!
@header ECKeyToListMap
@abstract Module of Encore
@availability OS X, GNUstep
@copyright 2004, 2005, 2006 Oliver Langer
Author: Oliver Langer
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-------------------------------------------------------------------------
Modification history
29.10.05 ola initial version
22.08.06 ola license changed
-------------------------------------------------------------------------
*/
#include
@implementation ECKeyToListMap
- init {
self = [super init];
self->keyToArrayDictionary = [[NSMutableDictionary alloc] init];
return self;
}
- (void) dealloc {
if ( nil != self->keyToArrayDictionary ) {
[self->keyToArrayDictionary release];
}
[super dealloc];
}
- (void) encodeWithCoder: (NSCoder *) aCoder {
[aCoder encodeObject: self->keyToArrayDictionary];
}
- (id) initWithCoder: (NSCoder *) aDecoder {
self = [super init];
self->keyToArrayDictionary
= (NSMutableDictionary *) [[aDecoder decodeObject] retain];
return self;
}
- (BOOL) isEqual: (id) object {
BOOL toReturn = NO;
if( nil != object ) {
if( [object isKindOfClass: [self class]] ) {
toReturn =
[((ECKeyToListMap *) object)->keyToArrayDictionary isEqual:
self->keyToArrayDictionary];
}
}
return toReturn;
}
- (unsigned) hash {
return [self->keyToArrayDictionary hash];
}
- addListItem: (id) aListItem forKey: (id) aKey {
NSMutableArray *list;
list = [self->keyToArrayDictionary objectForKey: aKey];
if( nil == list ) {
/* create list first: */
list = [[NSMutableArray alloc]init];
[self->keyToArrayDictionary setObject: list forKey: aKey];
} else {
//check wether item already exists in list
if( [list containsObject: aListItem] ) {
[[[ECIllegalArgumentException alloc]
initWithArgumentInfo:
[[NSString alloc]
initWithFormat:@"ECKeyToListMap::addListItem: Item \"%@\" already"\
" exists in specified list with key=\"%@\"", aListItem,
aKey]
] raise];
}
}
[list addObject: aListItem];
return self;
}
- (id ) allKeys {
return [[[ECArrayIterator alloc]
initWithArray: [self->keyToArrayDictionary allKeys]] autorelease];
}
- (BOOL) existsListItem: (id) aListItem forKey: (id) aKey {
BOOL toReturn = NO;
NSMutableArray *list;
list = [self->keyToArrayDictionary objectForKey: aKey];
if( nil != list ) {
toReturn = [list containsObject: aListItem];
}
return toReturn;
}
- (id ) listItemsOfKey: (id) aKey {
NSMutableArray *list;
list = [self->keyToArrayDictionary objectForKey: aKey];
if( nil != list ) {
return [[[ECArrayIterator alloc] initWithArray: list] autorelease];
} else {
return [[[ECEmptyIterator alloc] init] autorelease];
}
}
- removeListItem: (id) aListItem forKey: (id) aKey {
NSMutableArray *list;
list = [self->keyToArrayDictionary objectForKey: aKey];
if( nil != list ) {
if( [list containsObject: aListItem] ) {
[list removeObject: aListItem];
}
if( 0 == [list count] ) {
[self->keyToArrayDictionary removeObjectForKey: aKey];
}
}
return self;
}
@end