import {TerminalOps, terminalOps, terminalSink} from "./TerminalOps"; import {PipelineHelper} from "./PipelineHelper"; import {supplier, supplierFn} from "../Interface"; import {StreamShape} from "./StreamShape"; import {Enum} from "../Enum"; import {sink} from "./Sink"; import {Spliterator} from "../utils/Spliterator"; import { UnsupportedOperationException} from "../Exception"; import {Predication} from "../utils/Predication"; interface BooleanTerminalSink extends sink{ stop:boolean value:boolean getAndClearState():boolean } class MatchKind extends Enum{ @Enum.args(true, true) static ANY:MatchKind; @Enum.args(false, false) static ALL:MatchKind; @Enum.args(true, false) static NONE:MatchKind; private readonly stopOnPredicateMatches:boolean; private readonly shortCircuitResult:boolean; constructor(stopOnPredicateMatches:boolean, shortCircuitResult:boolean) { super(); this.stopOnPredicateMatches=stopOnPredicateMatches; this.shortCircuitResult = shortCircuitResult; } public getStopOnPredicateMatches():boolean{return this.stopOnPredicateMatches;} public getShortCircuitResult():boolean{return this.shortCircuitResult;} } export class MatchOps { private constructor() {} /*** * @type {MatchKind} */ public static readonly MatchKind = MatchKind; /*** * * @param predicate * @param {MatchKind} matcher * @returns {TerminalOps} */ public static makeRef(predicate:any, matcher:MatchKind):TerminalOps{ class MatchSink extends MatchOps.BooleanTerminalSink{ constructor() {super(matcher);} accept(o: T) { if (!this.stop && predicate.test(o) == matcher.getStopOnPredicateMatches()) { this.stop = true; this.value = matcher.getStopOnPredicateMatches(); } } } return new MatchOps.MatchOp(StreamShape.REFERENCE,matcher,new class implements supplier>{ get: supplierFn> = ()=> new MatchSink(); }); } /*** * * @param predicate * @param {MatchKind} matcher * @returns {TerminalOps} */ public static makeInt(predicate:Predication, matcher:MatchKind):TerminalOps{ class MatchSink extends MatchOps.BooleanTerminalSink{ constructor() {super(matcher);} accept(o: number) { if (!this.stop && predicate.test(o) == matcher.getStopOnPredicateMatches()) { this.stop = true; this.value = matcher.getStopOnPredicateMatches(); } } } return new MatchOps.MatchOp(StreamShape.INT_VALUE,matcher,new class implements supplier>{ get: supplierFn> = ()=> new MatchSink(); }); } private static readonly BooleanTerminalSink = class BooleanTerminalSink implements BooleanTerminalSink{ stop:boolean; value:boolean; constructor(matcher:MatchKind) { this.value = !matcher.getShortCircuitResult(); } getAndClearState(): boolean {return this.value;} cancellationRequested(): boolean {return this.stop;} /**@toDoImplement**/ accept(o: T): void { throw new UnsupportedOperationException("To do implement")} begin(value: number): void {} end(): void {} }; private static readonly MatchOp = class MatchOp implements terminalOps{ private readonly shape: StreamShape; private readonly matchKind: MatchKind; private readonly sinkSupplier:supplier>; constructor(shape:StreamShape, matchKind: MatchKind,sinkSupplier: supplier> ) { this.shape = shape; this.matchKind = matchKind; this.sinkSupplier = sinkSupplier; } evaluateSequential(helper: PipelineHelper, spliterator: Spliterator): boolean { return helper.wrapAndCopyInto(this.sinkSupplier.get(), spliterator).getAndClearState(); } /**@Mock**/ getOpFlag(): number {return 1;} inputShape(): StreamShape {return this.shape;} } } Object.package(this);