summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/ioutils/blocks/BlockReaders.java
blob: 2342c227512e321b911e2b012a160c8f291935fc (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
package bjc.utils.ioutils.blocks;

import java.io.*;
import java.util.Iterator;

/** Utility methods for constructing instances of {@link BlockReader}
 *
 * @author bjculkin */
public class BlockReaders {
	/** Create a new simple block reader that works off a regex.
	 *
	 * @param blockDelim The regex that separates blocks.
	 * @param source The reader to get blocks from.
	 *
	 * @return A configured simple reader. */
	public static SimpleBlockReader simple(
			final String blockDelim, final Reader source) 
	{
		return new SimpleBlockReader(blockDelim, source);
	}

	/** Create a new pushback block reader.
	 *
	 * @param src The block reader to read blocks from.
	 *
	 * @return A configured pushback reader. */
	public static PushbackBlockReader pushback(final BlockReader src) {
		return new PushbackBlockReader(src);
	}

	/** Create a new triggered block reader.
	 *
	 * @param source The block reader to read blocks from.
	 * @param action The action to execute before reading a block.
	 *
	 * @return A configured triggered block reader. */
	public static BlockReader trigger(
			final BlockReader source, final Runnable action)
	{
		return new TriggeredBlockReader(source, action);
	}

	/** Create a new layered block reader.
	 *
	 * @param primary The first source to read blocks from.
	 * @param secondary The second source to read blocks from.
	 *
	 * @return A configured layered block reader. */
	public static BlockReader layered(
			final BlockReader primary, final BlockReader secondary)
	{
		return new LayeredBlockReader(primary, secondary);
	}

	/** Create a new serial block reader.
	 *
	 * @param readers The readers to pull from, in the order to pull from them.
	 *
	 * @return A configured serial block reader. */
	public static BlockReader serial(final BlockReader... readers) {
		return new SerialBlockReader(readers);
	}
	
	/**
	 * Create a block reader from an iterator of blocks.
	 * 
	 * @param iter The iterator of blocks.
	 * 
	 * @return A block reader which returns blocks from the given iterator.
	 */
	public static BlockReader fromIterator(final Iterator<Block> iter) {
		return new IteratorBlockReader(iter);
	}
}