This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
COItemPath.m
204 lines (161 loc) · 4.55 KB
/
COItemPath.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#import "COItemPath.h"
#import "COItem.h"
#import <EtoileFoundation/Macros.h>
#import "COType.h"
@interface COItemPath (InternalPrivate)
- (id) initWithItemUUID: (ETUUID *)aUUID
valueName: (NSString *)aName
type: (COType)aType;
@end
@interface COItemPathToUnorderedContainer : COItemPath
@end
@interface COItemPathToOrderedContainer : COItemPath
{
NSUInteger index;
}
- (id) initWithItemUUID: (ETUUID *)aUUID
arrayName: (NSString *)collection
insertionIndex: (NSUInteger)anIndex
type: (COType)aType;
@end
@interface COItemPathToValue : COItemPath
@end
@implementation COItemPath
- (id) initWithItemUUID: (ETUUID *)aUUID
attributeName: (NSString *)aName
type: (COType)aType
{
SUPERINIT;
ASSIGN(uuid, aUUID);
ASSIGN(attribute, aName);
type = aType;
return self;
}
+ (COItemPath *) pathWithItemUUID: (ETUUID *)aUUID
unorderedCollectionName: (NSString *)collection
type: (COType)aType
{
return [[[COItemPathToUnorderedContainer alloc] initWithItemUUID: aUUID
attributeName: collection
type: aType] autorelease];
}
+ (COItemPath *) pathWithItemUUID: (ETUUID *)aUUID
arrayName: (NSString *)collection
insertionIndex: (NSUInteger)index
type: (COType)aType
{
return [[[COItemPathToOrderedContainer alloc] initWithItemUUID: aUUID
arrayName: collection
insertionIndex: index
type: aType] autorelease];
}
+ (COItemPath *) pathWithItemUUID: (ETUUID *)aUUID
valueName: (NSString *)aName
type: (COType)aType
{
return [[[COItemPathToUnorderedContainer alloc] initWithItemUUID: aUUID
attributeName: aName
type: aType] autorelease];
}
- (void) dealloc
{
[uuid release];
[attribute release];
[super dealloc];
}
- (id)copyWithZone:(NSZone *)zone
{
return [self retain];
}
- (void) insertValue: (id)aValue
inStoreItem: (COMutableItem *)aStoreItem
{
[NSException raise: NSInternalInconsistencyException
format: @"%@ unimplemented", NSStringFromSelector(_cmd)];
}
- (void) removeValue: (id)aValue inStoreItem: (COMutableItem *)aStoreItem
{
[NSException raise: NSInternalInconsistencyException
format: @"%@ unimplemented", NSStringFromSelector(_cmd)];
}
- (BOOL) isEqual:(id)object
{
if (![object isKindOfClass: [self class]])
return NO;
COItemPath *other = (COItemPath *)object;
return ([uuid isEqual: other->uuid] &&
[attribute isEqual: other->attribute]);
}
- (ETUUID *)UUID
{
return uuid;
}
@end
@implementation COItemPathToOrderedContainer
- (id) initWithItemUUID: (ETUUID *)aUUID
arrayName: (NSString *)collection
insertionIndex: (NSUInteger)anIndex
type: (COType)aType
{
if ((self = [super initWithItemUUID: aUUID
attributeName: collection
type: aType]) == nil)
{
return nil;
}
index = anIndex;
return self;
}
- (void) insertValue: (id)aValue
inStoreItem: (COMutableItem *)aStoreItem
{
[aStoreItem addObject: aValue
toOrderedAttribute: attribute
atIndex: index
type: type];
}
- (void) removeValue: (id)aValue inStoreItem: (COMutableItem *)aStoreItem
{
NSMutableArray *array = [aStoreItem valueForAttribute: attribute];
NSAssert([[array objectAtIndex: index] isEqual: aValue], @"value being removed is not what was expected.");
[array removeObjectAtIndex: index];
[aStoreItem setValue: array forAttribute: attribute];
}
- (BOOL) isEqual:(id)object
{
if (![object isKindOfClass: [self class]])
return NO;
COItemPathToOrderedContainer *other = (COItemPathToOrderedContainer *)object;
return (index == other->index && [super isEqual: object]);
}
@end
@implementation COItemPathToUnorderedContainer
- (void) insertValue: (id)aValue
inStoreItem: (COMutableItem *)aStoreItem
{
[aStoreItem addObject: aValue
toUnorderedAttribute: attribute
type: type];
}
- (void) removeValue: (id)aValue inStoreItem: (COMutableItem *)aStoreItem
{
NSMutableSet *set = [[aStoreItem valueForAttribute: attribute] mutableCopy];
NSAssert([set containsObject: aValue], @"value to be remove is not present in set");
[set removeObject: aValue];
[aStoreItem setValue: set forAttribute: attribute];
}
@end
@implementation COItemPathToValue
- (void) insertValue: (id)aValue
inStoreItem: (COMutableItem *)aStoreItem
{
[aStoreItem setValue: aValue
forAttribute: attribute
type: type];
}
- (void) removeValue: (id)aValue inStoreItem: (COMutableItem *)aStoreItem
{
NSAssert([[aStoreItem valueForAttribute: attribute] isEqual: aValue], @"value to be removed is not what was expected");
[aStoreItem removeValueForAttribute: attribute];
}
@end