FSTWatchChange.mm 4.5 KB

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