main
1using System.Linq;
2using mars.rover.common;
3
4namespace mars.rover.presentation.infrastructure
5{
6 public class SynchronousCommandPump : EventProcessor
7 {
8 readonly Registry<ParameterizedCommand<string>> commands;
9 readonly CommandProcessor processor;
10 readonly CommandFactory factory;
11
12 public SynchronousCommandPump(Registry<ParameterizedCommand<string>> commands, CommandProcessor processor,
13 CommandFactory factory)
14 {
15 this.commands = commands;
16 this.processor = processor;
17 this.factory = factory;
18 }
19
20 public virtual void process<Command, Input>(Input input) where Command : ParameterizedCommand<Input>
21 {
22 processor.add(factory.create_for(() => get<Command, Input>().run_against(input)));
23 }
24
25 public virtual void run()
26 {
27 processor.run();
28 }
29
30 ParameterizedCommand<Input> get<Command, Input>()
31 {
32 return new AdaptedCommand<Input>(commands.First(y => y is Command));
33 }
34
35 class AdaptedCommand<T> : ParameterizedCommand<T>
36 {
37 readonly ParameterizedCommand<string> command;
38
39 public AdaptedCommand(ParameterizedCommand<string> command)
40 {
41 this.command = command;
42 }
43
44 public void run_against(T item)
45 {
46 command.run_against(item as string);
47 }
48 }
49 }
50}