FIRGeoPoint.mm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/util/comparison.h"
  19. #include "Firestore/core/src/util/exception.h"
  20. using firebase::firestore::util::DoubleBitwiseEquals;
  21. using firebase::firestore::util::DoubleBitwiseHash;
  22. using firebase::firestore::util::ThrowInvalidArgument;
  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. #pragma mark - NSObject methods
  44. - (NSString *)description {
  45. return [NSString stringWithFormat:@"<FIRGeoPoint: (%f, %f)>", self.latitude, self.longitude];
  46. }
  47. - (BOOL)isEqual:(id)other {
  48. if (self == other) {
  49. return YES;
  50. }
  51. if (![other isKindOfClass:[FIRGeoPoint class]]) {
  52. return NO;
  53. }
  54. FIRGeoPoint *otherGeoPoint = (FIRGeoPoint *)other;
  55. return DoubleBitwiseEquals(self.latitude, otherGeoPoint.latitude) &&
  56. DoubleBitwiseEquals(self.longitude, otherGeoPoint.longitude);
  57. }
  58. - (NSUInteger)hash {
  59. return 31 * DoubleBitwiseHash(self.latitude) + DoubleBitwiseHash(self.longitude);
  60. }
  61. /** Implements NSCopying without actually copying because geopoints are immutable. */
  62. - (id)copyWithZone:(__unused NSZone *_Nullable)zone {
  63. return self;
  64. }
  65. @end
  66. @implementation FIRGeoPoint (Internal)
  67. - (NSComparisonResult)compare:(FIRGeoPoint *)other {
  68. NSComparisonResult result = WrapCompare<double>(self.latitude, other.latitude);
  69. if (result != NSOrderedSame) {
  70. return result;
  71. } else {
  72. return WrapCompare<double>(self.longitude, other.longitude);
  73. }
  74. }
  75. @end
  76. NS_ASSUME_NONNULL_END