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