]> err.no Git - sope/blob - sope-core/EOControl/EOKeyGlobalID.m
various bugfixes for the computeXXX: methods, proper implementations for Foundations...
[sope] / sope-core / EOControl / EOKeyGlobalID.m
1 /*
2   Copyright (C) 2000-2006 SKYRIX Software AG
3   Copyright (C) 2006      Helge Hess
4
5   This file is part of SOPE.
6
7   SOPE is free software; you can redistribute it and/or modify it under
8   the terms of the GNU Lesser General Public License as published by the
9   Free Software Foundation; either version 2, or (at your option) any
10   later version.
11
12   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
13   WARRANTY; without even the implied warranty of MERCHANTABILITY or
14   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15   License for more details.
16
17   You should have received a copy of the GNU Lesser General Public
18   License along with SOPE; see the file COPYING.  If not, write to the
19   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20   02111-1307, USA.
21 */
22
23 #include "EOKeyGlobalID.h"
24 #include "common.h"
25
26 @implementation EOKeyGlobalID
27
28 + (id)globalIDWithEntityName:(NSString *)_name
29   keys:(id *)_keyValues
30   keyCount:(unsigned int)_count
31   zone:(NSZone *)_zone
32 {
33   EOKeyGlobalID *kid;
34
35   NSAssert1(_count > 0, 
36             @"missing key-values (count is 0, entity is %@", _name);
37   
38   if ((kid = (id)NSAllocateObject(self, sizeof(id) * _count, _zone))) {
39     unsigned int i;
40     kid->entityName = [_name copyWithZone:_zone];
41     kid->count      = _count;
42     
43     for (i = 0; i < _count; i++) {
44 #if DEBUG
45       if (_keyValues[i] == nil) {
46         NSLog(@"WARN(%s): got 'nil' as a EOKeyGlobalID value (entity=%@)!",
47               __PRETTY_FUNCTION__, _name);
48       }
49 #endif
50       kid->values[i] = [_keyValues[i] retain];
51     }
52
53     return [kid autorelease];
54   }
55   
56   return nil;
57 }
58
59 - (void)dealloc {
60   unsigned int i;
61   for (i = 0; i < self->count; i++) {
62     [self->values[i] release];
63     self->values[i] = nil;
64   }
65   [self->entityName release];
66   [super dealloc];
67 }
68
69 /* accessors */
70
71 - (NSString *)entityName {
72   return self->entityName;
73 }
74
75 - (unsigned int)keyCount {
76   return self->count;
77 }
78 - (id *)keyValues {
79   return &(self->values[0]);
80 }
81
82 - (NSArray *)keyValuesArray {
83   return [NSArray arrayWithObjects:&(self->values[0]) count:self->count];
84 }
85
86 /* Equality */
87
88 - (unsigned)hash {
89   return [self->entityName hash] - [self->values[0] hash];
90 }
91
92 - (BOOL)isEqual:(id)_other {
93   EOKeyGlobalID *otherKey;
94   unsigned int i;
95
96   if (_other == nil)  return NO;
97   if (_other == self) return YES;
98   otherKey = _other;
99   if (otherKey->isa   != self->isa)   return NO;
100   if (otherKey->count != self->count) return NO;
101   if (![otherKey->entityName isEqualToString:self->entityName]) return NO;
102   
103   for (i = 0; i < self->count; i++) {
104     if (self->values[i] != otherKey->values[i]) {
105       if (![self->values[i] isEqual:otherKey->values[i]])
106         return NO;
107     }
108   }
109   
110   return YES;
111 }
112
113 /* NSCopying */
114
115 - (id)copyWithZone:(NSZone *)_zone {
116   return [self retain];
117 }
118
119 /* NSCoding */
120
121 - (void)encodeWithCoder:(NSCoder *)_coder {
122   [self doesNotRecognizeSelector:_cmd];
123 }
124 - (id)initWithCoder:(NSCoder *)_coder {
125   [self doesNotRecognizeSelector:_cmd];
126   return nil;
127 #if 0
128   NSString     *entityName;
129   NSZone       *z;
130   unsigned int count;
131   
132   z = [self zone];
133   [self release];
134
135   entityName = [_coder decodeObject];
136   
137   self = [EOKeyGlobalID globalIDWithEntityName:entityName
138                         keys:NULL
139                         keyCount:0
140                         zone:z];
141   return [self retain];
142 #endif
143 }
144
145 /* description */
146
147 - (NSString *)description {
148   NSMutableString *s;
149   NSString *d;
150   unsigned int i;
151   
152   s = [[NSMutableString alloc] init];
153   [s appendFormat:@"<0x%p[%@]: %@",
154        self, NSStringFromClass([self class]),
155        [self entityName]];
156
157   if (self->count == 0) {
158     [s appendString:@" no-key-values"];
159   }
160   else {
161     for (i = 0; i < self->count; i++) {
162       if (i == 0) [s appendString:@" "];
163       else        [s appendString:@"/"];
164       if (self->values[i] == nil)
165         [s appendString:@"<nil>"];
166       else if (self->values[i] == nil)
167         [s appendString:@"<NSNull>"];
168       else
169         [s appendString:[self->values[i] stringValue]];
170     }
171   }
172   
173   [s appendString:@">"];
174
175   d = [s copy];
176   [s release];
177   return [d autorelease];
178 }
179
180 @end /* EOKeyGlobalID */