github.com/searKing/golang/go@v1.2.74/container/stream/op/match/op.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package match 6 7 import ( 8 "context" 9 10 "github.com/searKing/golang/go/container/stream/op/terminal" 11 "github.com/searKing/golang/go/util/function/predicate" 12 "github.com/searKing/golang/go/util/object" 13 "github.com/searKing/golang/go/util/optional" 14 "github.com/searKing/golang/go/util/spliterator" 15 ) 16 17 type MatchOp struct { 18 terminal.TODOOperation 19 matchKind Kind 20 sinkNewer func() *MatchSink 21 } 22 23 func NewMatchOp(matchKind Kind, sinkNewer func() *MatchSink) *MatchOp { 24 reduceOp := &MatchOp{ 25 matchKind: matchKind, 26 sinkNewer: sinkNewer, 27 } 28 reduceOp.SetDerived(reduceOp) 29 return reduceOp 30 } 31 32 func NewMatchOp2(matchKind Kind, predicate predicate.Predicater) *MatchOp { 33 object.RequireNonNil(predicate) 34 return NewMatchOp(matchKind, func() *MatchSink { 35 return NewMatchSink(matchKind, predicate) 36 }) 37 } 38 39 func (op MatchOp) MakeSink() *MatchSink { 40 object.RequireNonNull(op.sinkNewer) 41 return op.sinkNewer() 42 } 43 44 func (op MatchOp) EvaluateParallel(ctx context.Context, spliterator spliterator.Spliterator) optional.Optional { 45 return terminal.WrapAndCopyInto(ctx, op.MakeSink(), spliterator).Get() 46 } 47 48 func (op MatchOp) EvaluateSequential(ctx context.Context, spliterator spliterator.Spliterator) optional.Optional { 49 // Approach for parallel implementation: 50 // - Decompose as per usual 51 // - run match on leaf chunks, call result "b" 52 // - if b == matchKind.shortCircuitOn, complete early and return b 53 // - else if we complete normally, return !shortCircuitOn 54 return NewMatchTask(op, spliterator).Invoke(ctx).Get() 55 }