github.com/searKing/golang/go@v1.2.74/util/spliterator/infinite_supplying_spliterator.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 spliterator 6 7 import ( 8 "context" 9 10 "github.com/searKing/golang/go/util/function/consumer" 11 "github.com/searKing/golang/go/util/function/supplier" 12 "github.com/searKing/golang/go/util/object" 13 ) 14 15 /** 16 * A Spliterator that infinitely supplies elements in no particular order. 17 * 18 * <p>Splitting divides the estimated size in two and stops when the 19 * estimate size is 0. 20 * 21 * <p>The {@code forEachRemaining} method if invoked will never terminate. 22 * The {@code tryAdvance} method always returns true. 23 * 24 */ 25 type infiniteSupplyingSpliterator struct { 26 TODO 27 estimate int 28 // The underlying spliterator 29 s supplier.Supplier 30 } 31 32 func NewInfiniteSupplyingSpliterator(size int, s supplier.Supplier) Spliterator { 33 split := &infiniteSupplyingSpliterator{ 34 estimate: size, 35 s: s, 36 } 37 split.SetDerived(split) 38 return split 39 } 40 41 func (split *infiniteSupplyingSpliterator) TrySplit() Spliterator { 42 if split.estimate == 0 { 43 return nil 44 } 45 return NewInfiniteSupplyingSpliterator(split.estimate>>1, split.s) 46 } 47 48 func (split *infiniteSupplyingSpliterator) TryAdvance(ctx context.Context, action consumer.Consumer) bool { 49 object.RequireNonNil(action) 50 action.Accept(split.s.Get()) 51 return true 52 } 53 54 func (split *infiniteSupplyingSpliterator) EstimateSize() int { 55 return split.estimate 56 } 57 58 func (split *infiniteSupplyingSpliterator) Characteristics() Characteristic { 59 return CharacteristicImmutable 60 }