FSTWatchChange.mm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #include <utility>
  18. #import "Firestore/Source/Model/FSTDocument.h"
  19. #import "Firestore/Source/Remote/FSTExistenceFilter.h"
  20. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  21. using firebase::firestore::model::DocumentKey;
  22. NS_ASSUME_NONNULL_BEGIN
  23. @implementation FSTWatchChange
  24. @end
  25. @implementation FSTDocumentWatchChange {
  26. DocumentKey _documentKey;
  27. }
  28. - (instancetype)initWithUpdatedTargetIDs:(NSArray<NSNumber *> *)updatedTargetIDs
  29. removedTargetIDs:(NSArray<NSNumber *> *)removedTargetIDs
  30. documentKey:(DocumentKey)documentKey
  31. document:(nullable FSTMaybeDocument *)document {
  32. self = [super init];
  33. if (self) {
  34. _updatedTargetIDs = updatedTargetIDs;
  35. _removedTargetIDs = removedTargetIDs;
  36. _documentKey = std::move(documentKey);
  37. _document = document;
  38. }
  39. return self;
  40. }
  41. - (const firebase::firestore::model::DocumentKey &)documentKey {
  42. return _documentKey;
  43. }
  44. - (BOOL)isEqual:(id)other {
  45. if (other == self) {
  46. return YES;
  47. }
  48. if (![other isMemberOfClass:[FSTDocumentWatchChange class]]) {
  49. return NO;
  50. }
  51. FSTDocumentWatchChange *otherChange = (FSTDocumentWatchChange *)other;
  52. return [_updatedTargetIDs isEqual:otherChange.updatedTargetIDs] &&
  53. [_removedTargetIDs isEqual:otherChange.removedTargetIDs] &&
  54. [_documentKey isEqual:otherChange.documentKey] &&
  55. (_document == otherChange.document || [_document isEqual:otherChange.document]);
  56. }
  57. - (NSUInteger)hash {
  58. NSUInteger hash = self.updatedTargetIDs.hash;
  59. hash = hash * 31 + self.removedTargetIDs.hash;
  60. hash = hash * 31 + self.documentKey.Hash();
  61. hash = hash * 31 + self.document.hash;
  62. return hash;
  63. }
  64. @end
  65. @interface FSTExistenceFilterWatchChange ()
  66. - (instancetype)initWithFilter:(FSTExistenceFilter *)filter
  67. targetID:(FSTTargetID)targetID NS_DESIGNATED_INITIALIZER;
  68. @end
  69. @implementation FSTExistenceFilterWatchChange
  70. + (instancetype)changeWithFilter:(FSTExistenceFilter *)filter targetID:(FSTTargetID)targetID {
  71. return [[FSTExistenceFilterWatchChange alloc] initWithFilter:filter targetID:targetID];
  72. }
  73. - (instancetype)initWithFilter:(FSTExistenceFilter *)filter targetID:(FSTTargetID)targetID {
  74. self = [super init];
  75. if (self) {
  76. _filter = filter;
  77. _targetID = targetID;
  78. }
  79. return self;
  80. }
  81. - (BOOL)isEqual:(id)other {
  82. if (other == self) {
  83. return YES;
  84. }
  85. if (![other isMemberOfClass:[FSTExistenceFilterWatchChange class]]) {
  86. return NO;
  87. }
  88. FSTExistenceFilterWatchChange *otherChange = (FSTExistenceFilterWatchChange *)other;
  89. return [_filter isEqual:otherChange->_filter] && _targetID == otherChange->_targetID;
  90. }
  91. - (NSUInteger)hash {
  92. return self.filter.hash;
  93. }
  94. @end
  95. @implementation FSTWatchTargetChange
  96. - (instancetype)initWithState:(FSTWatchTargetChangeState)state
  97. targetIDs:(NSArray<NSNumber *> *)targetIDs
  98. resumeToken:(NSData *)resumeToken
  99. cause:(nullable NSError *)cause {
  100. self = [super init];
  101. if (self) {
  102. _state = state;
  103. _targetIDs = targetIDs;
  104. _resumeToken = resumeToken;
  105. _cause = cause;
  106. }
  107. return self;
  108. }
  109. - (BOOL)isEqual:(id)other {
  110. if (other == self) {
  111. return YES;
  112. }
  113. if (![other isMemberOfClass:[FSTWatchTargetChange class]]) {
  114. return NO;
  115. }
  116. FSTWatchTargetChange *otherChange = (FSTWatchTargetChange *)other;
  117. return _state == otherChange->_state && [_targetIDs isEqual:otherChange->_targetIDs] &&
  118. [_resumeToken isEqual:otherChange->_resumeToken] &&
  119. (_cause == otherChange->_cause || [_cause isEqual:otherChange->_cause]);
  120. }
  121. - (NSUInteger)hash {
  122. NSUInteger hash = (NSUInteger)self.state;
  123. hash = hash * 31 + self.targetIDs.hash;
  124. hash = hash * 31 + self.resumeToken.hash;
  125. hash = hash * 31 + self.cause.hash;
  126. return hash;
  127. }
  128. @end
  129. NS_ASSUME_NONNULL_END