SelectingReadStream.st
author Claus Gittinger <cg@exept.de>
Wed, 06 Mar 2013 18:13:20 +0100
changeset 2908 505c2f7bb21d
parent 2366 45a774d5420c
child 3091 bb830e805f01
permissions -rw-r--r--
added: #copyright
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
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
    [instance variables:]
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
        hasReadAhead        - because nil is a valid read-element,
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
                            this is used to know if readAhead is valid.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
"
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
!
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
examples
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
"
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
    |s|
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
    s := SelectingReadStream 
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
            on:#(1 2 3 4 5 6 7 8) readStream
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
            selecting:[:each | each even].
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
    s upToEnd  
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
"
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
! !
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
!SelectingReadStream class methodsFor:'instance creation'!
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
on:aStream selecting:aBlock
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
    ^ self basicNew on:aStream selecting:aBlock
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
! !
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
!SelectingReadStream methodsFor:'instance creation'!
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
on:aStream selecting:aBlock
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
    inStream := aStream.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
    selectBlock := aBlock.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
    hasReadAhead := false.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
! !
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
!SelectingReadStream methodsFor:'queries'!
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
atEnd
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
    |el|
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
    hasReadAhead ifTrue:[^ false].
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
    [
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
        inStream atEnd ifTrue:[
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
            ^ true
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
        ].
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
        el := inStream next.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
        (selectBlock value:el)
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
    ] whileFalse.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
    readAhead := el.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
    hasReadAhead := true.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
    ^ false.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
! !
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
!SelectingReadStream methodsFor:'reading'!
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
next
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
    |el|
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
    
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
    hasReadAhead ifTrue:[
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
        hasReadAhead := false.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
        ^ readAhead
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
    ].
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
    [
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
        inStream atEnd ifTrue:[
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
            ^ self pastEndRead
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
        ].
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
        el := inStream next.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
        (selectBlock value:el)
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
    ] whileFalse.
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
    ^ el
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
! !
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
!SelectingReadStream class methodsFor:'documentation'!
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
version_CVS
2908
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
   117
    ^ '$Header: /cvs/stx/stx/libbasic2/SelectingReadStream.st,v 1.3 2013-03-06 17:13:20 cg Exp $'
2362
eb7b174334c2 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
! !
2908
505c2f7bb21d added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2366
diff changeset
   119