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