-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathGenerator.java
More file actions
49 lines (40 loc) · 1.37 KB
/
Generator.java
File metadata and controls
49 lines (40 loc) · 1.37 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
package io.herrmann.generator;
import java.util.Iterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* Implementation of {@link GeneratorFunc} as an abstract class. This class
* mainly exists for backwards compatibility, but it can also cut down some of
* the boilerplate when using an anonymous inner class instead of a lambda.
* However, unlike a {@link GeneratorFunc}, this class is not stateless, and
* cannot be used concurrently.
*/
public abstract class Generator<T> implements GeneratorFunc<T> {
private GeneratorIterator<T> iter;
@Override
public void run(GeneratorIterator<T> gen) throws InterruptedException {
run();
}
protected abstract void run() throws InterruptedException;
protected void yield(T element) throws InterruptedException {
iter.yield(element);
}
@Override
public Iterator<T> iterator() {
iter = new GeneratorIterator<>(this);
return iter;
}
/**
* Creates a {@link Stream} from a {@link GeneratorFunc}. For cases where
* the generator isn't a lambda passed directly, the instance method {@link
* #stream()} is generally more concise.
*
* @param g The generator
* @return An ordered, sequential (non-parallel) stream of elements yielded
* by the generator
* @see #stream()
*/
public static <T> Stream<T> stream(GeneratorFunc<T> g) {
return StreamSupport.stream(g.spliterator(), false);
}
}