-
Notifications
You must be signed in to change notification settings - Fork 43
/
FizzBuzz.java
90 lines (71 loc) · 2.8 KB
/
FizzBuzz.java
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
/*
# Licensed Materials - Property of IBM
# Copyright IBM Corp. 2016
*/
package games;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import com.ibm.streamsx.topology.TStream;
import com.ibm.streamsx.topology.Topology;
import com.ibm.streamsx.topology.context.StreamsContextFactory;
import com.ibm.streamsx.topology.function.Function;
import com.ibm.streamsx.topology.streams.BeaconStreams;
/**
* Play
* <a href="https://en.wikipedia.org/wiki/Fizz_buzz">Fizz Buzz</a>.
*
*/
public class FizzBuzz {
/**
* Entry point for a streaming Fizz Buzz!
*/
public static void main(String[] args) throws Exception {
Topology topology = new Topology();
// Declare an infinite stream of Long values
TStream<Long> counting = BeaconStreams.longBeacon(topology);
// Throttle the rate to allow the output to be seen easier
counting = counting.throttle(100, TimeUnit.MILLISECONDS);
// Print the tuples to standard output
playFizzBuzz(counting).print();
// At this point the streaming topology (streaming) is
// declared, but no data is flowing. The topology
// must be submitted to a StreamsContext to be executed.
// Since this is an streaming graph with an endless
// data source it will run for ever
Future<?> runningTopology = StreamsContextFactory.getEmbedded().submit(topology);
// Run for one minute before canceling.
Thread.sleep(TimeUnit.MINUTES.toMillis(1));
runningTopology.cancel(true);
}
/**
* Return a stream that plays Fizz Buzz based
* upon the values in the input stream.
*/
public static TStream<String> playFizzBuzz(TStream<Long> counting) {
/*
* Transform an input stream of longs TStream<Long> to a
* stream of strings TStream<String> that follow
* the Fizz Buzz rules based upon each value in the
* input stream.
*/
TStream<String> shouts = counting.transform(new Function<Long,String>() {
private static final long serialVersionUID = 1L;
@Override
public String apply(Long v) {
// Skip 0, humans count from 1!
if (v == 0)
return null;
StringBuilder sb = new StringBuilder();
if (v % 3 == 0)
sb.append("Fizz");
if (v % 5 == 0)
sb.append("Buzz");
if (sb.length() == 0)
sb.append(Long.toString(v));
else
sb.append("!");
return sb.toString();
}});
return shouts;
}
}