FSTDocumentReference.mm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/Local/FSTDocumentReference.h"
  17. #include <utility>
  18. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  19. #include "Firestore/core/src/firebase/firestore/util/comparison.h"
  20. using firebase::firestore::model::DocumentKey;
  21. using firebase::firestore::util::WrapCompare;
  22. NS_ASSUME_NONNULL_BEGIN
  23. @implementation FSTDocumentReference {
  24. DocumentKey _key;
  25. }
  26. - (instancetype)initWithKey:(DocumentKey)key ID:(int32_t)ID {
  27. self = [super init];
  28. if (self) {
  29. _key = std::move(key);
  30. _ID = ID;
  31. }
  32. return self;
  33. }
  34. - (BOOL)isEqual:(id)other {
  35. if (other == self) return YES;
  36. if (![[other class] isEqual:[self class]]) return NO;
  37. FSTDocumentReference *reference = (FSTDocumentReference *)other;
  38. return [self.key isEqualToKey:reference.key] && self.ID == reference.ID;
  39. }
  40. - (NSUInteger)hash {
  41. NSUInteger result = [self.key hash];
  42. result = result * 31u + self.ID;
  43. return result;
  44. }
  45. - (NSString *)description {
  46. return [NSString stringWithFormat:@"<FSTDocumentReference: key=%s, ID=%d>",
  47. self.key.ToString().c_str(), self.ID];
  48. }
  49. - (id)copyWithZone:(nullable NSZone *)zone {
  50. // FSTDocumentReference is immutable
  51. return self;
  52. }
  53. - (const firebase::firestore::model::DocumentKey &)key {
  54. return _key;
  55. }
  56. @end
  57. #pragma mark Comparators
  58. /** Sorts document references by key then ID. */
  59. const NSComparator FSTDocumentReferenceComparatorByKey =
  60. ^NSComparisonResult(FSTDocumentReference *left, FSTDocumentReference *right) {
  61. NSComparisonResult result = FSTDocumentKeyComparator(left.key, right.key);
  62. if (result != NSOrderedSame) {
  63. return result;
  64. }
  65. return WrapCompare<int32_t>(left.ID, right.ID);
  66. };
  67. /** Sorts document references by ID then key. */
  68. const NSComparator FSTDocumentReferenceComparatorByID =
  69. ^NSComparisonResult(FSTDocumentReference *left, FSTDocumentReference *right) {
  70. NSComparisonResult result = WrapCompare<int32_t>(left.ID, right.ID);
  71. if (result != NSOrderedSame) {
  72. return result;
  73. }
  74. return FSTDocumentKeyComparator(left.key, right.key);
  75. };
  76. NS_ASSUME_NONNULL_END