summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/ioutils/BlockReader.java
blob: fed74fe224effa03104a9ea9a68a01ce67235df1 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package bjc.utils.ioutils;

import java.io.LineNumberReader;
import java.io.Reader;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.function.Consumer;
import java.util.regex.Pattern;

/**
 * Implements reading numbered blocks from a source.
 * 
 * A block is a series of characters, separated by some regular expression.
 * 
 * NOTE: The EOF marker is always treated as a delimiter. You are expected to
 * handle blocks that may be shorter than you expect.
 * 
 * @author EVE
 *
 */
public class BlockReader implements AutoCloseable, Iterator<Block> {
	/*
	 * I/O source for blocks.
	 */
	private LineNumberReader	lnReader;
	private Scanner			blockReader;

	/*
	 * The current block.
	 */
	private Block	currBlock;
	private int	blockNo;

	/**
	 * Create a new block reader.
	 * 
	 * @param blockDelim
	 *                The pattern that separates blocks. Note that the end
	 *                of file is always considered to end a block.
	 * 
	 * @param source
	 *                The source to read blocks from.
	 */
	public BlockReader(String blockDelim, Reader source) {
		lnReader = new LineNumberReader(source);

		blockReader = new Scanner(lnReader);

		String pattern = String.format("(?:%s)|\\Z", blockDelim);
		Pattern pt = Pattern.compile(pattern, Pattern.MULTILINE);

		blockReader.useDelimiter(pt);
	}

	/**
	 * Check if this reader has an available block.
	 * 
	 * @return Whether or not another block is available.
	 */
	public boolean hasNextBlock() {
		return blockReader.hasNext();
	}

	/**
	 * Get the current block.
	 * 
	 * @return The current block, or null if there is no current block.
	 */
	public Block getBlock() {
		return currBlock;
	}

	/**
	 * Move to the next block.
	 * 
	 * @return Whether or not the next block was successfully read.
	 */
	public boolean nextBlock() {
		try {
			int blockStartLine = lnReader.getLineNumber();
			String blockContents = blockReader.next();
			int blockEndLine = lnReader.getLineNumber();
			blockNo += 1;

			currBlock = new Block(blockNo, blockContents, blockStartLine, blockEndLine);

			return true;
		} catch (NoSuchElementException nseex) {
			return false;
		}
	}

	/**
	 * Execute an action for each remaining block.
	 * 
	 * @param action
	 *                The action to execute for each block
	 */
	public void forEachBlock(Consumer<Block> action) {
		while (hasNextBlock()) {
			nextBlock();

			action.accept(currBlock);
		}
	}

	/**
	 * Retrieve the number of blocks that have been read so far.
	 * 
	 * @return The number of blocks read so far.
	 */
	public int getBlockCount() {
		return blockNo;
	}

	@Override
	public void close() throws Exception {
		blockReader.close();

		lnReader.close();
	}

	/**
	 * Set the delimiter used to separate blocks.
	 * 
	 * @param delim
	 *                The delimiter used to separate blocks.
	 */
	public void setDelimiter(String delim) {
		blockReader.useDelimiter(delim);
	}

	/*
	 * Iterator implementation.
	 */

	@Override
	public boolean hasNext() {
		return blockReader.hasNext();
	}

	@Override
	public Block next() {
		nextBlock();

		return getBlock();
	}
}