FIRGeoPoint.mm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/API/FIRGeoPoint+Internal.h"
  17. #import "Firestore/core/src/firebase/firestore/util/comparison.h"
  18. #import "Firestore/Source/Util/FSTUsageValidation.h"
  19. using firebase::firestore::util::DoubleBitwiseEquals;
  20. using firebase::firestore::util::DoubleBitwiseHash;
  21. using firebase::firestore::util::WrapCompare;
  22. NS_ASSUME_NONNULL_BEGIN
  23. @implementation FIRGeoPoint
  24. - (instancetype)initWithLatitude:(double)latitude longitude:(double)longitude {
  25. if (self = [super init]) {
  26. if (latitude < -90 || latitude > 90 || !isfinite(latitude)) {
  27. FSTThrowInvalidArgument(
  28. @"GeoPoint requires a latitude value in the range of [-90, 90], "
  29. "but was %f",
  30. latitude);
  31. }
  32. if (longitude < -180 || longitude > 180 || !isfinite(longitude)) {
  33. FSTThrowInvalidArgument(
  34. @"GeoPoint requires a longitude value in the range of [-180, 180], "
  35. "but was %f",
  36. longitude);
  37. }
  38. _latitude = latitude;
  39. _longitude = longitude;
  40. }
  41. return self;
  42. }
  43. - (NSComparisonResult)compare:(FIRGeoPoint *)other {
  44. NSComparisonResult result = WrapCompare<double>(self.latitude, other.latitude);
  45. if (result != NSOrderedSame) {
  46. return result;
  47. } else {
  48. return WrapCompare<double>(self.longitude, other.longitude);
  49. }
  50. }
  51. #pragma mark - NSObject methods
  52. - (NSString *)description {
  53. return [NSString stringWithFormat:@"<FIRGeoPoint: (%f, %f)>", self.latitude, self.longitude];
  54. }
  55. - (BOOL)isEqual:(id)other {
  56. if (self == other) {
  57. return YES;
  58. }
  59. if (![other isKindOfClass:[FIRGeoPoint class]]) {
  60. return NO;
  61. }
  62. FIRGeoPoint *otherGeoPoint = (FIRGeoPoint *)other;
  63. return DoubleBitwiseEquals(self.latitude, otherGeoPoint.latitude) &&
  64. DoubleBitwiseEquals(self.longitude, otherGeoPoint.longitude);
  65. }
  66. - (NSUInteger)hash {
  67. return 31 * DoubleBitwiseHash(self.latitude) + DoubleBitwiseHash(self.longitude);
  68. }
  69. /** Implements NSCopying without actually copying because geopoints are immutable. */
  70. - (id)copyWithZone:(NSZone *_Nullable)zone {
  71. return self;
  72. }
  73. @end
  74. NS_ASSUME_NONNULL_END