IOSAD-92 PEPIdentity using hashBasedOnKeys etc. IOSAD-92
authorDirk Zimmermann <dirk@pep-project.org>
Tue, 24 Apr 2018 13:29:19 +0200
branchIOSAD-92
changeset 594a9cae3a6f78d
parent 593 78a14e6e06d0
child 595 09804658bc89
IOSAD-92 PEPIdentity using hashBasedOnKeys etc.
pEpObjCAdapter/PEPIdentity.m
     1.1 --- a/pEpObjCAdapter/PEPIdentity.m	Tue Apr 24 13:29:18 2018 +0200
     1.2 +++ b/pEpObjCAdapter/PEPIdentity.m	Tue Apr 24 13:29:19 2018 +0200
     1.3 @@ -12,6 +12,8 @@
     1.4  #import "PEPMessageUtil.h"
     1.5  #import "PEPSession.h"
     1.6  
     1.7 +#import "NSObject+Extension.h"
     1.8 +
     1.9  @implementation PEPIdentity
    1.10  
    1.11  - (nonnull instancetype)initWithAddress:(NSString * _Nonnull)address
    1.12 @@ -108,19 +110,31 @@
    1.13  
    1.14  // MARK: - Equality
    1.15  
    1.16 -- (BOOL)isEqual:(id)other
    1.17 +/**
    1.18 + The keys that should be used to decide `isEqual` and compute the `hash`.
    1.19 + */
    1.20 +static NSArray *s_keys;
    1.21 +
    1.22 +- (BOOL)isEqualToPEPIdentity:(PEPIdentity * _Nonnull)identity
    1.23  {
    1.24 -    if (other == self) {
    1.25 -        return YES;
    1.26 -    } else {
    1.27 -        PEPIdentity *theOther = (PEPIdentity *) other;
    1.28 -        return [self.address isEqualToString:theOther.address] && self.isOwn == theOther.isOwn;
    1.29 -    }
    1.30 +    return [self isEqualToObject:identity basedOnKeys:s_keys];
    1.31  }
    1.32  
    1.33  - (NSUInteger)hash
    1.34  {
    1.35 -    return self.address.hash;
    1.36 +    return [self hashBasedOnKeys:s_keys];
    1.37 +}
    1.38 +
    1.39 +- (BOOL)isEqual:(id)object
    1.40 +{
    1.41 +    if (object == self) {
    1.42 +        return YES;
    1.43 +    }
    1.44 +    if (!object || ![object isKindOfClass:[self class]]) {
    1.45 +        return NO;
    1.46 +    }
    1.47 +
    1.48 +    return [self isEqualToPEPIdentity:object];
    1.49  }
    1.50  
    1.51  // MARK: - NSKeyValueCoding
    1.52 @@ -258,4 +272,11 @@
    1.53              (long) self.commType, self.language];
    1.54  }
    1.55  
    1.56 +// MARK: - Static Initialization
    1.57 +
    1.58 ++ (void)initialize
    1.59 +{
    1.60 +    s_keys = @[@"address"];
    1.61 +}
    1.62 +
    1.63  @end