|
1 "{ Package: 'stx:libbasic2' }" |
|
2 |
|
3 Stream subclass:#SelectingReadStream |
|
4 instanceVariableNames:'selectBlock hasReadAhead readAhead inStream' |
|
5 classVariableNames:'' |
|
6 poolDictionaries:'' |
|
7 category:'Streams' |
|
8 ! |
|
9 |
|
10 !SelectingReadStream class methodsFor:'documentation'! |
|
11 |
|
12 documentation |
|
13 " |
|
14 A stream which only delivers elements for which a select block returns true |
|
15 from an underlying stream. |
|
16 |
|
17 Needs readAhead for proper atEnd handling. |
|
18 |
|
19 [instance variables:] |
|
20 hasReadAhead - because nil is a valid read-element, |
|
21 this is used to know if readAhead is valid. |
|
22 " |
|
23 ! |
|
24 |
|
25 examples |
|
26 " |
|
27 |s| |
|
28 |
|
29 s := SelectingReadStream |
|
30 on:#(1 2 3 4 5 6 7 8) readStream |
|
31 selecting:[:each | each even]. |
|
32 s upToEnd |
|
33 " |
|
34 ! ! |
|
35 |
|
36 !SelectingReadStream class methodsFor:'instance creation'! |
|
37 |
|
38 on:aStream selecting:aBlock |
|
39 ^ self basicNew on:aStream selecting:aBlock |
|
40 ! ! |
|
41 |
|
42 !SelectingReadStream methodsFor:'instance creation'! |
|
43 |
|
44 on:aStream selecting:aBlock |
|
45 inStream := aStream. |
|
46 selectBlock := aBlock. |
|
47 hasReadAhead := false. |
|
48 ! ! |
|
49 |
|
50 !SelectingReadStream methodsFor:'queries'! |
|
51 |
|
52 atEnd |
|
53 |el| |
|
54 |
|
55 hasReadAhead ifTrue:[^ false]. |
|
56 |
|
57 [ |
|
58 inStream atEnd ifTrue:[ |
|
59 ^ true |
|
60 ]. |
|
61 el := inStream next. |
|
62 (selectBlock value:el) |
|
63 ] whileFalse. |
|
64 readAhead := el. |
|
65 hasReadAhead := true. |
|
66 ^ false. |
|
67 ! ! |
|
68 |
|
69 !SelectingReadStream methodsFor:'reading'! |
|
70 |
|
71 next |
|
72 |el| |
|
73 |
|
74 hasReadAhead ifTrue:[ |
|
75 hasReadAhead := false. |
|
76 ^ readAhead |
|
77 ]. |
|
78 [ |
|
79 inStream atEnd ifTrue:[ |
|
80 ^ self pastEndRead |
|
81 ]. |
|
82 el := inStream next. |
|
83 (selectBlock value:el) |
|
84 ] whileFalse. |
|
85 ^ el |
|
86 ! ! |
|
87 |
|
88 !SelectingReadStream class methodsFor:'documentation'! |
|
89 |
|
90 version_CVS |
|
91 ^ '$Header: /cvs/stx/stx/libbasic2/SelectingReadStream.st,v 1.1 2009-12-05 11:15:39 cg Exp $' |
|
92 ! ! |