Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.osgi; |
| 2 | |
| 3 | import java.util.*; |
| 4 | |
| 5 | import aQute.lib.osgi.Descriptors.PackageRef; |
| 6 | import aQute.libg.header.*; |
| 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 | |
| 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 190 | for (Map.Entry<PackageRef,Attrs> s : entrySet()) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 191 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 202 | if (unique) { |
| 203 | while (containsKey(ref)) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 204 | ref = ref.getDuplicate(); |
| 205 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 206 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 207 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 216 | if (mine != null) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 217 | return mine; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 218 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 219 | return deflt; |
| 220 | } |
| 221 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 222 | @Deprecated |
| 223 | public boolean equals(Object other) { |
| 224 | return super.equals(other); |
| 225 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 226 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 227 | @Deprecated |
| 228 | public int hashCode() { |
| 229 | return super.hashCode(); |
| 230 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 231 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 232 | } |