blob: a7f62909d68d063beef6b36de374983f4d9028e5 [file] [log] [blame]
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00001package aQute.bnd.osgi;
Stuart McCullochbb014372012-06-07 21:57:32 +00002
3import java.util.*;
4
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00005import aQute.bnd.header.*;
6import aQute.bnd.osgi.Descriptors.PackageRef;
Stuart McCullochbb014372012-06-07 21:57:32 +00007
Stuart McCulloch2286f232012-06-15 13:27:53 +00008public class Packages implements Map<PackageRef,Attrs> {
9 private LinkedHashMap<PackageRef,Attrs> map;
10 static Map<PackageRef,Attrs> EMPTY = Collections.emptyMap();
Stuart McCullochbb014372012-06-07 21:57:32 +000011
12 public Packages(Packages other) {
13 if (other.map != null) {
Stuart McCulloch2286f232012-06-15 13:27:53 +000014 map = new LinkedHashMap<Descriptors.PackageRef,Attrs>(other.map);
Stuart McCullochbb014372012-06-07 21:57:32 +000015 }
16 }
17
Stuart McCulloch2286f232012-06-15 13:27:53 +000018 public Packages() {}
Stuart McCullochbb014372012-06-07 21:57:32 +000019
20 public void clear() {
Stuart McCulloch2286f232012-06-15 13:27:53 +000021 if (map != null)
Stuart McCullochbb014372012-06-07 21:57:32 +000022 map.clear();
23 }
24
25 public boolean containsKey(PackageRef name) {
26 if (map == null)
27 return false;
28
29 return map.containsKey(name);
30 }
31
Stuart McCulloch2286f232012-06-15 13:27:53 +000032 @Deprecated
33 public boolean containsKey(Object name) {
Stuart McCullochbb014372012-06-07 21:57:32 +000034 assert name instanceof PackageRef;
35 if (map == null)
36 return false;
37
Stuart McCullochd4826102012-06-26 16:34:24 +000038 return map.containsKey(name);
Stuart McCullochbb014372012-06-07 21:57:32 +000039 }
40
41 public boolean containsValue(Attrs value) {
42 if (map == null)
43 return false;
44
45 return map.containsValue(value);
46 }
47
Stuart McCulloch2286f232012-06-15 13:27:53 +000048 @Deprecated
49 public boolean containsValue(Object value) {
Stuart McCullochbb014372012-06-07 21:57:32 +000050 assert value instanceof Attrs;
51 if (map == null)
52 return false;
53
Stuart McCullochd4826102012-06-26 16:34:24 +000054 return map.containsValue(value);
Stuart McCullochbb014372012-06-07 21:57:32 +000055 }
56
Stuart McCulloch2286f232012-06-15 13:27:53 +000057 public Set<java.util.Map.Entry<PackageRef,Attrs>> entrySet() {
Stuart McCullochbb014372012-06-07 21:57:32 +000058 if (map == null)
59 return EMPTY.entrySet();
60
61 return map.entrySet();
62 }
63
Stuart McCulloch2286f232012-06-15 13:27:53 +000064 @Deprecated
65 public Attrs get(Object key) {
Stuart McCullochbb014372012-06-07 21:57:32 +000066 assert key instanceof PackageRef;
67 if (map == null)
68 return null;
69
Stuart McCullochd4826102012-06-26 16:34:24 +000070 return map.get(key);
Stuart McCullochbb014372012-06-07 21:57:32 +000071 }
72
73 public Attrs get(PackageRef key) {
74 if (map == null)
75 return null;
76
77 return map.get(key);
78 }
79
80 public boolean isEmpty() {
81 return map == null || map.isEmpty();
82 }
83
84 public Set<PackageRef> keySet() {
85 if (map == null)
86 return EMPTY.keySet();
87
88 return map.keySet();
89 }
90
91 public Attrs put(PackageRef ref) {
92 Attrs attrs = get(ref);
93 if (attrs != null)
94 return attrs;
95
96 attrs = new Attrs();
97 put(ref, attrs);
98 return attrs;
99 }
100
101 public Attrs put(PackageRef key, Attrs value) {
102 if (map == null)
Stuart McCulloch2286f232012-06-15 13:27:53 +0000103 map = new LinkedHashMap<PackageRef,Attrs>();
Stuart McCullochbb014372012-06-07 21:57:32 +0000104
105 return map.put(key, value);
106 }
107
Stuart McCulloch2286f232012-06-15 13:27:53 +0000108 public void putAll(Map< ? extends PackageRef, ? extends Attrs> map) {
Stuart McCullochd4826102012-06-26 16:34:24 +0000109 if (this.map == null) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000110 if (map.isEmpty())
111 return;
Stuart McCullochd4826102012-06-26 16:34:24 +0000112 this.map = new LinkedHashMap<PackageRef,Attrs>();
113 }
Stuart McCullochbb014372012-06-07 21:57:32 +0000114 this.map.putAll(map);
115 }
116
117 public void putAllIfAbsent(Map<PackageRef, ? extends Attrs> map) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000118 for (Map.Entry<PackageRef, ? extends Attrs> entry : map.entrySet()) {
119 if (!containsKey(entry.getKey()))
Stuart McCullochbb014372012-06-07 21:57:32 +0000120 put(entry.getKey(), entry.getValue());
121 }
122 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000123
124 @Deprecated
125 public Attrs remove(Object var0) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000126 assert var0 instanceof PackageRef;
127 if (map == null)
128 return null;
129
Stuart McCullochd4826102012-06-26 16:34:24 +0000130 return map.remove(var0);
Stuart McCullochbb014372012-06-07 21:57:32 +0000131 }
132
133 public Attrs remove(PackageRef var0) {
134 if (map == null)
135 return null;
136 return map.remove(var0);
137 }
138
139 public int size() {
140 if (map == null)
141 return 0;
142 return map.size();
143 }
144
145 public Collection<Attrs> values() {
146 if (map == null)
147 return EMPTY.values();
148
149 return map.values();
150 }
151
152 public Attrs getByFQN(String s) {
153 if (map == null)
154 return null;
155
Stuart McCulloch2286f232012-06-15 13:27:53 +0000156 for (Map.Entry<PackageRef,Attrs> pr : map.entrySet()) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000157 if (pr.getKey().getFQN().equals(s))
158 return pr.getValue();
159 }
160 return null;
161 }
162
163 public Attrs getByBinaryName(String s) {
164 if (map == null)
165 return null;
166
Stuart McCulloch2286f232012-06-15 13:27:53 +0000167 for (Map.Entry<PackageRef,Attrs> pr : map.entrySet()) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000168 if (pr.getKey().getBinary().equals(s))
169 pr.getValue();
170 }
171 return null;
172 }
173
174 public boolean containsFQN(String s) {
175 return getByFQN(s) != null;
176 }
177
178 public boolean containsBinaryName(String s) {
179 return getByFQN(s) != null;
180 }
181
Stuart McCulloch55d4dfe2012-08-07 10:57:21 +0000182 @Override
Stuart McCullochbb014372012-06-07 21:57:32 +0000183 public String toString() {
184 StringBuilder sb = new StringBuilder();
185 append(sb);
186 return sb.toString();
187 }
188
189 public void append(StringBuilder sb) {
190 String del = "";
Stuart McCulloch2286f232012-06-15 13:27:53 +0000191 for (Map.Entry<PackageRef,Attrs> s : entrySet()) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000192 sb.append(del);
193 sb.append(s.getKey());
194 if (!s.getValue().isEmpty()) {
195 sb.append(';');
196 s.getValue().append(sb);
197 }
198 del = ",";
199 }
200 }
201
202 public void merge(PackageRef ref, boolean unique, Attrs... attrs) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000203 if (unique) {
204 while (containsKey(ref))
Stuart McCullochbb014372012-06-07 21:57:32 +0000205 ref = ref.getDuplicate();
206 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000207
Stuart McCullochbb014372012-06-07 21:57:32 +0000208 Attrs org = put(ref);
209 for (Attrs a : attrs) {
210 if (a != null)
211 org.putAll(a);
212 }
213 }
214
215 public Attrs get(PackageRef packageRef, Attrs deflt) {
216 Attrs mine = get(packageRef);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000217 if (mine != null)
Stuart McCullochbb014372012-06-07 21:57:32 +0000218 return mine;
Stuart McCulloch2286f232012-06-15 13:27:53 +0000219
Stuart McCullochbb014372012-06-07 21:57:32 +0000220 return deflt;
221 }
222
Stuart McCulloch55d4dfe2012-08-07 10:57:21 +0000223 @Override
Stuart McCullochbb014372012-06-07 21:57:32 +0000224 @Deprecated
225 public boolean equals(Object other) {
226 return super.equals(other);
227 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000228
Stuart McCulloch55d4dfe2012-08-07 10:57:21 +0000229 @Override
Stuart McCullochbb014372012-06-07 21:57:32 +0000230 @Deprecated
231 public int hashCode() {
232 return super.hashCode();
233 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000234
Stuart McCullochbb014372012-06-07 21:57:32 +0000235}