1
2
3
4 package nl.toolforge.karma.core.cmd;
5
6 import java.util.Collection;
7 import java.util.HashMap;
8 import java.util.HashSet;
9 import java.util.Iterator;
10 import java.util.Map;
11 import java.util.Set;
12
13 /***
14 * @author W.M. Oosterom
15 */
16 public class CommandDescriptorMap {
17
18 private Map map = new HashMap();
19
20 public CommandDescriptorMap() {
21
22 }
23
24 /***
25 *
26 * @param newDescriptor
27 */
28 public void add(CommandDescriptor newDescriptor) {
29
30 if (newDescriptor == null) {
31 return;
32 }
33
34
35
36 Set identifiers = new HashSet();
37 if (newDescriptor.getName() != null) {
38 identifiers.add(newDescriptor.getName());
39 }
40 if (newDescriptor.getAliasList() != null) {
41 identifiers.addAll(newDescriptor.getAliasList());
42 }
43 identifiers.remove(null);
44
45
46
47
48
49
50 String alreadyContainsKey = null;
51 for (Iterator i = identifiers.iterator(); i.hasNext();) {
52 String currentAlias = (String) i.next();
53 if (map.containsKey(currentAlias)) {
54 alreadyContainsKey = currentAlias;
55 }
56 }
57 if (alreadyContainsKey != null) {
58
59
60
61 CommandDescriptor existingDescriptor = (CommandDescriptor) map.get(alreadyContainsKey);
62 for (Iterator i = identifiers.iterator(); i.hasNext();) {
63 String currentAlias = (String) i.next();
64 if (!map.containsKey(currentAlias)) {
65 map.put(currentAlias, existingDescriptor);
66 }
67 }
68 } else {
69
70
71 for (Iterator i = identifiers.iterator(); i.hasNext();) {
72 map.put(i.next(), newDescriptor);
73 }
74 }
75 }
76
77 /***
78 * Adds all objects in <code>anotherMap</code> to this <code>CommandDescriptorMap</code>.
79 *
80 * @param anotherMap A <code>CommandDescriptorMap</code> instance.
81 */
82 public void addAll(CommandDescriptorMap anotherMap) {
83
84 for (Iterator i = anotherMap.keySet().iterator(); i.hasNext();) {
85 String key = (String) i.next();
86 map.put(key, anotherMap.get(key));
87 }
88 }
89
90 /***
91 * Returns this <code>CommandDescriptorMap</code>s values.
92 */
93 public Collection values() {
94 return map.values();
95 }
96
97 /***
98 * Returns a Collection view of all keys in this <code>CommandDescriptorMap</code>.
99 *
100 * @return A Collection view of all keys in this <code>CommandDescriptorMap</code>.
101 */
102 public Collection keySet() {
103 return map.keySet();
104 }
105
106 /***
107 * Returns a <code>CommandDescriptor</code> instance identified by the key <code>name</code>.
108 *
109 * @param name A command descriptor name. Acts as key for the <code>CommandDescriptorMap</code>.
110 *
111 * @return
112 */
113 public CommandDescriptor get(String name) {
114 if (name == null) {
115 return null;
116 }
117 return (CommandDescriptor) map.get(name);
118 }
119
120 /***
121 * Returns the number of keys in this <code>CommandDescriptorMap</code>.
122 *
123 * @return The number of keys in this <code>CommandDescriptorMap</code>.
124 */
125 public int size() {
126 return keySet().size();
127
128 }
129 }