CollectingReadStream.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 5247 abb5d984aafa
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:

"
 COPYRIGHT (c) 2009 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic2' }"

"{ NameSpace: Smalltalk }"

PeekableStream subclass:#CollectingReadStream
	instanceVariableNames:'collectBlock inStream'
	classVariableNames:''
	poolDictionaries:''
	category:'Streams-Misc'
!

!CollectingReadStream class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2009 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    A stream which evaluates collectBlock for every element
    read from an underlying stream, providing the result from
    the evaluation as read-elements.
    Useful to process a readStream, for tracing or diverting to another
    processing stage.

    [Author:]
        Claus Gittinger
"
!

examples
"
                                                            [exBegin]
    |s|

    s := CollectingReadStream 
            on:#(1 2 3 4 5 6 7 8) readStream
            collecting:[:each | each squared].
    s upToEnd  
                                                            [exEnd]

                                                            [exBegin]
    |s|

    s := (#(1 2 3 4 5 6 7 8) readStream 
            selecting:[:n | n odd])
                collecting:[:n | n squared].
    s upToEnd    
                                                            [exEnd]
"
! !

!CollectingReadStream class methodsFor:'instance creation'!

diverting:aStream to:anotherStream
    ^ self on:aStream collecting:[:el | anotherStream nextPut:el. el].

    "
     |s|

     s := CollectingReadStream 
            diverting:#(1 2 3 4 5 6 7 8) readStream
            to:Transcript.
     s upToEnd  
    "
!

on:aStream collecting:aBlock
    ^ self basicNew on:aStream collecting:aBlock

    "
     |s|

     s := CollectingReadStream 
            on:#(1 2 3 4 5 6 7 8) readStream
            collecting:[:each | each squared].
     s upToEnd  
    "
! !

!CollectingReadStream methodsFor:'accessing'!

readStream
    ^ self

    "Created: / 26-10-2011 / 17:25:43 / cg"
! !

!CollectingReadStream methodsFor:'instance creation'!

on:aStream collecting:aBlock
    inStream := aStream.
    collectBlock := aBlock.
! !

!CollectingReadStream methodsFor:'obsolete positioning'!

position0Based
    <resource: #obsolete>
    ^ inStream position

    "Created: / 26-10-2011 / 17:23:56 / cg"
!

position1Based
    <resource: #obsolete>
    ^ inStream position + 1

    "Created: / 26-10-2011 / 17:22:42 / cg"
! !

!CollectingReadStream methodsFor:'queries'!

atEnd
    ^ inStream atEnd
!

contents
    ^ nil.

    "Created: / 26-05-2019 / 00:52:54 / Claus Gittinger"
!

contentsSpecies
    "return a class of which instances will be returned, when
     parts of the collection are asked for. 
     (see upTo-kind of methods in Stream)"

    ^ inStream contentsSpecies.
! !

!CollectingReadStream methodsFor:'reading'!

next
    inStream atEnd ifTrue:[
        ^ self pastEndRead
    ].
    ^ collectBlock value:(inStream next)
!

nextOrNil
    |ch|

    ch := inStream nextOrNil.
    ch notNil ifTrue:[
        collectBlock value:ch
    ].
    ^ ch

    "Created: / 26-10-2011 / 17:29:01 / cg"
!

nextPeek
    self next.
    ^ self peek

    "Created: / 26-10-2011 / 17:25:16 / cg"
!

peek
    ^ inStream peek

    "Created: / 26-10-2011 / 17:24:59 / cg"
!

peekOrNil
    ^ inStream peekOrNil

    "Created: / 26-10-2011 / 17:25:32 / cg"
! !

!CollectingReadStream methodsFor:'stream protocol'!

position
    ^ inStream position

    "Created: / 26-10-2011 / 17:25:08 / cg"
!

position:newPos
    inStream position:newPos

    "Created: / 26-05-2019 / 00:57:19 / Claus Gittinger"
! !

!CollectingReadStream class methodsFor:'documentation'!

version_CVS
    ^ '$Header$'
! !