FNamedNode.m 2.4 KB

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