FSTWatchChange.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Source/Remote/FSTWatchChange.h"
  17. #import "Firestore/Source/Model/FSTDocument.h"
  18. #import "Firestore/Source/Model/FSTDocumentKey.h"
  19. #import "Firestore/Source/Remote/FSTExistenceFilter.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. @implementation FSTWatchChange
  22. @end
  23. @implementation FSTDocumentWatchChange
  24. - (instancetype)initWithUpdatedTargetIDs:(NSArray<NSNumber *> *)updatedTargetIDs
  25. removedTargetIDs:(NSArray<NSNumber *> *)removedTargetIDs
  26. documentKey:(FSTDocumentKey *)documentKey
  27. document:(nullable FSTMaybeDocument *)document {
  28. self = [super init];
  29. if (self) {
  30. _updatedTargetIDs = updatedTargetIDs;
  31. _removedTargetIDs = removedTargetIDs;
  32. _documentKey = documentKey;
  33. _document = document;
  34. }
  35. return self;
  36. }
  37. - (BOOL)isEqual:(id)other {
  38. if (other == self) {
  39. return YES;
  40. }
  41. if (![other isMemberOfClass:[FSTDocumentWatchChange class]]) {
  42. return NO;
  43. }
  44. FSTDocumentWatchChange *otherChange = (FSTDocumentWatchChange *)other;
  45. return [_updatedTargetIDs isEqual:otherChange.updatedTargetIDs] &&
  46. [_removedTargetIDs isEqual:otherChange.removedTargetIDs] &&
  47. [_documentKey isEqual:otherChange.documentKey] &&
  48. (_document == otherChange.document || [_document isEqual:otherChange.document]);
  49. }
  50. - (NSUInteger)hash {
  51. NSUInteger hash = self.updatedTargetIDs.hash;
  52. hash = hash * 31 + self.removedTargetIDs.hash;
  53. hash = hash * 31 + self.documentKey.hash;
  54. hash = hash * 31 + self.document.hash;
  55. return hash;
  56. }
  57. @end
  58. @interface FSTExistenceFilterWatchChange ()
  59. - (instancetype)initWithFilter:(FSTExistenceFilter *)filter
  60. targetID:(FSTTargetID)targetID NS_DESIGNATED_INITIALIZER;
  61. @end
  62. @implementation FSTExistenceFilterWatchChange
  63. + (instancetype)changeWithFilter:(FSTExistenceFilter *)filter targetID:(FSTTargetID)targetID {
  64. return [[FSTExistenceFilterWatchChange alloc] initWithFilter:filter targetID:targetID];
  65. }
  66. - (instancetype)initWithFilter:(FSTExistenceFilter *)filter targetID:(FSTTargetID)targetID {
  67. self = [super init];
  68. if (self) {
  69. _filter = filter;
  70. _targetID = targetID;
  71. }
  72. return self;
  73. }
  74. - (BOOL)isEqual:(id)other {
  75. if (other == self) {
  76. return YES;
  77. }
  78. if (![other isMemberOfClass:[FSTExistenceFilterWatchChange class]]) {
  79. return NO;
  80. }
  81. FSTExistenceFilterWatchChange *otherChange = (FSTExistenceFilterWatchChange *)other;
  82. return [_filter isEqual:otherChange->_filter] && _targetID == otherChange->_targetID;
  83. }
  84. - (NSUInteger)hash {
  85. return self.filter.hash;
  86. }
  87. @end
  88. @implementation FSTWatchTargetChange
  89. - (instancetype)initWithState:(FSTWatchTargetChangeState)state
  90. targetIDs:(NSArray<NSNumber *> *)targetIDs
  91. resumeToken:(NSData *)resumeToken
  92. cause:(nullable NSError *)cause {
  93. self = [super init];
  94. if (self) {
  95. _state = state;
  96. _targetIDs = targetIDs;
  97. _resumeToken = resumeToken;
  98. _cause = cause;
  99. }
  100. return self;
  101. }
  102. - (BOOL)isEqual:(id)other {
  103. if (other == self) {
  104. return YES;
  105. }
  106. if (![other isMemberOfClass:[FSTWatchTargetChange class]]) {
  107. return NO;
  108. }
  109. FSTWatchTargetChange *otherChange = (FSTWatchTargetChange *)other;
  110. return _state == otherChange->_state && [_targetIDs isEqual:otherChange->_targetIDs] &&
  111. [_resumeToken isEqual:otherChange->_resumeToken] &&
  112. (_cause == otherChange->_cause || [_cause isEqual:otherChange->_cause]);
  113. }
  114. - (NSUInteger)hash {
  115. NSUInteger hash = (NSUInteger)self.state;
  116. hash = hash * 31 + self.targetIDs.hash;
  117. hash = hash * 31 + self.resumeToken.hash;
  118. hash = hash * 31 + self.cause.hash;
  119. return hash;
  120. }
  121. @end
  122. NS_ASSUME_NONNULL_END