FTrackedQuery.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 query:(FQuerySpec *)query lastUse:(NSTimeInterval)lastUse isActive:(BOOL)isActive {
  42. return [self initWithId:queryId query:query lastUse:lastUse isActive:isActive isComplete:NO];
  43. }
  44. - (FTrackedQuery *)updateLastUse:(NSTimeInterval)lastUse {
  45. return [[FTrackedQuery alloc] initWithId:self.queryId
  46. query:self.query
  47. lastUse:lastUse
  48. isActive:self.isActive
  49. isComplete:self.isComplete];
  50. }
  51. - (FTrackedQuery *)setComplete {
  52. return [[FTrackedQuery alloc] initWithId:self.queryId
  53. query:self.query
  54. lastUse:self.lastUse
  55. isActive:self.isActive
  56. isComplete:YES];
  57. }
  58. - (FTrackedQuery *)setActiveState:(BOOL)isActive {
  59. return [[FTrackedQuery alloc] initWithId:self.queryId
  60. query:self.query
  61. lastUse:self.lastUse
  62. isActive:isActive
  63. isComplete:self.isComplete];
  64. }
  65. - (BOOL)isEqual:(id)object {
  66. if (![object isKindOfClass:[FTrackedQuery class]]) {
  67. return NO;
  68. }
  69. FTrackedQuery *other = (FTrackedQuery *)object;
  70. if (self.queryId != other.queryId) return NO;
  71. if (self.query != other.query && ![self.query isEqual:other.query]) return NO;
  72. if (self.lastUse != other.lastUse) return NO;
  73. if (self.isComplete != other.isComplete) return NO;
  74. if (self.isActive != other.isActive) return NO;
  75. return YES;
  76. }
  77. - (NSUInteger)hash {
  78. NSUInteger hash = self.queryId;
  79. hash = hash * 31 + self.query.hash;
  80. hash = hash * 31 + (self.isActive ? 1 : 0);
  81. hash = hash * 31 + (NSUInteger)self.lastUse;
  82. hash = hash * 31 + (self.isComplete ? 1 : 0);
  83. hash = hash * 31 + (self.isActive ? 1 : 0);
  84. return hash;
  85. }
  86. @end