Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame^] | 1 | package aQute.bnd.header; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 2 | |
| 3 | import java.util.*; |
| 4 | |
| 5 | import aQute.lib.collections.*; |
Stuart McCulloch | 81d48de | 2012-06-29 19:23:09 +0000 | [diff] [blame] | 6 | import aQute.service.reporter.*; |
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 Parameters implements Map<String,Attrs> { |
| 9 | private LinkedHashMap<String,Attrs> map; |
| 10 | static Map<String,Attrs> EMPTY = Collections.emptyMap(); |
| 11 | String error; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 12 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 13 | public Parameters() {} |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 14 | |
| 15 | public Parameters(String header) { |
| 16 | OSGiHeader.parseHeader(header, null, this); |
| 17 | } |
| 18 | |
| 19 | public Parameters(String header, Reporter reporter) { |
| 20 | OSGiHeader.parseHeader(header, reporter, this); |
| 21 | } |
| 22 | |
| 23 | public void clear() { |
| 24 | map.clear(); |
| 25 | } |
| 26 | |
| 27 | public boolean containsKey(final String name) { |
| 28 | if (map == null) |
| 29 | return false; |
| 30 | |
| 31 | return map.containsKey(name); |
| 32 | } |
| 33 | |
| 34 | @SuppressWarnings("cast") |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 35 | @Deprecated |
| 36 | public boolean containsKey(Object name) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 37 | assert name instanceof String; |
| 38 | if (map == null) |
| 39 | return false; |
| 40 | |
| 41 | return map.containsKey((String) name); |
| 42 | } |
| 43 | |
| 44 | public boolean containsValue(Attrs value) { |
| 45 | if (map == null) |
| 46 | return false; |
| 47 | |
| 48 | return map.containsValue(value); |
| 49 | } |
| 50 | |
| 51 | @SuppressWarnings("cast") |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 52 | @Deprecated |
| 53 | public boolean containsValue(Object value) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 54 | assert value instanceof Attrs; |
| 55 | if (map == null) |
| 56 | return false; |
| 57 | |
| 58 | return map.containsValue((Attrs) value); |
| 59 | } |
| 60 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 61 | public Set<java.util.Map.Entry<String,Attrs>> entrySet() { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 62 | if (map == null) |
| 63 | return EMPTY.entrySet(); |
| 64 | |
| 65 | return map.entrySet(); |
| 66 | } |
| 67 | |
| 68 | @SuppressWarnings("cast") |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 69 | @Deprecated |
| 70 | public Attrs get(Object key) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 71 | assert key instanceof String; |
| 72 | if (map == null) |
| 73 | return null; |
| 74 | |
| 75 | return map.get((String) key); |
| 76 | } |
| 77 | |
| 78 | public Attrs get(String key) { |
| 79 | if (map == null) |
| 80 | return null; |
| 81 | |
| 82 | return map.get(key); |
| 83 | } |
| 84 | |
| 85 | public boolean isEmpty() { |
| 86 | return map == null || map.isEmpty(); |
| 87 | } |
| 88 | |
| 89 | public Set<String> keySet() { |
| 90 | if (map == null) |
| 91 | return EMPTY.keySet(); |
| 92 | |
| 93 | return map.keySet(); |
| 94 | } |
| 95 | |
| 96 | public Attrs put(String key, Attrs value) { |
| 97 | assert key != null; |
| 98 | assert value != null; |
| 99 | |
| 100 | if (map == null) |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 101 | map = new LinkedHashMap<String,Attrs>(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 102 | |
| 103 | return map.put(key, value); |
| 104 | } |
| 105 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 106 | public void putAll(Map< ? extends String, ? extends Attrs> map) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 107 | if (this.map == null) { |
| 108 | if (map.isEmpty()) |
| 109 | return; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 110 | this.map = new LinkedHashMap<String,Attrs>(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 111 | } |
| 112 | this.map.putAll(map); |
| 113 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 114 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 115 | public void putAllIfAbsent(Map<String, ? extends Attrs> map) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 116 | for (Map.Entry<String, ? extends Attrs> entry : map.entrySet()) { |
| 117 | if (!containsKey(entry.getKey())) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 118 | put(entry.getKey(), entry.getValue()); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | @SuppressWarnings("cast") |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 123 | @Deprecated |
| 124 | public Attrs remove(Object var0) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 125 | assert var0 instanceof String; |
| 126 | if (map == null) |
| 127 | return null; |
| 128 | |
| 129 | return map.remove((String) var0); |
| 130 | } |
| 131 | |
| 132 | public Attrs remove(String var0) { |
| 133 | if (map == null) |
| 134 | return null; |
| 135 | return map.remove(var0); |
| 136 | } |
| 137 | |
| 138 | public int size() { |
| 139 | if (map == null) |
| 140 | return 0; |
| 141 | return map.size(); |
| 142 | } |
| 143 | |
| 144 | public Collection<Attrs> values() { |
| 145 | if (map == null) |
| 146 | return EMPTY.values(); |
| 147 | |
| 148 | return map.values(); |
| 149 | } |
| 150 | |
| 151 | public String toString() { |
| 152 | StringBuilder sb = new StringBuilder(); |
| 153 | append(sb); |
| 154 | return sb.toString(); |
| 155 | } |
| 156 | |
| 157 | public void append(StringBuilder sb) { |
| 158 | String del = ""; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 159 | for (Map.Entry<String,Attrs> s : entrySet()) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 160 | sb.append(del); |
| 161 | sb.append(s.getKey()); |
| 162 | if (!s.getValue().isEmpty()) { |
| 163 | sb.append(';'); |
| 164 | s.getValue().append(sb); |
| 165 | } |
| 166 | |
| 167 | del = ","; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | @Deprecated |
| 172 | public boolean equals(Object other) { |
| 173 | return super.equals(other); |
| 174 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 175 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 176 | @Deprecated |
| 177 | public int hashCode() { |
| 178 | return super.hashCode(); |
| 179 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 180 | |
| 181 | public boolean isEqual(Parameters other) { |
| 182 | if (this == other) |
| 183 | return true; |
| 184 | |
| 185 | if (size() != other.size()) |
| 186 | return false; |
| 187 | |
| 188 | if (isEmpty()) |
| 189 | return true; |
| 190 | |
| 191 | SortedList<String> l = new SortedList<String>(keySet()); |
| 192 | SortedList<String> lo = new SortedList<String>(other.keySet()); |
| 193 | if (!l.isEqual(lo)) |
| 194 | return false; |
| 195 | |
| 196 | for (String key : keySet()) { |
| 197 | if (!get(key).isEqual(other.get(key))) |
| 198 | return false; |
| 199 | } |
| 200 | return true; |
| 201 | } |
| 202 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 203 | public Map<String, ? extends Map<String,String>> asMapMap() { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 204 | return this; |
| 205 | } |
| 206 | } |