//
//  YSRACEmptySequence.m
//  ReactiveObjC
//
//  Created by Justin Spahr-Summers on 2012-10-29.
//  Copyright (c) 2012 GitHub. All rights reserved.
//

#import "YSRACEmptySequence.h"

@implementation YSRACEmptySequence

#pragma mark Lifecycle

+ (instancetype)empty {
	static id singleton;
	static dispatch_once_t pred;

	dispatch_once(&pred, ^{
		singleton = [[self alloc] init];
	});

	return singleton;
}

#pragma mark YSRACSequence

- (id)head {
	return nil;
}

- (YSRACSequence *)tail {
	return nil;
}

- (YSRACSequence *)bind:(YSRACStreamBindBlock)bindBlock passingThroughValuesFromSequence:(YSRACSequence *)passthroughSequence {
	return passthroughSequence ?: self;
}

#pragma mark NSCoding

- (Class)classForCoder {
	// Empty sequences should be encoded as themselves, not array sequences.
	return self.class;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
	// Return the singleton.
	return self.class.empty;
}

- (void)encodeWithCoder:(NSCoder *)coder {
}

#pragma mark NSObject

- (NSString *)description {
	return [NSString stringWithFormat:@"<%@: %p>{ name = %@ }", self.class, self, self.name];
}

- (NSUInteger)hash {
	// This hash isn't ideal, but it's better than -[YSRACSequence hash], which
	// would just be zero because we have no head.
	return (NSUInteger)(__bridge void *)self;
}

- (BOOL)isEqual:(YSRACSequence *)seq {
	return (self == seq);
}

@end
