FAckUserWrite.m 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 "FAckUserWrite.h"
  17. #import "FPath.h"
  18. #import "FOperationSource.h"
  19. #import "FImmutableTree.h"
  20. @implementation FAckUserWrite
  21. - (id) initWithPath:(FPath *)operationPath affectedTree:(FImmutableTree *)tree revert:(BOOL)shouldRevert {
  22. self = [super init];
  23. if (self) {
  24. self->_source = [FOperationSource userInstance];
  25. self->_type = FOperationTypeAckUserWrite;
  26. self->_path = operationPath;
  27. self->_affectedTree = tree;
  28. self->_revert = shouldRevert;
  29. }
  30. return self;
  31. }
  32. - (FAckUserWrite *) operationForChild:(NSString *)childKey {
  33. if (![self.path isEmpty]) {
  34. NSAssert([self.path.getFront isEqualToString:childKey], @"operationForChild called for unrelated child.");
  35. return [[FAckUserWrite alloc] initWithPath:[self.path popFront] affectedTree:self.affectedTree revert:self.revert];
  36. } else if (self.affectedTree.value != nil) {
  37. NSAssert(self.affectedTree.children.isEmpty, @"affectedTree should not have overlapping affected paths.");
  38. // All child locations are affected as well; just return same operation.
  39. return self;
  40. } else {
  41. FImmutableTree *childTree = [self.affectedTree subtreeAtPath:[[FPath alloc] initWith:childKey]];
  42. return [[FAckUserWrite alloc] initWithPath:[FPath empty] affectedTree:childTree revert:self.revert];
  43. }
  44. }
  45. - (NSString *) description {
  46. return [NSString stringWithFormat:@"FAckUserWrite { path=%@, revert=%d, affectedTree=%@ }", self.path, self.revert, self.affectedTree];
  47. }
  48. @end