SelectingReadStream.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 3271 4a30a0fe6b25
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2908
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
     1
"
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
     2
 COPYRIGHT (c) 2009 by eXept Software AG
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
     3
              All Rights Reserved
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
     4
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
     5
 This software is furnished under a license and may be used
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
     6
 only in accordance with the terms of that license and with the
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
     8
 be provided or otherwise made available to, or used by, any
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
     9
 other person.  No title to or ownership of the software is
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    10
 hereby transferred.
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    11
"
2362
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
Stream subclass:#SelectingReadStream
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
	instanceVariableNames:'selectBlock hasReadAhead readAhead inStream'
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
	classVariableNames:''
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	poolDictionaries:''
2366
45a774d5420c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2362
diff changeset
    18
	category:'Streams-Misc'
2362
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
!
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!SelectingReadStream class methodsFor:'documentation'!
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
2908
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    23
copyright
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    24
"
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    25
 COPYRIGHT (c) 2009 by eXept Software AG
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    26
              All Rights Reserved
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    27
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    28
 This software is furnished under a license and may be used
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    29
 only in accordance with the terms of that license and with the
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    31
 be provided or otherwise made available to, or used by, any
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    32
 other person.  No title to or ownership of the software is
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    33
 hereby transferred.
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    34
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    35
"
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    36
!
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
    37
2362
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
documentation
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
"
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
    A stream which only delivers elements for which a select block returns true
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
    from an underlying stream. 
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
    Needs readAhead for proper atEnd handling.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
3271
4a30a0fe6b25 documentation
Claus Gittinger <cg@exept.de>
parents: 3091
diff changeset
    45
    [Author:]
4a30a0fe6b25 documentation
Claus Gittinger <cg@exept.de>
parents: 3091
diff changeset
    46
        Claus Gittinger
4a30a0fe6b25 documentation
Claus Gittinger <cg@exept.de>
parents: 3091
diff changeset
    47
2362
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
    [instance variables:]
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
        hasReadAhead        - because nil is a valid read-element,
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
                            this is used to know if readAhead is valid.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
"
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
!
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
examples
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
"
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
    |s|
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
    s := SelectingReadStream 
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
            on:#(1 2 3 4 5 6 7 8) readStream
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
            selecting:[:each | each even].
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
    s upToEnd  
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
"
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
! !
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
!SelectingReadStream class methodsFor:'instance creation'!
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
on:aStream selecting:aBlock
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
    ^ self basicNew on:aStream selecting:aBlock
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
! !
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
!SelectingReadStream methodsFor:'instance creation'!
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
on:aStream selecting:aBlock
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
    inStream := aStream.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
    selectBlock := aBlock.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
    hasReadAhead := false.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
! !
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
!SelectingReadStream methodsFor:'queries'!
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
atEnd
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
    |el|
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
    hasReadAhead ifTrue:[^ false].
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
    [
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
        inStream atEnd ifTrue:[
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
            ^ true
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
        ].
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
        el := inStream next.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
        (selectBlock value:el)
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
    ] whileFalse.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
    readAhead := el.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
    hasReadAhead := true.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
    ^ false.
3091
bb830e805f01 class: SelectingReadStream
Claus Gittinger <cg@exept.de>
parents: 2908
diff changeset
    96
!
bb830e805f01 class: SelectingReadStream
Claus Gittinger <cg@exept.de>
parents: 2908
diff changeset
    97
bb830e805f01 class: SelectingReadStream
Claus Gittinger <cg@exept.de>
parents: 2908
diff changeset
    98
contentsSpecies
bb830e805f01 class: SelectingReadStream
Claus Gittinger <cg@exept.de>
parents: 2908
diff changeset
    99
    "return a class of which instances will be returned, when
bb830e805f01 class: SelectingReadStream
Claus Gittinger <cg@exept.de>
parents: 2908
diff changeset
   100
     parts of the collection are asked for. 
bb830e805f01 class: SelectingReadStream
Claus Gittinger <cg@exept.de>
parents: 2908
diff changeset
   101
     (see upTo-kind of methods in Stream)"
bb830e805f01 class: SelectingReadStream
Claus Gittinger <cg@exept.de>
parents: 2908
diff changeset
   102
bb830e805f01 class: SelectingReadStream
Claus Gittinger <cg@exept.de>
parents: 2908
diff changeset
   103
    ^ inStream contentsSpecies.
2362
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
! !
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
!SelectingReadStream methodsFor:'reading'!
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
next
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
    |el|
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
    
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
    hasReadAhead ifTrue:[
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
        hasReadAhead := false.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
        ^ readAhead
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
    ].
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
    [
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
        inStream atEnd ifTrue:[
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
            ^ self pastEndRead
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
        ].
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
        el := inStream next.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
        (selectBlock value:el)
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
    ] whileFalse.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
    ^ el
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
! !
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
!SelectingReadStream class methodsFor:'documentation'!
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
version_CVS
3271
4a30a0fe6b25 documentation
Claus Gittinger <cg@exept.de>
parents: 3091
diff changeset
   128
    ^ '$Header: /cvs/stx/stx/libbasic2/SelectingReadStream.st,v 1.5 2014-04-30 18:20:55 cg Exp $'
2362
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
! !
2908
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
   130