blob: 64e4a4ee11a95c8e9a29a7dbd9bacc85bdaaa55e [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.lib.osgi;
2
3import java.util.*;
4
5import aQute.lib.osgi.Descriptors.PackageRef;
6import aQute.libg.header.*;
7
Stuart McCulloch4482c702012-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 McCullochf3173222012-06-07 21:57:32 +000011
12 public Packages(Packages other) {
13 if (other.map != null) {
Stuart McCulloch4482c702012-06-15 13:27:53 +000014 map = new LinkedHashMap<Descriptors.PackageRef,Attrs>(other.map);
Stuart McCullochf3173222012-06-07 21:57:32 +000015 }
16 }
17
Stuart McCulloch4482c702012-06-15 13:27:53 +000018 public Packages() {}
Stuart McCullochf3173222012-06-07 21:57:32 +000019
20 public void clear() {
Stuart McCulloch4482c702012-06-15 13:27:53 +000021 if (map != null)
Stuart McCullochf3173222012-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 McCulloch4482c702012-06-15 13:27:53 +000032 @Deprecated
33 public boolean containsKey(Object name) {
Stuart McCullochf3173222012-06-07 21:57:32 +000034 assert name instanceof PackageRef;
35 if (map == null)
36 return false;
37
Stuart McCulloch669423b2012-06-26 16:34:24 +000038 return map.containsKey(name);
Stuart McCullochf3173222012-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 McCulloch4482c702012-06-15 13:27:53 +000048 @Deprecated
49 public boolean containsValue(Object value) {
Stuart McCullochf3173222012-06-07 21:57:32 +000050 assert value instanceof Attrs;
51 if (map == null)
52 return false;
53
Stuart McCulloch669423b2012-06-26 16:34:24 +000054 return map.containsValue(value);
Stuart McCullochf3173222012-06-07 21:57:32 +000055 }
56
Stuart McCulloch4482c702012-06-15 13:27:53 +000057 public Set<java.util.Map.Entry<PackageRef,Attrs>> entrySet() {
Stuart McCullochf3173222012-06-07 21:57:32 +000058 if (map == null)
59 return EMPTY.entrySet();
60
61 return map.entrySet();
62 }
63
Stuart McCulloch4482c702012-06-15 13:27:53 +000064 @Deprecated
65 public Attrs get(Object key) {
Stuart McCullochf3173222012-06-07 21:57:32 +000066 assert key instanceof PackageRef;
67 if (map == null)
68 return null;
69
Stuart McCulloch669423b2012-06-26 16:34:24 +000070 return map.get(key);
Stuart McCullochf3173222012-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 McCulloch4482c702012-06-15 13:27:53 +0000103 map = new LinkedHashMap<PackageRef,Attrs>();
Stuart McCullochf3173222012-06-07 21:57:32 +0000104
105 return map.put(key, value);
106 }
107
Stuart McCulloch4482c702012-06-15 13:27:53 +0000108 public void putAll(Map< ? extends PackageRef, ? extends Attrs> map) {
Stuart McCulloch669423b2012-06-26 16:34:24 +0000109 if (this.map == null) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000110 if (map.isEmpty())
111 return;
Stuart McCulloch669423b2012-06-26 16:34:24 +0000112 this.map = new LinkedHashMap<PackageRef,Attrs>();
113 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000114 this.map.putAll(map);
115 }
116
117 public void putAllIfAbsent(Map<PackageRef, ? extends Attrs> map) {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000118 for (Map.Entry<PackageRef, ? extends Attrs> entry : map.entrySet()) {
119 if (!containsKey(entry.getKey()))
Stuart McCullochf3173222012-06-07 21:57:32 +0000120 put(entry.getKey(), entry.getValue());
121 }
122 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000123
124 @Deprecated
125 public Attrs remove(Object var0) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000126 assert var0 instanceof PackageRef;
127 if (map == null)
128 return null;
129
Stuart McCulloch669423b2012-06-26 16:34:24 +0000130 return map.remove(var0);
Stuart McCullochf3173222012-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 McCulloch4482c702012-06-15 13:27:53 +0000156 for (Map.Entry<PackageRef,Attrs> pr : map.entrySet()) {
Stuart McCullochf3173222012-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 McCulloch4482c702012-06-15 13:27:53 +0000167 for (Map.Entry<PackageRef,Attrs> pr : map.entrySet()) {
Stuart McCullochf3173222012-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
182 public String toString() {
183 StringBuilder sb = new StringBuilder();
184 append(sb);
185 return sb.toString();
186 }
187
188 public void append(StringBuilder sb) {
189 String del = "";
Stuart McCulloch4482c702012-06-15 13:27:53 +0000190 for (Map.Entry<PackageRef,Attrs> s : entrySet()) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000191 sb.append(del);
192 sb.append(s.getKey());
193 if (!s.getValue().isEmpty()) {
194 sb.append(';');
195 s.getValue().append(sb);
196 }
197 del = ",";
198 }
199 }
200
201 public void merge(PackageRef ref, boolean unique, Attrs... attrs) {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000202 if (unique) {
203 while (containsKey(ref))
Stuart McCullochf3173222012-06-07 21:57:32 +0000204 ref = ref.getDuplicate();
205 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000206
Stuart McCullochf3173222012-06-07 21:57:32 +0000207 Attrs org = put(ref);
208 for (Attrs a : attrs) {
209 if (a != null)
210 org.putAll(a);
211 }
212 }
213
214 public Attrs get(PackageRef packageRef, Attrs deflt) {
215 Attrs mine = get(packageRef);
Stuart McCulloch4482c702012-06-15 13:27:53 +0000216 if (mine != null)
Stuart McCullochf3173222012-06-07 21:57:32 +0000217 return mine;
Stuart McCulloch4482c702012-06-15 13:27:53 +0000218
Stuart McCullochf3173222012-06-07 21:57:32 +0000219 return deflt;
220 }
221
Stuart McCullochf3173222012-06-07 21:57:32 +0000222 @Deprecated
223 public boolean equals(Object other) {
224 return super.equals(other);
225 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000226
Stuart McCullochf3173222012-06-07 21:57:32 +0000227 @Deprecated
228 public int hashCode() {
229 return super.hashCode();
230 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000231
Stuart McCullochf3173222012-06-07 21:57:32 +0000232}