FWriteRecord.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "FirebaseDatabase/Sources/Core/FWriteRecord.h"
  17. #import "FirebaseDatabase/Sources/Core/Utilities/FPath.h"
  18. #import "FirebaseDatabase/Sources/Snapshot/FCompoundWrite.h"
  19. #import "FirebaseDatabase/Sources/Snapshot/FNode.h"
  20. @interface FWriteRecord ()
  21. @property(nonatomic, readwrite) NSInteger writeId;
  22. @property(nonatomic, strong, readwrite) FPath *path;
  23. @property(nonatomic, strong, readwrite) id<FNode> overwrite;
  24. @property(nonatomic, strong, readwrite) FCompoundWrite *merge;
  25. @property(nonatomic, readwrite) BOOL visible;
  26. @end
  27. @implementation FWriteRecord
  28. - (id)initWithPath:(FPath *)path
  29. overwrite:(id<FNode>)overwrite
  30. writeId:(NSInteger)writeId
  31. visible:(BOOL)isVisible {
  32. self = [super init];
  33. if (self) {
  34. self.path = path;
  35. if (overwrite == nil) {
  36. [NSException raise:NSInvalidArgumentException
  37. format:@"Can't pass nil as overwrite parameter to an "
  38. @"overwrite write record"];
  39. }
  40. self.overwrite = overwrite;
  41. self.merge = nil;
  42. self.writeId = writeId;
  43. self.visible = isVisible;
  44. }
  45. return self;
  46. }
  47. - (id)initWithPath:(FPath *)path
  48. merge:(FCompoundWrite *)merge
  49. writeId:(NSInteger)writeId {
  50. self = [super init];
  51. if (self) {
  52. self.path = path;
  53. if (merge == nil) {
  54. [NSException raise:NSInvalidArgumentException
  55. format:@"Can't pass nil as merge parameter to an merge "
  56. @"write record"];
  57. }
  58. self.overwrite = nil;
  59. self.merge = merge;
  60. self.writeId = writeId;
  61. self.visible = YES;
  62. }
  63. return self;
  64. }
  65. - (id<FNode>)overwrite {
  66. if (self->_overwrite == nil) {
  67. [NSException raise:NSInvalidArgumentException
  68. format:@"Can't get overwrite for merge write record!"];
  69. }
  70. return self->_overwrite;
  71. }
  72. - (FCompoundWrite *)compoundWrite {
  73. if (self->_merge == nil) {
  74. [NSException raise:NSInvalidArgumentException
  75. format:@"Can't get merge for overwrite write record!"];
  76. }
  77. return self->_merge;
  78. }
  79. - (BOOL)isMerge {
  80. return self->_merge != nil;
  81. }
  82. - (BOOL)isOverwrite {
  83. return self->_overwrite != nil;
  84. }
  85. - (NSString *)description {
  86. if (self.isOverwrite) {
  87. return
  88. [NSString stringWithFormat:@"FWriteRecord { writeId = %lu, path = "
  89. @"%@, overwrite = %@, visible = %d }",
  90. (unsigned long)self.writeId, self.path,
  91. self.overwrite, self.visible];
  92. } else {
  93. return [NSString
  94. stringWithFormat:
  95. @"FWriteRecord { writeId = %lu, path = %@, merge = %@ }",
  96. (unsigned long)self.writeId, self.path, self.merge];
  97. }
  98. }
  99. - (BOOL)isEqual:(id)object {
  100. if (![object isKindOfClass:[self class]]) {
  101. return NO;
  102. }
  103. FWriteRecord *other = (FWriteRecord *)object;
  104. if (self->_writeId != other->_writeId)
  105. return NO;
  106. if (self->_path != other->_path && ![self->_path isEqual:other->_path])
  107. return NO;
  108. if (self->_overwrite != other->_overwrite &&
  109. ![self->_overwrite isEqual:other->_overwrite])
  110. return NO;
  111. if (self->_merge != other->_merge && ![self->_merge isEqual:other->_merge])
  112. return NO;
  113. if (self->_visible != other->_visible)
  114. return NO;
  115. return YES;
  116. }
  117. - (NSUInteger)hash {
  118. NSUInteger hash = self->_writeId * 17;
  119. hash = hash * 31 + self->_path.hash;
  120. hash = hash * 31 + self->_overwrite.hash;
  121. hash = hash * 31 + self->_merge.hash;
  122. hash = hash * 31 + ((self->_visible) ? 1 : 0);
  123. return hash;
  124. }
  125. @end