import { BiConsumer, BinaryOperator, BiPredicate, Collector, Comparator, Consumer, Decuple, Maybe, Nonuple, Octuple, Pair, Predicate, Quadruple, Quintuple, Septuple, Sextuple, Single, Supplier, Triple, TypedBiFunction, TypedFunction, } from "andross"; import { IStream, ITry, ITryStream, } from "./Interfaces"; import { collect, collectWith, consumeFirst, end, every, find, findIndex, first, fork, group, has, isEmpty, isSizeBetween, join, last, max, maxBy, min, minBy, none, nth, partition, reduce, reduceSame, size, slice, some, splice, sum, toArray, toMap, toSet, tryCompute, tryEnd, } from "./Methods"; /** * @private */ export abstract class AbstractStream implements IStream { protected iterable: Iterable; private done = false; public constructor(iterable: Iterable) { this.iterable = iterable; } public [Symbol.iterator]() { this.check(); return this.iterable[Symbol.iterator](); } public collect(collector: Collector): R { this.check(); return collect(this.iterable, collector); } public collectWith(supplier: Supplier, accumulator: BiConsumer, finisher: TypedFunction): R { this.check(); return collectWith(this.iterable, supplier, accumulator, finisher); } public end(): void { this.check(); end(this.iterable); } public every(predicate: Predicate): boolean { this.check(); return every(this.iterable, predicate); } public find(predicate: BiPredicate): Maybe { this.check(); return find(this.iterable, predicate); } public findIndex(predicate: BiPredicate): number { this.check(); return findIndex(this.iterable, predicate); } public first(): Maybe { this.check(); return first(this.iterable); } public forEach(consumer: Consumer): void { this.visit(consumer).end(); } public fork(): this { this.checkOnly(); this.iterable = fork(this.iterable); return this.clone(this.iterable); } public group(classifier: TypedFunction): Map { this.check(); return group(this.iterable, classifier); } public has(object: T): boolean { this.check(); return has(this.iterable, object); } public isEmpty(): boolean { this.checkOnly(); const result = isEmpty(this.iterable); this.iterable = result.iterable; return result.result; } public isSizeBetween(lower?: number, upper?: number): boolean { this.checkOnly(); const result = isSizeBetween(this.iterable, lower, upper); this.iterable = result.iterable; return result.result; } public join(delimiter?: string, prefix?: string, suffix?: string): string { this.check(); return join(this.iterable, delimiter, prefix, suffix); } public last(): Maybe { this.check(); return last(this.iterable); } public none(predicate: Predicate): boolean { this.check(); return none(this.iterable, predicate); } public nth(n: number): Maybe { this.check(); return nth(this.iterable, n); } public max(comparator?: Comparator): Maybe { this.check(); return max(this.iterable, comparator); } public maxBy(sortKey: TypedFunction): Maybe { this.check(); return maxBy(this.iterable, sortKey); } public min(comparator?: Comparator): Maybe { this.check(); return min(this.iterable, comparator); } public minBy(sortKey: TypedFunction): Maybe { this.check(); return minBy(this.iterable, sortKey); } public partition(predicate: Predicate): { false: T[], true: T[] } { this.check(); return partition(this.iterable, predicate); } public reduce(reducer: TypedBiFunction, initialValue: S): S { this.check(); return reduce(this.iterable, reducer, initialValue); } public reduceSame(reducer: TypedBiFunction): Maybe { this.check(); return reduceSame(this.iterable, reducer); } public size(): number { this.check(); return size(this.iterable); } public shift(): Maybe { this.checkOnly(); let result: Maybe; this.iterable = consumeFirst(this.iterable, item => result = item); return result; } public some(predicate: Predicate): boolean { this.check(); return some(this.iterable, predicate); } public slice(startOffset?: number, endOffset?: number): T[] { this.checkOnly(); const result = slice(this.iterable, startOffset, endOffset); this.iterable = result.iterable; return result.result; } public splice(offset?: number, maxAmount?: number): T[] { this.checkOnly(); const result = splice(this.iterable, offset, maxAmount); this.iterable = result.iterable; return result.result; } public sum(converter?: TypedFunction): number { this.check(); return sum(this.iterable, converter); } public toArray(fresh?: boolean): T[] { this.check(); return toArray(this.iterable, fresh); } public toSet(fresh?: boolean): Set { this.check(); return toSet(this.iterable, fresh); } public toJSON(): T[] { return this.toArray(); } public toString() { return `Stream[done=${this.done}]`; } public toMap(keyMapper: TypedFunction, valueMapper: TypedFunction, merger?: BinaryOperator): Map { this.check(); return toMap(this.iterable, keyMapper, valueMapper, merger); } public tryCompute(operation: TypedFunction, S>): ITry { this.checkOnly(); return tryCompute(this, operation); } public tryEnd(): ITry { this.check(); return tryEnd(this.iterable); } public abstract chunkBy(classifier: TypedBiFunction): IStream; public abstract concat(...iterables: Iterable[]): this; public abstract consume(sink: T[] | Consumer, offset?: number, maxAmount?: number): this; public abstract cycle(count?: number): this; public abstract flatMap(mapper: TypedFunction>): IStream; public abstract filter(predicate: Predicate): this; public abstract filterBy(target: K, keyExtractor?: TypedFunction, comparator?: Comparator): this; public abstract index(): IStream<{index: number, value: T}>; public abstract limit(limitTo: number): this; public abstract map(mapper: TypedFunction): IStream; public abstract promise(promiseConverter: TypedFunction>): Promise>; public abstract reverse(): this; public abstract skip(toSkip: number): this; public abstract chunk(chunkSize: 0): IStream; public abstract chunk(chunkSize: 1): IStream>; public abstract chunk(chunkSize: 2): IStream | Single>; public abstract chunk(chunkSize: 3): IStream | Pair | Single>; public abstract chunk(chunkSize: 4): IStream | Triple | Pair | Single>; public abstract chunk(chunkSize: 5): IStream | Quadruple | Triple | Pair | Single>; public abstract chunk(chunkSize: 6): IStream | Quintuple | Quadruple | Triple | Pair | Single>; public abstract chunk(chunkSize: 7): IStream | Sextuple | Quintuple | Quadruple | Triple | Pair | Single>; public abstract chunk(chunkSize: 8): IStream | Septuple | Sextuple | Quintuple | Quadruple | Triple | Pair | Single>; public abstract chunk(chunkSize: 9): IStream | Octuple | Septuple | Sextuple | Quintuple | Quadruple | Triple | Pair | Single>; public abstract chunk(chunkSize: 10): IStream | Nonuple | Octuple | Septuple | Sextuple | Quintuple | Quadruple | Triple | Pair | Single>; public abstract chunk(sliceSize: number): IStream; public abstract sort(comparator?: Comparator): this; public abstract sortBy(keyExtractor: TypedFunction, comparator?: Comparator): this; public abstract try(operation: TypedFunction): ITryStream; public abstract unique(comparator?: Comparator): this; public abstract uniqueBy(keyExtractor?: TypedFunction): this; public abstract visit(consumer: Consumer): this; public abstract zip(other: Iterable): IStream<[Maybe, Maybe]>; public abstract zipSame(...others: Iterable[]): IStream[]>; protected abstract clone(iterable: Iterable): this; protected checkOnly() { if (this.done) { throw new Error("Stream was already consumed."); } } protected check() { if (this.done) { throw new Error("Stream was already consumed."); } this.done = true; } }