FWriteRecord.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "FWriteRecord.h"
  17. #import "FPath.h"
  18. #import "FNode.h"
  19. #import "FCompoundWrite.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 overwrite:(id<FNode>)overwrite writeId:(NSInteger)writeId visible:(BOOL)isVisible {
  29. self = [super init];
  30. if (self) {
  31. self.path = path;
  32. if (overwrite == nil) {
  33. [NSException raise:NSInvalidArgumentException format:@"Can't pass nil as overwrite parameter to an overwrite write record"];
  34. }
  35. self.overwrite = overwrite;
  36. self.merge = nil;
  37. self.writeId = writeId;
  38. self.visible = isVisible;
  39. }
  40. return self;
  41. }
  42. - (id)initWithPath:(FPath *)path merge:(FCompoundWrite *)merge writeId:(NSInteger)writeId {
  43. self = [super init];
  44. if (self) {
  45. self.path = path;
  46. if (merge == nil) {
  47. [NSException raise:NSInvalidArgumentException format:@"Can't pass nil as merge parameter to an merge write record"];
  48. }
  49. self.overwrite = nil;
  50. self.merge = merge;
  51. self.writeId = writeId;
  52. self.visible = YES;
  53. }
  54. return self;
  55. }
  56. - (id<FNode>)overwrite {
  57. if (self->_overwrite == nil) {
  58. [NSException raise:NSInvalidArgumentException format:@"Can't get overwrite for merge write record!"];
  59. }
  60. return self->_overwrite;
  61. }
  62. - (FCompoundWrite *)compoundWrite {
  63. if (self->_merge == nil) {
  64. [NSException raise:NSInvalidArgumentException format:@"Can't get merge for overwrite write record!"];
  65. }
  66. return self->_merge;
  67. }
  68. - (BOOL)isMerge {
  69. return self->_merge != nil;
  70. }
  71. - (BOOL)isOverwrite {
  72. return self->_overwrite != nil;
  73. }
  74. - (NSString *)description {
  75. if (self.isOverwrite) {
  76. return [NSString stringWithFormat:@"FWriteRecord { writeId = %lu, path = %@, overwrite = %@, visible = %d }",
  77. (unsigned long)self.writeId, self.path, self.overwrite, self.visible];
  78. } else {
  79. return [NSString stringWithFormat:@"FWriteRecord { writeId = %lu, path = %@, merge = %@ }",
  80. (unsigned long)self.writeId, self.path, self.merge];
  81. }
  82. }
  83. - (BOOL)isEqual:(id)object {
  84. if (![object isKindOfClass:[self class]]) {
  85. return NO;
  86. }
  87. FWriteRecord *other = (FWriteRecord *)object;
  88. if (self->_writeId != other->_writeId) return NO;
  89. if (self->_path != other->_path && ![self->_path isEqual:other->_path]) return NO;
  90. if (self->_overwrite != other->_overwrite && ![self->_overwrite isEqual:other->_overwrite]) return NO;
  91. if (self->_merge != other->_merge && ![self->_merge isEqual:other->_merge]) return NO;
  92. if (self->_visible != other->_visible) return NO;
  93. return YES;
  94. }
  95. - (NSUInteger)hash {
  96. NSUInteger hash = self->_writeId * 17;
  97. hash = hash * 31 + self->_path.hash;
  98. hash = hash * 31 + self->_overwrite.hash;
  99. hash = hash * 31 + self->_merge.hash;
  100. hash = hash * 31 + ((self->_visible) ? 1 : 0);
  101. return hash;
  102. }
  103. @end