FIRMutableData.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "FIRMutableData.h"
  17. #import "FIRMutableData_Private.h"
  18. #import "FSnapshotHolder.h"
  19. #import "FSnapshotUtilities.h"
  20. #import "FChildrenNode.h"
  21. #import "FTransformedEnumerator.h"
  22. #import "FNamedNode.h"
  23. #import "FIndexedNode.h"
  24. @interface FIRMutableData ()
  25. - (id) initWithPrefixPath:(FPath *)path andSnapshotHolder:(FSnapshotHolder *)snapshotHolder;
  26. @property (strong, nonatomic) FSnapshotHolder* data;
  27. @property (strong, nonatomic) FPath* prefixPath;
  28. @end
  29. @implementation FIRMutableData
  30. @synthesize data;
  31. @synthesize prefixPath;
  32. - (id) initWithNode:(id<FNode>)node {
  33. FSnapshotHolder* holder = [[FSnapshotHolder alloc] init];
  34. FPath* path = [FPath empty];
  35. [holder updateSnapshot:path withNewSnapshot:node];
  36. return [self initWithPrefixPath:path andSnapshotHolder:holder];
  37. }
  38. - (id) initWithPrefixPath:(FPath *)path andSnapshotHolder:(FSnapshotHolder *)snapshotHolder {
  39. self = [super init];
  40. if (self) {
  41. self.prefixPath = path;
  42. self.data = snapshotHolder;
  43. }
  44. return self;
  45. }
  46. - (FIRMutableData *)childDataByAppendingPath:(NSString *)path {
  47. FPath* wholePath = [self.prefixPath childFromString:path];
  48. return [[FIRMutableData alloc] initWithPrefixPath:wholePath andSnapshotHolder:self.data];
  49. }
  50. - (FIRMutableData *) parent {
  51. if ([self.prefixPath isEmpty]) {
  52. return nil;
  53. } else {
  54. FPath* path = [self.prefixPath parent];
  55. return [[FIRMutableData alloc] initWithPrefixPath:path andSnapshotHolder:self.data];
  56. }
  57. }
  58. - (void) setValue:(id)aValue {
  59. id<FNode> node = [FSnapshotUtilities nodeFrom:aValue withValidationFrom:@"setValue:"];
  60. [self.data updateSnapshot:self.prefixPath withNewSnapshot:node];
  61. }
  62. - (void) setPriority:(id)aPriority {
  63. id<FNode> node = [self.data getNode:self.prefixPath];
  64. id<FNode> pri = [FSnapshotUtilities nodeFrom:aPriority];
  65. node = [node updatePriority:pri];
  66. [self.data updateSnapshot:self.prefixPath withNewSnapshot:node];
  67. }
  68. - (id) value {
  69. return [[self.data getNode:self.prefixPath] val];
  70. }
  71. - (id) priority {
  72. return [[[self.data getNode:self.prefixPath] getPriority] val];
  73. }
  74. - (BOOL) hasChildren {
  75. id<FNode> node = [self.data getNode:self.prefixPath];
  76. return ![node isLeafNode] && ![(FChildrenNode*)node isEmpty];
  77. }
  78. - (BOOL) hasChildAtPath:(NSString *)path {
  79. id<FNode> node = [self.data getNode:self.prefixPath];
  80. FPath* childPath = [[FPath alloc] initWith:path];
  81. return ![[node getChild:childPath] isEmpty];
  82. }
  83. - (NSUInteger) childrenCount {
  84. return [[self.data getNode:self.prefixPath] numChildren];
  85. }
  86. - (NSString *) key {
  87. return [self.prefixPath getBack];
  88. }
  89. - (id<FNode>) nodeValue {
  90. return [self.data getNode:self.prefixPath];
  91. }
  92. - (NSEnumerator<FIRMutableData *> *) children {
  93. FIndexedNode *indexedNode = [FIndexedNode indexedNodeWithNode:self.nodeValue];
  94. return [[FTransformedEnumerator alloc] initWithEnumerator:[indexedNode childEnumerator] andTransform:^id(FNamedNode *node) {
  95. FPath* childPath = [self.prefixPath childFromString:node.name];
  96. FIRMutableData * childData = [[FIRMutableData alloc] initWithPrefixPath:childPath andSnapshotHolder:self.data];
  97. return childData;
  98. }];
  99. }
  100. - (BOOL) isEqualToData:(FIRMutableData *)other {
  101. return self.data == other.data && [[self.prefixPath description] isEqualToString:[other.prefixPath description]];
  102. }
  103. - (NSString *) description {
  104. if (self.key == nil) {
  105. return [NSString stringWithFormat:@"FIRMutableData (top-most transaction) %@ %@", self.key, self.value];
  106. } else {
  107. return [NSString stringWithFormat:@"FIRMutableData (%@) %@", self.key, self.value];
  108. }
  109. }
  110. @end