import { consumer, IConsumer, intStream, IntStreamBuilder, IStreamBuilder, spliterator, Stream } from "../Interface"; import { Spliterators} from "../utils/Spliterators"; import {Exception} from "../Exception"; import {StreamSupport} from "./StreamSupport"; import {Objects} from "../type/Objects"; /**MOCK*/ export interface StreamImpl extends IConsumer, spliterator{ } export abstract class Streams{ public static StreamImpl = class StreamImpl implements StreamImpl{ buffer:T[]; counter:number = 1; accept(o: T): void { if(Objects.isNull(this.buffer)) this.buffer = [o]; else{ this.buffer[this.counter++]=o; } } forEachRemaining(action: consumer): void { let i:number = 0; if(this.buffer.length<=0) return; do{ (>action).accept( this.buffer[i++] ); }while (!Objects.isNull(this.buffer[i])); } tryAdvance(action: consumer): boolean { return false; } trySplit(): spliterator { return undefined; } estimateSize(): number {return 0;} } public static StreamBuilder = class StreamBuilderImpl extends Streams.StreamImpl implements IStreamBuilder> { add(t: T): StreamBuilderImpl { if( this.counter > 0 ) super.accept(t); else{ throw new Exception(); } return this; } build(): Stream { if(this.counter<0) return; this.counter = -this.counter; return StreamSupport.stream(new Spliterators.ArraySpliterator(this.buffer)); } estimateSize(): number {return this.counter;} }; public static IntStreamBuilder = class IntStreamBuilderImpl extends Streams.StreamImpl implements IntStreamBuilder { accept(o: number): void {super.accept(o);} add(t: number): IntStreamBuilder { if( this.counter > 0 ) super.accept(t); else{ throw new Exception(); } return this; } build(): intStream { if(this.counter<0) return; this.counter = -this.counter; return StreamSupport.intStream(new Spliterators.IntArraySpliterator(this.buffer)) ; } estimateSize(): number {return this.counter;} } }