FIRGeoPoint.mm 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "Firestore/core/include/firebase/firestore/geo_point.h"
  18. #include "Firestore/core/src/firebase/firestore/api/input_validation.h"
  19. #include "Firestore/core/src/firebase/firestore/util/comparison.h"
  20. using firebase::firestore::api::ThrowInvalidArgument;
  21. using firebase::firestore::util::DoubleBitwiseEquals;
  22. using firebase::firestore::util::DoubleBitwiseHash;
  23. using firebase::firestore::util::WrapCompare;
  24. NS_ASSUME_NONNULL_BEGIN
  25. @implementation FIRGeoPoint
  26. - (instancetype)initWithLatitude:(double)latitude longitude:(double)longitude {
  27. if (self = [super init]) {
  28. if (latitude < -90 || latitude > 90 || !isfinite(latitude)) {
  29. ThrowInvalidArgument("GeoPoint requires a latitude value in the range of [-90, 90], "
  30. "but was %s",
  31. latitude);
  32. }
  33. if (longitude < -180 || longitude > 180 || !isfinite(longitude)) {
  34. ThrowInvalidArgument("GeoPoint requires a longitude value in the range of [-180, 180], "
  35. "but was %s",
  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. @implementation FIRGeoPoint (Internal)
  75. - (firestore::GeoPoint)toGeoPoint {
  76. return firestore::GeoPoint(self.latitude, self.longitude);
  77. }
  78. @end
  79. NS_ASSUME_NONNULL_END