1 package org.lsst.ccs.command;
2
3 import java.util.Collections;
4 import java.util.Iterator;
5 import java.util.LinkedHashMap;
6 import java.util.List;
7 import org.lsst.ccs.command.annotations.Command;
8
9
10
11
12
13 class RoutingDictionary implements Dictionary {
14
15 private LinkedHashMap<String,DictionaryCommand> routeMap = new LinkedHashMap<>();
16
17 void add(String route, Dictionary dict) {
18 routeMap.put(route,new RoutingCommand(route,dict));
19 }
20
21 void remove(String route) {
22 routeMap.remove(route);
23 }
24
25 @Override
26 public boolean containsCommand(BasicCommand tc) {
27 return containsCommand(tc.getCommand(), tc.getArgumentCount());
28 }
29
30 @Override
31 public DictionaryCommand findCommand(BasicCommand tc) {
32 return findCommand(tc.getCommand(), tc.getArgumentCount());
33 }
34
35 @Override
36 public boolean containsCommand(String command, int argumentCount) {
37 return routeMap.containsKey(command);
38 }
39
40 @Override
41 public DictionaryCommand findCommand(String command, int argumentCount) {
42 return routeMap.get(command);
43 }
44
45 @Override
46 public int size() {
47 return routeMap.size();
48 }
49
50 @Override
51 public Iterator<DictionaryCommand> iterator() {
52 return routeMap.values().iterator();
53 }
54
55 private static class RoutingCommand implements DictionaryCommand {
56 private final String route;
57 private final Dictionary dict;
58 private final static DictionaryArgument[] THE_COMMAND = { new RoutingArgument() };
59
60 private RoutingCommand(String route, Dictionary dict) {
61 this.route = route;
62 this.dict = dict;
63 }
64
65 @Override
66 public String[] getAliases() {
67 return NO_ALIASES;
68 }
69
70 @Override
71 public DictionaryArgument[] getArguments() {
72 return THE_COMMAND;
73 }
74
75 @Override
76 public String getCommandName() {
77 return route;
78 }
79
80 @Override
81 public String getDescription() {
82 return "Route command to a different dictionary";
83 }
84
85 @Override
86 public Command.CommandType getType() {
87 return Command.CommandType.QUERY;
88 }
89
90 @Override
91 public boolean isVarArgs() {
92 return true;
93 }
94
95 @Override
96 public int getLevel() {
97 return Command.NORMAL;
98 }
99
100 @Override
101 public boolean matchesCommandOrAlias(String value) {
102 return route.equals(value);
103 }
104 }
105 private static class RoutingArgument implements DictionaryArgument {
106
107 @Override
108 public String getDescription() {
109 return "The command to execute";
110 }
111
112 @Override
113 public String getName() {
114 return "command...";
115 }
116
117 @Override
118 public String getSimpleType() {
119 return "String";
120 }
121
122 @Override
123 public String getType() {
124 return "java.lang.String";
125 }
126
127 @Override
128 public List<String> getValues() {
129 return Collections.<String>emptyList();
130 }
131
132 }
133 }