//
//  NSMutableOrderedSet+gmsafekit.m
//  OJia
//
//  Created by 张培创 on 15/12/28.
//  Copyright © 2015年 com.ouj. All rights reserved.
//

#import "NSMutableOrderedSet+gmsafekit.h"

@implementation NSMutableOrderedSet (gmsafekit)

-(id) safeObjectAtIndex:(NSUInteger)index
{
    if (index>=self.count) {
        return nil;
    }
    return [self objectAtIndex:index];
}

-(void)safeAddObject:(id)anObject
{
    if (!anObject) {
        NSLog(@"safeWarning --addObject: NSMUtableArray add nil object");
        return;
    }
    [self addObject:anObject];
}

- (void)safeAddObjectsFromArray:(NSArray *)otherArray {
    if(otherArray.count > 0){
        [self addObjectsFromArray:otherArray];
    }
}

- (void)safeInsertObject:(id)anObject atIndex:(NSUInteger)index
{
    if (index > [self count]) {
        NSLog(@"%@" , [NSString stringWithFormat:@"safeWarning --insertObject: index[%ld] > count[%ld]",(long)index ,(long)[self count]]);
        return;
    }
    if (!anObject) {
        NSLog(@"safeWarning --insertObject: object is nil");
        return;
    }
    [self insertObject:anObject atIndex:index];
}

- (void)safeInsertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes {
    if(objects.count > 0 && indexes){
        [self insertObjects:objects atIndexes:indexes];
    }
}
- (void)safeRemoveObjectAtIndex:(NSUInteger)index
{
    if (index >= [self count]) {
        NSLog(@"%@" , [NSString stringWithFormat:@"safeWarning --removeObjectAtIndex: index[%ld] >= count[%ld]",(long)index ,(long)[self count]]);
        return;
    }
    
    return [self removeObjectAtIndex:index];
}
- (void)safeReplaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
{
    if (index >= [self count]) {
        NSLog(@"%@" , [NSString stringWithFormat:@"safeWarning --replaceObjectAtIndex: index[%ld] >= count[%ld]",(long)index ,(long)[self count]]);
        return;
    }
    if (!anObject) {
        NSLog(@"safeWarning --replaceObjectAtIndex: object is nil");
        return;
    }
    [self replaceObjectAtIndex:index withObject:anObject];
}

@end
