FSTWatchChange.mm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  20. using firebase::firestore::model::DocumentKey;
  21. using firebase::firestore::model::TargetId;
  22. using firebase::firestore::remote::ExistenceFilter;
  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 == 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:(ExistenceFilter)filter
  68. targetID:(TargetId)targetID NS_DESIGNATED_INITIALIZER;
  69. @end
  70. @implementation FSTExistenceFilterWatchChange {
  71. ExistenceFilter _filter;
  72. }
  73. + (instancetype)changeWithFilter:(ExistenceFilter)filter targetID:(TargetId)targetID {
  74. return [[FSTExistenceFilterWatchChange alloc] initWithFilter:filter targetID:targetID];
  75. }
  76. - (instancetype)initWithFilter:(ExistenceFilter)filter targetID:(TargetId)targetID {
  77. self = [super init];
  78. if (self) {
  79. _filter = filter;
  80. _targetID = targetID;
  81. }
  82. return self;
  83. }
  84. - (const ExistenceFilter &)filter {
  85. return _filter;
  86. }
  87. - (BOOL)isEqual:(id)other {
  88. if (other == self) {
  89. return YES;
  90. }
  91. if (![other isMemberOfClass:[FSTExistenceFilterWatchChange class]]) {
  92. return NO;
  93. }
  94. FSTExistenceFilterWatchChange *otherChange = (FSTExistenceFilterWatchChange *)other;
  95. return _filter == otherChange->_filter && _targetID == otherChange->_targetID;
  96. }
  97. - (NSUInteger)hash {
  98. return _filter.count();
  99. }
  100. @end
  101. @implementation FSTWatchTargetChange
  102. - (instancetype)initWithState:(FSTWatchTargetChangeState)state
  103. targetIDs:(NSArray<NSNumber *> *)targetIDs
  104. resumeToken:(NSData *)resumeToken
  105. cause:(nullable NSError *)cause {
  106. self = [super init];
  107. if (self) {
  108. _state = state;
  109. _targetIDs = targetIDs;
  110. _resumeToken = [resumeToken copy];
  111. _cause = cause;
  112. }
  113. return self;
  114. }
  115. - (BOOL)isEqual:(id)other {
  116. if (other == self) {
  117. return YES;
  118. }
  119. if (![other isMemberOfClass:[FSTWatchTargetChange class]]) {
  120. return NO;
  121. }
  122. FSTWatchTargetChange *otherChange = (FSTWatchTargetChange *)other;
  123. return _state == otherChange->_state && [_targetIDs isEqual:otherChange->_targetIDs] &&
  124. [_resumeToken isEqual:otherChange->_resumeToken] &&
  125. (_cause == otherChange->_cause || [_cause isEqual:otherChange->_cause]);
  126. }
  127. - (NSUInteger)hash {
  128. NSUInteger hash = (NSUInteger)self.state;
  129. hash = hash * 31 + self.targetIDs.hash;
  130. hash = hash * 31 + self.resumeToken.hash;
  131. hash = hash * 31 + self.cause.hash;
  132. return hash;
  133. }
  134. @end
  135. NS_ASSUME_NONNULL_END