github.com/searKing/golang/go@v1.2.74/container/stream/op/while/drop_while_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 while 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 /** 18 * A specialization for the dropWhile operation that controls if 19 * elements to be dropped are counted and passed downstream. 20 * <p> 21 * This specialization is utilized by the {@link TakeWhileTask} for 22 * pipelines that are ordered. In such cases elements cannot be dropped 23 * until all elements have been collected. 24 * 25 * @param <T> the type of both input and output elements 26 */ 27 type DropWhileOp struct { 28 terminal.TODOOperation 29 sinkNewer func() DropWhileSink 30 } 31 32 func NewDropWhileOp(sinkNewer func() DropWhileSink) *DropWhileOp { 33 reduceOp := &DropWhileOp{ 34 sinkNewer: sinkNewer, 35 } 36 return reduceOp 37 } 38 39 func NewDropWhileOp2(retainAndCountDroppedElements bool, predicate predicate.Predicater) *DropWhileOp { 40 object.RequireNonNil(predicate) 41 return NewDropWhileOp(func() DropWhileSink { 42 return NewDropWhileSink(retainAndCountDroppedElements, predicate) 43 }) 44 } 45 46 func (op DropWhileOp) MakeSink() DropWhileSink { 47 object.RequireNonNull(op.sinkNewer) 48 return op.sinkNewer() 49 } 50 51 func (op DropWhileOp) EvaluateParallel(ctx context.Context, spliterator spliterator.Spliterator) optional.Optional { 52 return terminal.WrapAndCopyInto(ctx, op.MakeSink(), spliterator).Get() 53 } 54 55 func (op DropWhileOp) EvaluateSequential(ctx context.Context, spliterator spliterator.Spliterator) optional.Optional { 56 // Approach for parallel implementation: 57 // - Decompose as per usual 58 // - run match on leaf chunks, call result "b" 59 // - if b == matchKind.shortCircuitOn, complete early and return b 60 // - else if we complete normally, return !shortCircuitOn 61 return (&DropWhileTask{}).WithSpliterator(op, spliterator).Invoke(ctx).Get() 62 }