FSTDocumentReference.m 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #import "Firestore/Source/Model/FSTDocumentKey.h"
  18. #import "Firestore/Source/Util/FSTComparison.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. @implementation FSTDocumentReference
  21. - (instancetype)initWithKey:(FSTDocumentKey *)key ID:(int)ID {
  22. self = [super init];
  23. if (self) {
  24. _key = key;
  25. _ID = ID;
  26. }
  27. return self;
  28. }
  29. - (BOOL)isEqual:(id)other {
  30. if (other == self) return YES;
  31. if (!other || ![[other class] isEqual:[self class]]) return NO;
  32. FSTDocumentReference *reference = (FSTDocumentReference *)other;
  33. return [self.key isEqualToKey:reference.key] && self.ID == reference.ID;
  34. }
  35. - (NSUInteger)hash {
  36. NSUInteger result = [self.key hash];
  37. result = result * 31u + self.ID;
  38. return result;
  39. }
  40. - (NSString *)description {
  41. return [NSString stringWithFormat:@"<FSTDocumentReference: key=%@, ID=%d>", self.key, self.ID];
  42. }
  43. - (id)copyWithZone:(nullable NSZone *)zone {
  44. // FSTDocumentReference is immutable
  45. return self;
  46. }
  47. @end
  48. #pragma mark Comparators
  49. /** Sorts document references by key then ID. */
  50. const NSComparator FSTDocumentReferenceComparatorByKey =
  51. ^NSComparisonResult(FSTDocumentReference *left, FSTDocumentReference *right) {
  52. NSComparisonResult result = FSTDocumentKeyComparator(left.key, right.key);
  53. if (result != NSOrderedSame) {
  54. return result;
  55. }
  56. return FSTCompareInts(left.ID, right.ID);
  57. };
  58. /** Sorts document references by ID then key. */
  59. const NSComparator FSTDocumentReferenceComparatorByID =
  60. ^NSComparisonResult(FSTDocumentReference *left, FSTDocumentReference *right) {
  61. NSComparisonResult result = FSTCompareInts(left.ID, right.ID);
  62. if (result != NSOrderedSame) {
  63. return result;
  64. }
  65. return FSTDocumentKeyComparator(left.key, right.key);
  66. };
  67. NS_ASSUME_NONNULL_END