Skip to content
This repository was archived by the owner on Jul 7, 2024. It is now read-only.

Commit d78b582

Browse files
committed
clean up COItemPath
1 parent 4bb0e2e commit d78b582

3 files changed

Lines changed: 80 additions & 36 deletions

File tree

COItem.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ valuesForAttributes: (NSDictionary *)valuesForAttributes;
8080

8181
- (void)removeValueForAttribute: (NSString*)anAttribute;
8282

83+
/**
84+
* Creates the container if needed.
85+
*/
86+
- (void) addObject: (id)aValue
87+
toUnorderedAttribute: (NSString*)anAttribute
88+
type: (COType *)aType;
89+
90+
/**
91+
* Creates the container if needed.
92+
*/
93+
- (void) addObject: (id)aValue
94+
toOrderedAttribute: (NSString*)anAttribute
95+
atIndex: (NSUInteger)anIndex
96+
type: (COType *)aType;
97+
98+
8399
/** @taskunit convenience */
84100

85101
- (void) addObject: (id)aValue

COItem.m

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,60 @@ - (void)removeValueForAttribute: (NSString*)anAttribute
377377

378378
/** @taskunit convenience */
379379

380+
- (void) addObject: (id)aValue
381+
toUnorderedAttribute: (NSString*)anAttribute
382+
type: (COType *)aType
383+
{
384+
if (![aType isMultivalued] || [aType isOrdered])
385+
{
386+
[NSException raise: NSInvalidArgumentException
387+
format: @"expected unordered type"];
388+
}
389+
390+
if ([self typeForAttribute: anAttribute] == nil)
391+
{
392+
[self setValue: [aType isUnique] ? [NSSet set] : [NSCountedSet set]
393+
forAttribute: anAttribute
394+
type: aType];
395+
}
396+
397+
NSMutableSet *set = [[self valueForAttribute: anAttribute] mutableCopy];
398+
NSAssert([set isKindOfClass: [NSMutableSet class]], @"expected NSMutableSet");
399+
[set addObject: aValue];
400+
[self setValue: set
401+
forAttribute: anAttribute
402+
type: aType];
403+
[set release];
404+
}
405+
406+
- (void) addObject: (id)aValue
407+
toOrderedAttribute: (NSString*)anAttribute
408+
atIndex: (NSUInteger)anIndex
409+
type: (COType *)aType
410+
{
411+
if (![aType isMultivalued] || ![aType isOrdered])
412+
{
413+
[NSException raise: NSInvalidArgumentException
414+
format: @"expected ordered type"];
415+
}
416+
417+
if ([self typeForAttribute: anAttribute] == nil)
418+
{
419+
[self setValue: [NSMutableArray array]
420+
forAttribute: anAttribute
421+
type: aType];
422+
}
423+
424+
NSMutableArray *array = [[self valueForAttribute: anAttribute] mutableCopy];
425+
NSAssert([array isKindOfClass: [NSMutableArray class]], @"expected NSMutableArray");
426+
[array insertObject: aValue
427+
atIndex: anIndex];
428+
[self setValue: array
429+
forAttribute: anAttribute
430+
type: aType];
431+
[array release];
432+
}
433+
380434
- (void) addObject: (id)aValue
381435
forAttribute: (NSString*)anAttribute
382436
{

Scraps/COItemPath.m

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -131,35 +131,12 @@ - (id) initWithItemUUID: (ETUUID *)aUUID
131131
}
132132

133133
- (void) insertValue: (id)aValue
134-
ofType: (COType *)aType
135134
inStoreItem: (COMutableItem *)aStoreItem
136135
{
137-
if (![aType isPrimitive])
138-
{
139-
[NSException raise: NSInvalidArgumentException
140-
format: @"expected primitive type"];
141-
}
142-
143-
if (nil == [aStoreItem typeForAttribute: attribute])
144-
{
145-
[aStoreItem setType: [COType arrayWithPrimitiveType: aType]
146-
forAttribute: attribute];
147-
}
148-
else
149-
{
150-
if (![[aStoreItem typeForAttribute: attribute] isMultivalued] ||
151-
![[aStoreItem typeForAttribute: attribute] isOrdered] ||
152-
![[[aStoreItem typeForAttribute: attribute] primitiveType] isEqual: aType])
153-
{
154-
[NSException raise: NSInvalidArgumentException
155-
format: @"type mismatch"];
156-
}
157-
}
158-
159-
NSMutableArray *array = [[NSMutableArray alloc] initWithArray: [aStoreItem valueForAttribute: attribute]];
160-
[array insertObject: aValue atIndex: index];
161-
[aStoreItem setValue: array forAttribute: attribute];
162-
[array release];
136+
[aStoreItem addObject: aValue
137+
toOrderedAttribute: attribute
138+
atIndex: index
139+
type: type];
163140
}
164141

165142
- (BOOL) isEqual:(id)object
@@ -179,14 +156,9 @@ @implementation COItemPathToUnorderedContainer
179156
- (void) insertValue: (id)aValue
180157
inStoreItem: (COMutableItem *)aStoreItem
181158
{
182-
assert([[aStoreItem typeForAttribute: attribute] isMultivalued]);
183-
assert(![[aStoreItem typeForAttribute: attribute] isOrdered]);
184-
185-
NSMutableSet *set = [[aStoreItem valueForAttribute: attribute] mutableCopy]; // may be NSMutableSet subclass NSCountedSet
186-
assert([set isKindOfClass: [NSMutableSet class]]);
187-
[set addObject: aValue];
188-
[aStoreItem setValue: set forAttribute: attribute];
189-
[set release];
159+
[aStoreItem addObject: aValue
160+
toUnorderedAttribute: attribute
161+
type: type];
190162
}
191163

192164
@end
@@ -197,7 +169,9 @@ @implementation COItemPathToValue
197169
- (void) insertValue: (id)aValue
198170
inStoreItem: (COMutableItem *)aStoreItem
199171
{
200-
// FIXME:
172+
[aStoreItem setValue: aValue
173+
forAttribute: attribute
174+
type: type];
201175
}
202176

203177
@end

0 commit comments

Comments
 (0)