FSTSnapshotVersion.m 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/Core/FSTSnapshotVersion.h"
  17. #import "Firestore/Source/Core/FSTTimestamp.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. @implementation FSTSnapshotVersion
  20. + (instancetype)noVersion {
  21. static FSTSnapshotVersion *min;
  22. static dispatch_once_t onceToken;
  23. dispatch_once(&onceToken, ^{
  24. FSTTimestamp *timestamp = [[FSTTimestamp alloc] initWithSeconds:0 nanos:0];
  25. min = [FSTSnapshotVersion versionWithTimestamp:timestamp];
  26. });
  27. return min;
  28. }
  29. + (instancetype)versionWithTimestamp:(FSTTimestamp *)timestamp {
  30. return [[FSTSnapshotVersion alloc] initWithTimestamp:timestamp];
  31. }
  32. - (instancetype)initWithTimestamp:(FSTTimestamp *)timestamp {
  33. self = [super init];
  34. if (self) {
  35. _timestamp = timestamp;
  36. }
  37. return self;
  38. }
  39. #pragma mark - NSObject methods
  40. - (BOOL)isEqual:(id)object {
  41. if (self == object) {
  42. return YES;
  43. }
  44. if (![object isKindOfClass:[FSTSnapshotVersion class]]) {
  45. return NO;
  46. }
  47. return [self.timestamp isEqual:((FSTSnapshotVersion *)object).timestamp];
  48. }
  49. - (NSUInteger)hash {
  50. return self.timestamp.hash;
  51. }
  52. - (NSString *)description {
  53. return [NSString stringWithFormat:@"<FSTSnapshotVersion: %@>", self.timestamp];
  54. }
  55. - (id)copyWithZone:(NSZone *_Nullable)zone {
  56. // Implements NSCopying without actually copying because timestamps are immutable.
  57. return self;
  58. }
  59. #pragma mark - Public methods
  60. - (NSComparisonResult)compare:(FSTSnapshotVersion *)other {
  61. return [self.timestamp compare:other.timestamp];
  62. }
  63. @end
  64. NS_ASSUME_NONNULL_END