import {terminalOps, terminalSink} from "./TerminalOps"; import {PipelineHelper} from "./PipelineHelper"; import { predicate, supplier, supplierFn} from "../Interface"; import {StreamShape} from "./StreamShape"; import {Spliterator} from "../utils/Spliterator"; import {Optional} from "../utils/Optional"; import {ofInt} from "./Sink"; import {OptionalInt} from "../utils/OptionalInt"; export class FindOps { private constructor() {} private static readonly FindOp = class FindOps implements terminalOps{ protected readonly shapeOps:StreamShape; readonly mustFindFirst:boolean; // parallel readonly emptyValue:O; readonly sinkSupplier:supplier>; readonly predicate:predicate; // parallel readonly opFlags; constructor(mustFindFirst:boolean, shapeOps:StreamShape, emptyValue:O, predicate:predicate, sinkSupplier:supplier> ) { this.mustFindFirst = mustFindFirst; // parallel this.shapeOps = shapeOps; this.emptyValue = emptyValue; this.predicate = predicate; // parallel this.sinkSupplier = sinkSupplier; this.opFlags = 0x01; // MOCK } /**TerminalOps**/ evaluateSequential(helper: PipelineHelper, spliterator: Spliterator): O { let tmp:O = helper.wrapAndCopyInto(this.sinkSupplier.get(), spliterator).get(); return Optional.ofNullable(tmp).isPresent() ? tmp : this.emptyValue; } getOpFlag(): number {return this.opFlags;} inputShape(): StreamShape {return this.shapeOps;} }; private static readonly FindSink = class FindSink implements terminalSink{ protected hasValue:boolean; value:T; constructor() {} accept(o: T): void { if(!this.hasValue){ this.hasValue = true; this.value = o; } } /**TerminalSink**/ get(): O {return null;} begin(value: number): void {} /**@override**/ cancellationRequested(): boolean {return this.hasValue;} end(): void {} }; private static readonly FindSinkImpl = class FindSinkImpl{ public static readonly OfRef = class OfRef extends FindOps.FindSink>{ /**@override**/ public get():Optional {return this.hasValue ? Optional.of(this.value) : null;} public static readonly OP_FIND_FIRST = class OF_FIND_ANY implements supplier>>{ get = ()=> new FindOps.FindOp>( true, StreamShape.REFERENCE, >Optional.empty(), null, new class implements supplier>>{ get : supplierFn>> = () => new FindOps.FindSinkImpl.OfRef(); }) }; public static readonly OP_FIND_ANY = class OF_FIND_FIRST implements supplier>>{ get = ()=> new FindOps.FindOp>( false, StreamShape.REFERENCE, >Optional.empty(), null, new class implements supplier>>{ get : supplierFn>> = () => new FindOps.FindSinkImpl.OfRef(); }) } }; public static readonly OfInt = class OfInt extends FindOps.FindSink implements ofInt{ /**@override**/ accept(o: number) {super.accept(o);} /**@override**/ public get():OptionalInt {return this.hasValue ? OptionalInt.of(this.value) : null;} public static readonly OP_FIND_FIRST: terminalOps = new FindOps.FindOp( true, StreamShape.INT_VALUE, OptionalInt.empty(), null, new class implements supplier>{ get = ()=> new FindOps.FindSinkImpl.OfInt(); } ); public static readonly OP_FIND_ANY: terminalOps = new FindOps.FindOp( false, StreamShape.INT_VALUE, OptionalInt.empty(), null, new class implements supplier>{ get = ()=> new FindOps.FindSinkImpl.OfInt(); } ); } }; public static makeInt( findFirst:boolean ):terminalOps{ return findFirst ? FindOps.FindSinkImpl.OfInt.OP_FIND_FIRST : FindOps.FindSinkImpl.OfInt.OP_FIND_ANY; } public static makeRef( findFirst:boolean ):terminalOps>{ return ( findFirst ? new FindOps.FindSinkImpl.OfRef.OP_FIND_FIRST() : new FindOps.FindSinkImpl.OfRef.OP_FIND_ANY() ).get(); } } Object.package(this);