diff options
author | Joel Fischer <joeljfischer@gmail.com> | 2016-05-25 08:44:23 -0400 |
---|---|---|
committer | Joel Fischer <joeljfischer@gmail.com> | 2016-05-25 08:44:23 -0400 |
commit | fb8e9903a323acaf5fc78819bb3c203567542ab2 (patch) | |
tree | e40665103ac7db492e0a40e34cd92f3390defa55 /SmartDeviceLink/SDLPrioritizedObjectCollection.m | |
parent | f7540a02262832e34c67b0953dd8a1804a046fea (diff) | |
download | sdl_ios-fb8e9903a323acaf5fc78819bb3c203567542ab2.tar.gz |
Shift files into root directory
Diffstat (limited to 'SmartDeviceLink/SDLPrioritizedObjectCollection.m')
-rw-r--r-- | SmartDeviceLink/SDLPrioritizedObjectCollection.m | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/SmartDeviceLink/SDLPrioritizedObjectCollection.m b/SmartDeviceLink/SDLPrioritizedObjectCollection.m new file mode 100644 index 000000000..637596d14 --- /dev/null +++ b/SmartDeviceLink/SDLPrioritizedObjectCollection.m @@ -0,0 +1,66 @@ +// +// SDLPrioritizedOutputCollection.m +// SmartDeviceLink +// + +#import "SDLPrioritizedObjectCollection.h" +#import "SDLObjectWithPriority.h" + + +@interface SDLPrioritizedObjectCollection () { + NSMutableArray *privateArray; +} +@end + + +@implementation SDLPrioritizedObjectCollection + +- (instancetype)init { + self = [super init]; + if (self) { + privateArray = [NSMutableArray new]; + } + return self; +} + +- (void)addObject:(id)object withPriority:(NSInteger)priority { + if (object == nil || [[NSNull null] isEqual:object]) { + return; + } + + SDLObjectWithPriority *newWrapper = [SDLObjectWithPriority objectWithObject:object priority:priority]; + + @synchronized(privateArray) { + // Find correct place to insert. + // Sorted in descending order. + BOOL lowerPriorityFound = NO; + NSInteger currentCount = privateArray.count; + for (int x = 0; x < currentCount; x++) { + SDLObjectWithPriority *o = privateArray[x]; + if (o.priority <= priority) { + lowerPriorityFound = YES; + [privateArray insertObject:newWrapper atIndex:x]; + break; + } + } + if (!lowerPriorityFound) { + [privateArray addObject:newWrapper]; + } + } +} + +- (instancetype)nextObject { + if (privateArray.count == 0) { + return nil; + } + + SDLObjectWithPriority *obj = nil; + @synchronized(privateArray) { + obj = (SDLObjectWithPriority *)[privateArray lastObject]; + [privateArray removeLastObject]; + } + + return obj.object; +} + +@end |