FNamedNode.m 2.6 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 "FirebaseDatabase/Sources/FNamedNode.h"
  17. #import "FirebaseDatabase/Sources/FIndex.h"
  18. #import "FirebaseDatabase/Sources/FMaxNode.h"
  19. #import "FirebaseDatabase/Sources/Snapshot/FEmptyNode.h"
  20. #import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
  21. @interface FNamedNode ()
  22. @property(nonatomic, strong, readwrite) NSString *name;
  23. @property(nonatomic, strong, readwrite) id<FNode> node;
  24. @end
  25. @implementation FNamedNode
  26. + (FNamedNode *)nodeWithName:(NSString *)name node:(id<FNode>)node {
  27. return [[FNamedNode alloc] initWithName:name andNode:node];
  28. }
  29. - (id)initWithName:(NSString *)name andNode:(id<FNode>)node {
  30. self = [super init];
  31. if (self) {
  32. self.name = name;
  33. self.node = node;
  34. }
  35. return self;
  36. }
  37. - (id)copy {
  38. return self;
  39. }
  40. - (id)copyWithZone:(NSZone *)zone {
  41. return self;
  42. }
  43. + (FNamedNode *)min {
  44. static FNamedNode *min = nil;
  45. static dispatch_once_t once;
  46. dispatch_once(&once, ^{
  47. min = [[FNamedNode alloc] initWithName:[FUtilities minName]
  48. andNode:[FEmptyNode emptyNode]];
  49. });
  50. return min;
  51. }
  52. + (FNamedNode *)max {
  53. static FNamedNode *max = nil;
  54. static dispatch_once_t once;
  55. dispatch_once(&once, ^{
  56. max = [[FNamedNode alloc] initWithName:[FUtilities maxName]
  57. andNode:[FMaxNode maxNode]];
  58. });
  59. return max;
  60. }
  61. - (NSString *)description {
  62. return
  63. [NSString stringWithFormat:@"NamedNode[%@] %@", self.name, self.node];
  64. }
  65. - (BOOL)isEqual:(id)object {
  66. if (self == object) {
  67. return YES;
  68. }
  69. if (object == nil || ![object isKindOfClass:[FNamedNode class]]) {
  70. return NO;
  71. }
  72. FNamedNode *namedNode = object;
  73. if (![self.name isEqualToString:namedNode.name]) {
  74. return NO;
  75. }
  76. if (![self.node isEqual:namedNode.node]) {
  77. return NO;
  78. }
  79. return YES;
  80. }
  81. - (NSUInteger)hash {
  82. NSUInteger nameHash = [self.name hash];
  83. NSUInteger nodeHash = [self.node hash];
  84. NSUInteger result = 31 * nameHash + nodeHash;
  85. return result;
  86. }
  87. @end