Files
firebase-ios-sdk/Firebase/Database/Core/FWriteRecord.m
T

139 lines
4.1 KiB
Objective-C
Raw Normal View History

2017-05-15 12:27:07 -07:00
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "FWriteRecord.h"
#import "FCompoundWrite.h"
2019-08-05 13:27:47 -07:00
#import "FNode.h"
#import "FPath.h"
2017-05-15 12:27:07 -07:00
@interface FWriteRecord ()
2019-08-05 13:27:47 -07:00
@property(nonatomic, readwrite) NSInteger writeId;
@property(nonatomic, strong, readwrite) FPath *path;
@property(nonatomic, strong, readwrite) id<FNode> overwrite;
@property(nonatomic, strong, readwrite) FCompoundWrite *merge;
@property(nonatomic, readwrite) BOOL visible;
2017-05-15 12:27:07 -07:00
@end
@implementation FWriteRecord
2019-08-05 13:27:47 -07:00
- (id)initWithPath:(FPath *)path
overwrite:(id<FNode>)overwrite
writeId:(NSInteger)writeId
visible:(BOOL)isVisible {
2017-05-15 12:27:07 -07:00
self = [super init];
if (self) {
self.path = path;
if (overwrite == nil) {
2019-08-05 13:27:47 -07:00
[NSException raise:NSInvalidArgumentException
format:@"Can't pass nil as overwrite parameter to an "
@"overwrite write record"];
2017-05-15 12:27:07 -07:00
}
self.overwrite = overwrite;
self.merge = nil;
self.writeId = writeId;
self.visible = isVisible;
}
return self;
}
2019-08-05 13:27:47 -07:00
- (id)initWithPath:(FPath *)path
merge:(FCompoundWrite *)merge
writeId:(NSInteger)writeId {
2017-05-15 12:27:07 -07:00
self = [super init];
if (self) {
self.path = path;
if (merge == nil) {
2019-08-05 13:27:47 -07:00
[NSException raise:NSInvalidArgumentException
format:@"Can't pass nil as merge parameter to an merge "
@"write record"];
2017-05-15 12:27:07 -07:00
}
self.overwrite = nil;
self.merge = merge;
self.writeId = writeId;
self.visible = YES;
}
return self;
}
- (id<FNode>)overwrite {
if (self->_overwrite == nil) {
2019-08-05 13:27:47 -07:00
[NSException raise:NSInvalidArgumentException
format:@"Can't get overwrite for merge write record!"];
2017-05-15 12:27:07 -07:00
}
return self->_overwrite;
}
- (FCompoundWrite *)compoundWrite {
if (self->_merge == nil) {
2019-08-05 13:27:47 -07:00
[NSException raise:NSInvalidArgumentException
format:@"Can't get merge for overwrite write record!"];
2017-05-15 12:27:07 -07:00
}
return self->_merge;
}
- (BOOL)isMerge {
return self->_merge != nil;
}
- (BOOL)isOverwrite {
return self->_overwrite != nil;
}
- (NSString *)description {
if (self.isOverwrite) {
2019-08-05 13:27:47 -07:00
return
[NSString stringWithFormat:@"FWriteRecord { writeId = %lu, path = "
@"%@, overwrite = %@, visible = %d }",
(unsigned long)self.writeId, self.path,
self.overwrite, self.visible];
2017-05-15 12:27:07 -07:00
} else {
2019-08-05 13:27:47 -07:00
return [NSString
stringWithFormat:
@"FWriteRecord { writeId = %lu, path = %@, merge = %@ }",
2017-05-15 12:27:07 -07:00
(unsigned long)self.writeId, self.path, self.merge];
}
}
- (BOOL)isEqual:(id)object {
if (![object isKindOfClass:[self class]]) {
return NO;
}
FWriteRecord *other = (FWriteRecord *)object;
2019-08-05 13:27:47 -07:00
if (self->_writeId != other->_writeId)
return NO;
if (self->_path != other->_path && ![self->_path isEqual:other->_path])
return NO;
if (self->_overwrite != other->_overwrite &&
![self->_overwrite isEqual:other->_overwrite])
return NO;
if (self->_merge != other->_merge && ![self->_merge isEqual:other->_merge])
return NO;
if (self->_visible != other->_visible)
return NO;
2017-05-15 12:27:07 -07:00
return YES;
}
- (NSUInteger)hash {
NSUInteger hash = self->_writeId * 17;
hash = hash * 31 + self->_path.hash;
hash = hash * 31 + self->_overwrite.hash;
hash = hash * 31 + self->_merge.hash;
hash = hash * 31 + ((self->_visible) ? 1 : 0);
return hash;
}
@end