summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/TriggeredBlockReader.java
blob: 74076bb179a06bd489a27734ddd4516b42def2f0 (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
package bjc.utils.ioutils.blocks;

import java.io.IOException;

/**
 * A block reader that fires an action before a block is actually read.
 *
 * @author bjculkin
 *
 */
public class TriggeredBlockReader implements BlockReader {
	private final BlockReader source;

	/*
	 * The action to fire.
	 */
	private final Runnable action;

	/**
	 * Create a new triggered reader with the specified source/action.
	 *
	 * @param source
	 *                The block reader to read blocks from.
	 *
	 * @param action
	 *                The action to execute before reading a block.
	 */
	public TriggeredBlockReader(final BlockReader source, final Runnable action) {
		super();
		this.source = source;
		this.action = action;
	}

	@Override
	public boolean hasNextBlock() {
		action.run();

		return source.hasNextBlock();
	}

	@Override
	public Block getBlock() {
		return source.getBlock();
	}

	@Override
	public boolean nextBlock() {
		return source.nextBlock();
	}

	@Override
	public int getBlockCount() {
		return source.getBlockCount();
	}

	@Override
	public void close() throws IOException {
		source.close();
	}

	@Override
	public String toString() {
		return String.format("TriggeredBlockReader [source=%s]", source);
	}
}