summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLPrioritizedObjectCollection.m
diff options
context:
space:
mode:
Diffstat (limited to 'SmartDeviceLink/SDLPrioritizedObjectCollection.m')
-rw-r--r--SmartDeviceLink/SDLPrioritizedObjectCollection.m66
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