%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
nl.toolforge.karma.core.cmd.CommandDescriptorMap |
|
|
1 | /* |
|
2 | * Created on Oct 17, 2004 |
|
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 | 78 | private Map map = new HashMap(); |
19 | ||
20 | 78 | public CommandDescriptorMap() { |
21 | // Empty |
|
22 | 78 | } |
23 | ||
24 | /** |
|
25 | * |
|
26 | 95 | * @param newDescriptor |
27 | */ |
|
28 | public void add(CommandDescriptor newDescriptor) { |
|
29 | ||
30 | 35 | if (newDescriptor == null) { |
31 | 0 | return; |
32 | 95 | } |
33 | 95 | |
34 | 95 | // Create a set of identifiers for this command descriptor |
35 | // |
|
36 | 130 | Set identifiers = new HashSet(); |
37 | 130 | if (newDescriptor.getName() != null) { |
38 | 35 | identifiers.add(newDescriptor.getName()); |
39 | 95 | } |
40 | 35 | if (newDescriptor.getAliasList() != null) { |
41 | 35 | identifiers.addAll(newDescriptor.getAliasList()); |
42 | } |
|
43 | 35 | identifiers.remove(null); |
44 | ||
45 | // If map already contains a key, that is also found in identifiers |
|
46 | 95 | // than do not add the current command descriptor, but add the current |
47 | 95 | // aliases that were not yet registered to the commanddescriptor that |
48 | 304 | // was already put in map before |
49 | 304 | // |
50 | 111 | String alreadyContainsKey = null; |
51 | 35 | for (Iterator i = identifiers.iterator(); i.hasNext();) { |
52 | 112 | String currentAlias = (String) i.next(); |
53 | 207 | if (map.containsKey(currentAlias)) { |
54 | 28 | alreadyContainsKey = currentAlias; |
55 | } |
|
56 | } |
|
57 | 54 | if (alreadyContainsKey != null) { |
58 | 19 | // map already contains this command descriptor; add |
59 | 76 | // missing aliases |
60 | 76 | // |
61 | 7 | CommandDescriptor existingDescriptor = (CommandDescriptor) map.get(alreadyContainsKey); |
62 | 7 | for (Iterator i = identifiers.iterator(); i.hasNext();) { |
63 | 28 | String currentAlias = (String) i.next(); |
64 | 28 | if (!map.containsKey(currentAlias)) { |
65 | 0 | map.put(currentAlias, existingDescriptor); |
66 | } |
|
67 | 76 | } |
68 | 228 | } else { |
69 | // map did not yet contains this command descriptor |
|
70 | // |
|
71 | 123 | for (Iterator i = identifiers.iterator(); i.hasNext();) { |
72 | 84 | map.put(i.next(), newDescriptor); |
73 | } |
|
74 | } |
|
75 | 35 | } |
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 | 0 | for (Iterator i = anotherMap.keySet().iterator(); i.hasNext();) { |
85 | 0 | String key = (String) i.next(); |
86 | 0 | map.put(key, anotherMap.get(key)); |
87 | } |
|
88 | 0 | } |
89 | ||
90 | /** |
|
91 | * Returns this <code>CommandDescriptorMap</code>s values. |
|
92 | */ |
|
93 | public Collection values() { |
|
94 | 0 | return map.values(); |
95 | } |
|
96 | ||
97 | /** |
|
98 | * Returns a Collection view of all keys in this <code>CommandDescriptorMap</code>. |
|
99 | 38 | * |
100 | * @return A Collection view of all keys in this <code>CommandDescriptorMap</code>. |
|
101 | */ |
|
102 | public Collection keySet() { |
|
103 | 14 | 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 | 76 | * |
111 | * @return |
|
112 | */ |
|
113 | 76 | public CommandDescriptor get(String name) { |
114 | 28 | if (name == null) { |
115 | 0 | return null; |
116 | } |
|
117 | 28 | return (CommandDescriptor) map.get(name); |
118 | } |
|
119 | ||
120 | /** |
|
121 | * Returns the number of keys in this <code>CommandDescriptorMap</code>. |
|
122 | 38 | * |
123 | * @return The number of keys in this <code>CommandDescriptorMap</code>. |
|
124 | */ |
|
125 | public int size() { |
|
126 | 14 | return keySet().size(); |
127 | ||
128 | } |
|
129 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |