blob: bab463e947fd950db3e1c0b1f0487e65223da534 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
package bjc.utils.ioutils.blocks;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;
/**
* A block reader that supports applying a flatmap operation to blocks.
*
* The use-case in mind for this was tokenizing blocks.
*
* @author Benjamin Culkin
*/
public class FlatMappedBlockReader implements BlockReader {
/*
* The source reader.
*/
private BlockReader reader;
/*
* The current block, and any blocks pending from the last source block.
*/
private Iterator<Block> pending;
private Block current;
/*
* The operator to open blocks with.
*/
private Function<Block, List<Block>> transform;
/*
* The current block number.
*/
private int blockNo;
/**
* Create a new flat-mapping block reader.
*
* @param source
* The source to read blocks from
* @param trans
* The transform to use.
*/
public FlatMappedBlockReader(BlockReader source, Function<Block, List<Block>> trans) {
reader = source;
transform = trans;
blockNo = 0;
}
@Override
public boolean hasNextBlock() {
return pending.hasNext() || reader.hasNextBlock();
}
@Override
public Block getBlock() {
return current;
}
@Override
public boolean nextBlock() {
/*
* Attempt to get a new pending list if the one we have isn't
* valid.
*/
while(pending == null || !pending.hasNext()) {
if(!reader.hasNext()) return false;
pending = transform.apply(reader.next()).iterator();
}
/*
* Advance the iterator.
*/
current = pending.next();
blockNo += 1;
return true;
}
@Override
public int getBlockCount() {
return blockNo;
}
@Override
public void close() throws IOException {
reader.close();
}
}
|