FIRGeoPoint.mm 2.7 KB

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