FTrackedQuery.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "FTrackedQuery.h"
  17. #import "FQuerySpec.h"
  18. @interface FTrackedQuery ()
  19. @property(nonatomic, readwrite) NSUInteger queryId;
  20. @property(nonatomic, strong, readwrite) FQuerySpec *query;
  21. @property(nonatomic, readwrite) NSTimeInterval lastUse;
  22. @property(nonatomic, readwrite) BOOL isComplete;
  23. @property(nonatomic, readwrite) BOOL isActive;
  24. @end
  25. @implementation FTrackedQuery
  26. - (id)initWithId:(NSUInteger)queryId
  27. query:(FQuerySpec *)query
  28. lastUse:(NSTimeInterval)lastUse
  29. isActive:(BOOL)isActive
  30. isComplete:(BOOL)isComplete {
  31. self = [super init];
  32. if (self != nil) {
  33. self->_queryId = queryId;
  34. self->_query = query;
  35. self->_lastUse = lastUse;
  36. self->_isComplete = isComplete;
  37. self->_isActive = isActive;
  38. }
  39. return self;
  40. }
  41. - (id)initWithId:(NSUInteger)queryId
  42. query:(FQuerySpec *)query
  43. lastUse:(NSTimeInterval)lastUse
  44. isActive:(BOOL)isActive {
  45. return [self initWithId:queryId
  46. query:query
  47. lastUse:lastUse
  48. isActive:isActive
  49. isComplete:NO];
  50. }
  51. - (FTrackedQuery *)updateLastUse:(NSTimeInterval)lastUse {
  52. return [[FTrackedQuery alloc] initWithId:self.queryId
  53. query:self.query
  54. lastUse:lastUse
  55. isActive:self.isActive
  56. isComplete:self.isComplete];
  57. }
  58. - (FTrackedQuery *)setComplete {
  59. return [[FTrackedQuery alloc] initWithId:self.queryId
  60. query:self.query
  61. lastUse:self.lastUse
  62. isActive:self.isActive
  63. isComplete:YES];
  64. }
  65. - (FTrackedQuery *)setActiveState:(BOOL)isActive {
  66. return [[FTrackedQuery alloc] initWithId:self.queryId
  67. query:self.query
  68. lastUse:self.lastUse
  69. isActive:isActive
  70. isComplete:self.isComplete];
  71. }
  72. - (BOOL)isEqual:(id)object {
  73. if (![object isKindOfClass:[FTrackedQuery class]]) {
  74. return NO;
  75. }
  76. FTrackedQuery *other = (FTrackedQuery *)object;
  77. if (self.queryId != other.queryId)
  78. return NO;
  79. if (self.query != other.query && ![self.query isEqual:other.query])
  80. return NO;
  81. if (self.lastUse != other.lastUse)
  82. return NO;
  83. if (self.isComplete != other.isComplete)
  84. return NO;
  85. if (self.isActive != other.isActive)
  86. return NO;
  87. return YES;
  88. }
  89. - (NSUInteger)hash {
  90. NSUInteger hash = self.queryId;
  91. hash = hash * 31 + self.query.hash;
  92. hash = hash * 31 + (self.isActive ? 1 : 0);
  93. hash = hash * 31 + (NSUInteger)self.lastUse;
  94. hash = hash * 31 + (self.isComplete ? 1 : 0);
  95. hash = hash * 31 + (self.isActive ? 1 : 0);
  96. return hash;
  97. }
  98. @end