Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 1 | package aQute.bnd.osgi; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 2 | |
| 3 | import java.util.*; |
| 4 | |
Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 5 | import aQute.bnd.header.*; |
| 6 | import aQute.bnd.osgi.Descriptors.PackageRef; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 7 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 8 | public class Packages implements Map<PackageRef,Attrs> { |
| 9 | private LinkedHashMap<PackageRef,Attrs> map; |
| 10 | static Map<PackageRef,Attrs> EMPTY = Collections.emptyMap(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | |
| 12 | public Packages(Packages other) { |
| 13 | if (other.map != null) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 14 | map = new LinkedHashMap<Descriptors.PackageRef,Attrs>(other.map); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 15 | } |
| 16 | } |
| 17 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 18 | public Packages() {} |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 19 | |
| 20 | public void clear() { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 21 | if (map != null) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 22 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 32 | @Deprecated |
| 33 | public boolean containsKey(Object name) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 34 | assert name instanceof PackageRef; |
| 35 | if (map == null) |
| 36 | return false; |
| 37 | |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 38 | return map.containsKey(name); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | public boolean containsValue(Attrs value) { |
| 42 | if (map == null) |
| 43 | return false; |
| 44 | |
| 45 | return map.containsValue(value); |
| 46 | } |
| 47 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 48 | @Deprecated |
| 49 | public boolean containsValue(Object value) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 50 | assert value instanceof Attrs; |
| 51 | if (map == null) |
| 52 | return false; |
| 53 | |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 54 | return map.containsValue(value); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 57 | public Set<java.util.Map.Entry<PackageRef,Attrs>> entrySet() { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 58 | if (map == null) |
| 59 | return EMPTY.entrySet(); |
| 60 | |
| 61 | return map.entrySet(); |
| 62 | } |
| 63 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 64 | @Deprecated |
| 65 | public Attrs get(Object key) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 66 | assert key instanceof PackageRef; |
| 67 | if (map == null) |
| 68 | return null; |
| 69 | |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 70 | return map.get(key); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 71 | } |
| 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 103 | map = new LinkedHashMap<PackageRef,Attrs>(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 104 | |
| 105 | return map.put(key, value); |
| 106 | } |
| 107 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 108 | public void putAll(Map< ? extends PackageRef, ? extends Attrs> map) { |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 109 | if (this.map == null) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 110 | if (map.isEmpty()) |
| 111 | return; |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 112 | this.map = new LinkedHashMap<PackageRef,Attrs>(); |
| 113 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 114 | this.map.putAll(map); |
| 115 | } |
| 116 | |
| 117 | public void putAllIfAbsent(Map<PackageRef, ? extends Attrs> map) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 118 | for (Map.Entry<PackageRef, ? extends Attrs> entry : map.entrySet()) { |
| 119 | if (!containsKey(entry.getKey())) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 120 | put(entry.getKey(), entry.getValue()); |
| 121 | } |
| 122 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 123 | |
| 124 | @Deprecated |
| 125 | public Attrs remove(Object var0) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 126 | assert var0 instanceof PackageRef; |
| 127 | if (map == null) |
| 128 | return null; |
| 129 | |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 130 | return map.remove(var0); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 131 | } |
| 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 156 | for (Map.Entry<PackageRef,Attrs> pr : map.entrySet()) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 157 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 167 | for (Map.Entry<PackageRef,Attrs> pr : map.entrySet()) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 168 | 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 McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 182 | @Override |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 183 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 191 | for (Map.Entry<PackageRef,Attrs> s : entrySet()) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 192 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 203 | if (unique) { |
| 204 | while (containsKey(ref)) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 205 | ref = ref.getDuplicate(); |
| 206 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 207 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 208 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 217 | if (mine != null) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 218 | return mine; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 219 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 220 | return deflt; |
| 221 | } |
| 222 | |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 223 | @Override |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 224 | @Deprecated |
| 225 | public boolean equals(Object other) { |
| 226 | return super.equals(other); |
| 227 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 228 | |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 229 | @Override |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 230 | @Deprecated |
| 231 | public int hashCode() { |
| 232 | return super.hashCode(); |
| 233 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 234 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 235 | } |