github.com/searKing/golang/go@v1.2.74/container/stream/op/find/sink.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 find
     6  
     7  import (
     8  	"github.com/searKing/golang/go/container/stream/op/terminal"
     9  	"github.com/searKing/golang/go/util/optional"
    10  )
    11  
    12  type FindSink struct {
    13  	terminal.TODOSink
    14  	value optional.Optional
    15  }
    16  
    17  func NewFindSink() *FindSink {
    18  	sink := &FindSink{}
    19  	sink.SetDerived(sink)
    20  	return sink
    21  }
    22  
    23  // stores only first value
    24  func (sink *FindSink) Accept(value interface{}) {
    25  	if sink.value.IsPresent() {
    26  		return
    27  	}
    28  	sink.value = optional.Of(value)
    29  }
    30  
    31  // return true if found
    32  func (sink *FindSink) CancellationRequested() bool {
    33  	return sink.value.IsPresent()
    34  }
    35  
    36  func (sink *FindSink) Get() optional.Optional {
    37  	return sink.value
    38  }