This repository was archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueProducer.java
More file actions
76 lines (63 loc) · 2.21 KB
/
QueueProducer.java
File metadata and controls
76 lines (63 loc) · 2.21 KB
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
package picard.sam.markduplicates.util;
/*
* Created by GoodforGod
* Date: 24.02.2017
* Time: 15:02
*/
import htsjdk.samtools.util.PeekableIterator;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Function;
/*
* DEFAULT COMMENT
*/
public class QueueProducer<Iterable, Produced>
{
private final int JOB_CAPACITY;
private final int JOB_CAPACITY_DAFAULT = 8;
private final PeekableIterator<Iterable> iterator;
private final Function<PeekableIterator<Iterable>, Produced> handler;
private final BlockingQueue<Produced> queue;
public QueueProducer(final PeekableIterator<Iterable> iterator,
final Function<PeekableIterator<Iterable>, Produced> handler,
int capacity) {
this.iterator = iterator;
Objects.requireNonNull(handler, "Handler function must not be null!");
this.handler = handler;
this.JOB_CAPACITY = capacity;
this.queue = new LinkedBlockingQueue<>(JOB_CAPACITY);
start();
}
public QueueProducer(final PeekableIterator<Iterable> iterator,
final Function<PeekableIterator<Iterable>, Produced> handler) {
this.iterator = iterator;
Objects.requireNonNull(handler, "Handler function must not be null!");
this.handler = handler;
this.JOB_CAPACITY = JOB_CAPACITY_DAFAULT;
this.queue = new LinkedBlockingQueue<>(JOB_CAPACITY);
start();
}
private void start() {
new Thread(() -> {
while (iterator.hasNext()) {
try { queue.put(handler.apply(iterator)); }
catch (InterruptedException e) { e.printStackTrace(); }
}
}).start();
}
public boolean hasNext() {
return queue.peek() != null || iterator.hasNext();
}
public Produced peek() {
return queue.peek();
}
public Produced next() {
try { return queue.take(); }
catch (InterruptedException e) { e.printStackTrace(); }
return null;
}
public void finish() {
iterator.close();
}
}