FIRGeoPoint.m 2.5 KB

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