|
3 | 3 | import com.google.common.eventbus.EventBus; |
4 | 4 | import com.google.common.eventbus.Subscribe; |
5 | 5 | import com.google.inject.Inject; |
6 | | -import com.google.inject.assistedinject.Assisted; |
| 6 | +import com.google.inject.Singleton; |
7 | 7 | import com.thoughtworks.xstream.annotations.XStreamAlias; |
8 | 8 | import edu.wpi.grip.core.events.SocketChangedEvent; |
9 | 9 |
|
|
20 | 20 | */ |
21 | 21 | @XStreamAlias(value = "grip:Step") |
22 | 22 | public class Step { |
23 | | - private final Logger logger = Logger.getLogger(Step.class.getName()); |
24 | | - private Operation operation; |
25 | | - private InputSocket<?>[] inputSockets; |
26 | | - private OutputSocket<?>[] outputSockets; |
27 | | - private Optional<?> data; |
28 | 23 |
|
29 | | - public interface Factory { |
30 | | - Step create(Operation operation); |
31 | | - } |
| 24 | + private final Logger logger = Logger.getLogger(Step.class.getName()); |
32 | 25 |
|
33 | | - /** |
34 | | - * @param eventBus The Guava {@link EventBus} used by the application. |
35 | | - * @param operation The operation that is performed at this step. |
36 | | - */ |
37 | | - @Inject |
38 | | - Step(EventBus eventBus, @Assisted Operation operation) { |
39 | | - this.operation = operation; |
| 26 | + private final Operation operation; |
| 27 | + private final InputSocket<?>[] inputSockets; |
| 28 | + private final OutputSocket<?>[] outputSockets; |
| 29 | + private final Optional<?> data; |
40 | 30 |
|
41 | | - checkNotNull(eventBus); |
42 | | - checkNotNull(operation); |
| 31 | + @Singleton |
| 32 | + public static class Factory { |
| 33 | + private final EventBus eventBus; |
43 | 34 |
|
44 | | - // Create the list of input and output sockets, and mark this step as their owner. |
45 | | - inputSockets = operation.createInputSockets(eventBus); |
46 | | - for (Socket<?> socket : inputSockets) { |
47 | | - socket.setStep(Optional.of(this)); |
48 | | - eventBus.register(socket); |
| 35 | + @Inject |
| 36 | + public Factory(EventBus eventBus) { |
| 37 | + this.eventBus = eventBus; |
49 | 38 | } |
50 | 39 |
|
51 | | - outputSockets = operation.createOutputSockets(eventBus); |
52 | | - for (Socket<?> socket : outputSockets) { |
53 | | - socket.setStep(Optional.of(this)); |
54 | | - eventBus.register(socket); |
55 | | - } |
| 40 | + public Step create(Operation operation) { |
| 41 | + checkNotNull(operation, "The operation can not be null"); |
| 42 | + // Create the list of input and output sockets, and mark this step as their owner. |
| 43 | + final InputSocket<?>[] inputSockets = operation.createInputSockets(eventBus); |
56 | 44 |
|
57 | | - data = operation.createData(); |
| 45 | + for (Socket<?> socket : inputSockets) { |
| 46 | + eventBus.register(socket); |
| 47 | + } |
| 48 | + |
| 49 | + final OutputSocket<?>[] outputSockets = operation.createOutputSockets(eventBus); |
| 50 | + for (Socket<?> socket : outputSockets) { |
| 51 | + eventBus.register(socket); |
| 52 | + } |
58 | 53 |
|
59 | | - runPerformIfPossible(); |
| 54 | + final Step step = new Step(operation, inputSockets, outputSockets, operation.createData()); |
| 55 | + eventBus.register(step); |
| 56 | + for (Socket<?> socket : inputSockets) { |
| 57 | + socket.setStep(Optional.of(step)); |
| 58 | + } |
| 59 | + for (Socket<?> socket : outputSockets) { |
| 60 | + socket.setStep(Optional.of(step)); |
| 61 | + } |
| 62 | + |
| 63 | + step.runPerformIfPossible(); |
| 64 | + return step; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param operation The operation that is performed at this step. |
| 70 | + * @param inputSockets The input sockets from the operation. |
| 71 | + * @param outputSockets The output sockets provided by the operation. |
| 72 | + * @param data The data provided by the operation. |
| 73 | + */ |
| 74 | + Step(Operation operation, InputSocket<?>[] inputSockets, OutputSocket<?>[] outputSockets, Optional<?> data) { |
| 75 | + this.operation = operation; |
| 76 | + this.inputSockets = inputSockets; |
| 77 | + this.outputSockets = outputSockets; |
| 78 | + this.data = data; |
60 | 79 | } |
61 | 80 |
|
62 | 81 | /** |
|
0 commit comments