FSTDocumentKey.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/Model/FSTDocumentKey.h"
  17. #import "Firestore/Source/Core/FSTFirestoreClient.h"
  18. #import "Firestore/Source/Model/FSTPath.h"
  19. #import "Firestore/Source/Util/FSTAssert.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. @interface FSTDocumentKey ()
  22. /** The path to the document. */
  23. @property(strong, nonatomic, readwrite) FSTResourcePath *path;
  24. @end
  25. @implementation FSTDocumentKey
  26. + (instancetype)keyWithPath:(FSTResourcePath *)path {
  27. return [[FSTDocumentKey alloc] initWithPath:path];
  28. }
  29. + (instancetype)keyWithSegments:(NSArray<NSString *> *)segments {
  30. return [FSTDocumentKey keyWithPath:[FSTResourcePath pathWithSegments:segments]];
  31. }
  32. + (instancetype)keyWithPathString:(NSString *)resourcePath {
  33. NSArray<NSString *> *segments = [resourcePath componentsSeparatedByString:@"/"];
  34. return [FSTDocumentKey keyWithSegments:segments];
  35. }
  36. /** Designated initializer. */
  37. - (instancetype)initWithPath:(FSTResourcePath *)path {
  38. FSTAssert([FSTDocumentKey isDocumentKey:path], @"invalid document key path: %@", path);
  39. if (self = [super init]) {
  40. _path = path;
  41. }
  42. return self;
  43. }
  44. - (BOOL)isEqual:(id)object {
  45. if (self == object) {
  46. return YES;
  47. }
  48. if (![object isKindOfClass:[FSTDocumentKey class]]) {
  49. return NO;
  50. }
  51. return [self isEqualToKey:(FSTDocumentKey *)object];
  52. }
  53. - (NSUInteger)hash {
  54. return self.path.hash;
  55. }
  56. - (NSString *)description {
  57. return [NSString stringWithFormat:@"<FSTDocumentKey: %@>", self.path];
  58. }
  59. /** Implements NSCopying without actually copying because FSTDocumentKeys are immutable. */
  60. - (id)copyWithZone:(NSZone *_Nullable)zone {
  61. return self;
  62. }
  63. - (BOOL)isEqualToKey:(FSTDocumentKey *)other {
  64. return FSTDocumentKeyComparator(self, other) == NSOrderedSame;
  65. }
  66. - (NSComparisonResult)compare:(FSTDocumentKey *)other {
  67. return FSTDocumentKeyComparator(self, other);
  68. }
  69. + (NSComparator)comparator {
  70. return ^NSComparisonResult(id obj1, id obj2) {
  71. return [obj1 compare:obj2];
  72. };
  73. }
  74. + (BOOL)isDocumentKey:(FSTResourcePath *)path {
  75. return path.length % 2 == 0;
  76. }
  77. @end
  78. const NSComparator FSTDocumentKeyComparator =
  79. ^NSComparisonResult(FSTDocumentKey *key1, FSTDocumentKey *key2) {
  80. return [key1.path compare:key2.path];
  81. };
  82. NSString *const kDocumentKeyPath = @"__name__";
  83. NS_ASSUME_NONNULL_END