import {TerminalOps, terminalOps, terminalSink} from "./TerminalOps"; import {StreamShape} from "./StreamShape"; import {PipelineHelper} from "./PipelineHelper"; import {biConsumer, collector, ObjIntConsumer, supplier} from "../Interface"; import {UnsupportedOperationException} from "../Exception"; import {Spliterator} from "../utils/Spliterator"; import {OptionalInt} from "../utils/OptionalInt"; import {Objects} from "../type/Objects"; import {Optional} from "../utils/Optional"; import {BinaryOperator} from "../utils/BinaryOperator"; interface AccumulatingSink> extends terminalSink { combine(other:K):void; } /* * Evaluate * **/ export class ReduceOps { private constructor() {} /*** * * @type {Box>} */ private static Box = class Box{ state: T; constructor() {} public get():T{ return this.state; } }; /*** * @Nested * @type {terminalOps} */ private static ReduceOp = class ReduceOp> implements terminalOps{ private readonly shape:StreamShape; constructor(shape:StreamShape) {this.shape = shape;} // @toOverriding public makeSink():S{throw new UnsupportedOperationException();} evaluateSequential(helper: PipelineHelper, spliterator: Spliterator): R { return helper.wrapAndCopyInto(this.makeSink(),spliterator).get(); } getOpFlag(): number {return 0;} inputShape(): StreamShape {return this.shape;} }; /*** * @RefPipeline * @see Reduce, Min, Max */ public static makeRefOperator(operator:BinaryOperator):TerminalOps>{ Objects.requireNotNull(operator); class ReduceSink implements AccumulatingSink, ReduceSink> { private state:T; private empty:boolean; begin(value: number):void { this.state = null; this.empty = true; } accept(o: T): void { if(this.empty){ this.empty=!this.empty; this.state = o; }else{ this.state = operator.apply(this.state, o); } } /*** * @override **/ public get():Optional {return this.empty ? Optional.empty() : Optional.ofNullable(this.state);} // toDo decommission combine(other: ReduceSink): void {} cancellationRequested(): boolean {return false;} end(): void {} } return new class extends ReduceOps.ReduceOp,ReduceSink>{ constructor() {super(StreamShape.REFERENCE);} makeSink(): ReduceSink {return new ReduceSink();} } } /*** * @RefPipeline * @param {collector} collector * @return {TerminalOps} */ public static makeRefCollect(collector:collector):TerminalOps{ let supplier:supplier = Objects.requireNotNull(collector.supplier()), accumulator:biConsumer = Objects.requireNotNull(collector.accumulator()); class ReduceSink extends ReduceOps.Box implements AccumulatingSink { begin(value: number):void {this.state = supplier.get();} accept(o: T): void {accumulator.accept(this.state, o);} combine(other: ReduceSink): void {} cancellationRequested(): boolean {return false;} end(): void {} } return new class extends ReduceOps.ReduceOp{ constructor() {super(StreamShape.REFERENCE);} makeSink(): ReduceSink {return new ReduceSink();} } } public static makeRef(supplier:supplier, accumulator:biConsumer):TerminalOps{ Objects.requireNotNull(supplier); Objects.requireNotNull(accumulator); class ReduceSink extends ReduceOps.Box implements AccumulatingSink { begin(value: number):void {this.state = supplier.get();} accept(o: T): void {accumulator.accept(this.state, o);} combine(other: ReduceSink): void {} cancellationRequested(): boolean {return false;} end(): void {} } return new class extends ReduceOps.ReduceOp{ constructor() {super(StreamShape.REFERENCE);} makeSink(): ReduceSink {return new ReduceSink();} } } public static makeIntOperator(op:Function):TerminalOps{ Objects.requireNotNull(op); class ReduceSink implements AccumulatingSink{ private empty:boolean; private state:number; accept(o: number): void { if (this.empty) { this.empty = false; this.state = o; } else { this.state = op.call(null,this.state, o); } } get(): OptionalInt {return this.empty ? OptionalInt.empty() : OptionalInt.of(this.state);} begin(value: number): void { this.empty = true; this.state = 0; } cancellationRequested(): boolean {return false;} combine(other: ReduceSink): void { // } end(): void {} } return new class extends ReduceOps.ReduceOp{ constructor() {super(StreamShape.INT_VALUE);} makeSink(): ReduceSink {return new ReduceSink();} } } public static makeIntOperatorN(start:number, op:Function):TerminalOps{ Objects.requireNotNull(op); class ReduceSink implements AccumulatingSink{ private state:number; accept(o: number): void { this.state = op.call(null,this.state, o); } get(): number {return this.state;} begin(value: number): void {this.state = start;} cancellationRequested(): boolean {return false;} combine(other: ReduceSink): void { // } end(): void {} } return new class extends ReduceOps.ReduceOp{ constructor() {super(StreamShape.INT_VALUE);} makeSink(): ReduceSink {return new ReduceSink();} } } public static makeIntSupplier(supplier:supplier, accumulator: ObjIntConsumer):TerminalOps{ class ReduceSink extends ReduceOps.Box implements AccumulatingSink{ accept(o: number): void {accumulator.accept(this.state,o);} begin(value: number): void {this.state = supplier.get();} cancellationRequested(): boolean {return false;} combine(other: ReduceSink): void { } end(): void {} } return new class extends ReduceOps.ReduceOp{ constructor() {super(StreamShape.INT_VALUE);} makeSink(): ReduceSink {return new ReduceSink();} } } } Object.package(this);