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 | |
| 8 | public class Packages implements Map<PackageRef, Attrs> { |
| 9 | private LinkedHashMap<PackageRef, Attrs> map; |
| 10 | static Map<PackageRef, Attrs> EMPTY = Collections.emptyMap(); |
| 11 | |
| 12 | public Packages(Packages other) { |
| 13 | if (other.map != null) { |
| 14 | map = new LinkedHashMap<Descriptors.PackageRef, Attrs>(other.map); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | public Packages() { |
| 19 | } |
| 20 | |
| 21 | public void clear() { |
| 22 | if(map!=null) |
| 23 | map.clear(); |
| 24 | } |
| 25 | |
| 26 | public boolean containsKey(PackageRef name) { |
| 27 | if (map == null) |
| 28 | return false; |
| 29 | |
| 30 | return map.containsKey(name); |
| 31 | } |
| 32 | |
| 33 | @Deprecated public boolean containsKey(Object name) { |
| 34 | assert name instanceof PackageRef; |
| 35 | if (map == null) |
| 36 | return false; |
| 37 | |
| 38 | return map.containsKey((PackageRef) name); |
| 39 | } |
| 40 | |
| 41 | public boolean containsValue(Attrs value) { |
| 42 | if (map == null) |
| 43 | return false; |
| 44 | |
| 45 | return map.containsValue(value); |
| 46 | } |
| 47 | |
| 48 | @Deprecated public boolean containsValue(Object value) { |
| 49 | assert value instanceof Attrs; |
| 50 | if (map == null) |
| 51 | return false; |
| 52 | |
| 53 | return map.containsValue((Attrs) value); |
| 54 | } |
| 55 | |
| 56 | public Set<java.util.Map.Entry<PackageRef, Attrs>> entrySet() { |
| 57 | if (map == null) |
| 58 | return EMPTY.entrySet(); |
| 59 | |
| 60 | return map.entrySet(); |
| 61 | } |
| 62 | |
| 63 | @Deprecated public Attrs get(Object key) { |
| 64 | assert key instanceof PackageRef; |
| 65 | if (map == null) |
| 66 | return null; |
| 67 | |
| 68 | return map.get((PackageRef) key); |
| 69 | } |
| 70 | |
| 71 | public Attrs get(PackageRef key) { |
| 72 | if (map == null) |
| 73 | return null; |
| 74 | |
| 75 | return map.get(key); |
| 76 | } |
| 77 | |
| 78 | public boolean isEmpty() { |
| 79 | return map == null || map.isEmpty(); |
| 80 | } |
| 81 | |
| 82 | public Set<PackageRef> keySet() { |
| 83 | if (map == null) |
| 84 | return EMPTY.keySet(); |
| 85 | |
| 86 | return map.keySet(); |
| 87 | } |
| 88 | |
| 89 | public Attrs put(PackageRef ref) { |
| 90 | Attrs attrs = get(ref); |
| 91 | if (attrs != null) |
| 92 | return attrs; |
| 93 | |
| 94 | attrs = new Attrs(); |
| 95 | put(ref, attrs); |
| 96 | return attrs; |
| 97 | } |
| 98 | |
| 99 | public Attrs put(PackageRef key, Attrs value) { |
| 100 | if (map == null) |
| 101 | map = new LinkedHashMap<PackageRef, Attrs>(); |
| 102 | |
| 103 | return map.put(key, value); |
| 104 | } |
| 105 | |
| 106 | public void putAll(Map<? extends PackageRef, ? extends Attrs> map) { |
| 107 | if (this.map == null) |
| 108 | if (map.isEmpty()) |
| 109 | return; |
| 110 | else |
| 111 | this.map = new LinkedHashMap<PackageRef, Attrs>(); |
| 112 | this.map.putAll(map); |
| 113 | } |
| 114 | |
| 115 | public void putAllIfAbsent(Map<PackageRef, ? extends Attrs> map) { |
| 116 | for(Map.Entry<PackageRef, ? extends Attrs> entry : map.entrySet() ) { |
| 117 | if ( !containsKey(entry.getKey())) |
| 118 | put(entry.getKey(), entry.getValue()); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | @Deprecated public Attrs remove(Object var0) { |
| 123 | assert var0 instanceof PackageRef; |
| 124 | if (map == null) |
| 125 | return null; |
| 126 | |
| 127 | return map.remove((PackageRef) var0); |
| 128 | } |
| 129 | |
| 130 | public Attrs remove(PackageRef var0) { |
| 131 | if (map == null) |
| 132 | return null; |
| 133 | return map.remove(var0); |
| 134 | } |
| 135 | |
| 136 | public int size() { |
| 137 | if (map == null) |
| 138 | return 0; |
| 139 | return map.size(); |
| 140 | } |
| 141 | |
| 142 | public Collection<Attrs> values() { |
| 143 | if (map == null) |
| 144 | return EMPTY.values(); |
| 145 | |
| 146 | return map.values(); |
| 147 | } |
| 148 | |
| 149 | public Attrs getByFQN(String s) { |
| 150 | if (map == null) |
| 151 | return null; |
| 152 | |
| 153 | for (Map.Entry<PackageRef, Attrs> pr : map.entrySet()) { |
| 154 | if (pr.getKey().getFQN().equals(s)) |
| 155 | return pr.getValue(); |
| 156 | } |
| 157 | return null; |
| 158 | } |
| 159 | |
| 160 | public Attrs getByBinaryName(String s) { |
| 161 | if (map == null) |
| 162 | return null; |
| 163 | |
| 164 | for (Map.Entry<PackageRef, Attrs> pr : map.entrySet()) { |
| 165 | if (pr.getKey().getBinary().equals(s)) |
| 166 | pr.getValue(); |
| 167 | } |
| 168 | return null; |
| 169 | } |
| 170 | |
| 171 | public boolean containsFQN(String s) { |
| 172 | return getByFQN(s) != null; |
| 173 | } |
| 174 | |
| 175 | public boolean containsBinaryName(String s) { |
| 176 | return getByFQN(s) != null; |
| 177 | } |
| 178 | |
| 179 | public String toString() { |
| 180 | StringBuilder sb = new StringBuilder(); |
| 181 | append(sb); |
| 182 | return sb.toString(); |
| 183 | } |
| 184 | |
| 185 | public void append(StringBuilder sb) { |
| 186 | String del = ""; |
| 187 | for (Map.Entry<PackageRef, Attrs> s : entrySet()) { |
| 188 | sb.append(del); |
| 189 | sb.append(s.getKey()); |
| 190 | if (!s.getValue().isEmpty()) { |
| 191 | sb.append(';'); |
| 192 | s.getValue().append(sb); |
| 193 | } |
| 194 | del = ","; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | public void merge(PackageRef ref, boolean unique, Attrs... attrs) { |
| 199 | if ( unique ) { |
| 200 | while ( containsKey(ref)) |
| 201 | ref = ref.getDuplicate(); |
| 202 | } |
| 203 | |
| 204 | Attrs org = put(ref); |
| 205 | for (Attrs a : attrs) { |
| 206 | if (a != null) |
| 207 | org.putAll(a); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | public Attrs get(PackageRef packageRef, Attrs deflt) { |
| 212 | Attrs mine = get(packageRef); |
| 213 | if ( mine!=null) |
| 214 | return mine; |
| 215 | |
| 216 | return deflt; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | @Deprecated |
| 221 | public boolean equals(Object other) { |
| 222 | return super.equals(other); |
| 223 | } |
| 224 | |
| 225 | @Deprecated |
| 226 | public int hashCode() { |
| 227 | return super.hashCode(); |
| 228 | } |
| 229 | |
| 230 | } |