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.*; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 6 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 7 | public class Instructions implements Map<Instruction,Attrs> { |
| 8 | private LinkedHashMap<Instruction,Attrs> map; |
| 9 | static Map<Instruction,Attrs> EMPTY = Collections.emptyMap(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 10 | |
| 11 | public Instructions(Instructions other) { |
| 12 | if (other.map != null && !other.map.isEmpty()) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 13 | map = new LinkedHashMap<Instruction,Attrs>(other.map); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 14 | } |
| 15 | } |
| 16 | |
| 17 | public Instructions(Collection<String> other) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 18 | if (other != null) |
| 19 | for (String s : other) { |
| 20 | put(new Instruction(s), null); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 21 | } |
| 22 | } |
| 23 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 24 | public Instructions() {} |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 25 | |
| 26 | public Instructions(Parameters contained) { |
| 27 | append(contained); |
| 28 | } |
| 29 | |
| 30 | public Instructions(String h) { |
| 31 | this(new Parameters(h)); |
| 32 | } |
| 33 | |
| 34 | public void clear() { |
| 35 | map.clear(); |
| 36 | } |
| 37 | |
| 38 | public boolean containsKey(Instruction name) { |
| 39 | if (map == null) |
| 40 | return false; |
| 41 | |
| 42 | return map.containsKey(name); |
| 43 | } |
| 44 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 45 | @Deprecated |
| 46 | public boolean containsKey(Object name) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 47 | assert name instanceof Instruction; |
| 48 | if (map == null) |
| 49 | return false; |
| 50 | |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 51 | return map.containsKey(name); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | public boolean containsValue(Attrs value) { |
| 55 | if (map == null) |
| 56 | return false; |
| 57 | |
| 58 | return map.containsValue(value); |
| 59 | } |
| 60 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 61 | @Deprecated |
| 62 | public boolean containsValue(Object value) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 63 | assert value instanceof Attrs; |
| 64 | if (map == null) |
| 65 | return false; |
| 66 | |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 67 | return map.containsValue(value); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 70 | public Set<java.util.Map.Entry<Instruction,Attrs>> entrySet() { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 71 | if (map == null) |
| 72 | return EMPTY.entrySet(); |
| 73 | |
| 74 | return map.entrySet(); |
| 75 | } |
| 76 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 77 | @Deprecated |
| 78 | public Attrs get(Object key) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 79 | assert key instanceof Instruction; |
| 80 | if (map == null) |
| 81 | return null; |
| 82 | |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 83 | return map.get(key); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public Attrs get(Instruction key) { |
| 87 | if (map == null) |
| 88 | return null; |
| 89 | |
| 90 | return map.get(key); |
| 91 | } |
| 92 | |
| 93 | public boolean isEmpty() { |
| 94 | return map == null || map.isEmpty(); |
| 95 | } |
| 96 | |
| 97 | public Set<Instruction> keySet() { |
| 98 | if (map == null) |
| 99 | return EMPTY.keySet(); |
| 100 | |
| 101 | return map.keySet(); |
| 102 | } |
| 103 | |
| 104 | public Attrs put(Instruction key, Attrs value) { |
| 105 | if (map == null) |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 106 | map = new LinkedHashMap<Instruction,Attrs>(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 107 | |
| 108 | return map.put(key, value); |
| 109 | } |
| 110 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 111 | public void putAll(Map< ? extends Instruction, ? extends Attrs> map) { |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 112 | if (this.map == null) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 113 | if (map.isEmpty()) |
| 114 | return; |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 115 | this.map = new LinkedHashMap<Instruction,Attrs>(); |
| 116 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 117 | this.map.putAll(map); |
| 118 | } |
| 119 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 120 | @Deprecated |
| 121 | public Attrs remove(Object var0) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 122 | assert var0 instanceof Instruction; |
| 123 | if (map == null) |
| 124 | return null; |
| 125 | |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 126 | return map.remove(var0); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | public Attrs remove(Instruction var0) { |
| 130 | if (map == null) |
| 131 | return null; |
| 132 | return map.remove(var0); |
| 133 | } |
| 134 | |
| 135 | public int size() { |
| 136 | if (map == null) |
| 137 | return 0; |
| 138 | return map.size(); |
| 139 | } |
| 140 | |
| 141 | public Collection<Attrs> values() { |
| 142 | if (map == null) |
| 143 | return EMPTY.values(); |
| 144 | |
| 145 | return map.values(); |
| 146 | } |
| 147 | |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 148 | @Override |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 149 | public String toString() { |
| 150 | return map == null ? "{}" : map.toString(); |
| 151 | } |
| 152 | |
| 153 | public void append(Parameters other) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 154 | for (Map.Entry<String,Attrs> e : other.entrySet()) { |
| 155 | put(new Instruction(e.getKey()), e.getValue()); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 158 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 159 | public <T> Collection<T> select(Collection<T> set, boolean emptyIsAll) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 160 | return select(set, null, emptyIsAll); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 161 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 162 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 163 | public <T> Collection<T> select(Collection<T> set, Set<Instruction> unused, boolean emptyIsAll) { |
| 164 | List<T> input = new ArrayList<T>(set); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 165 | if (emptyIsAll && isEmpty()) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 166 | return input; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 167 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 168 | List<T> result = new ArrayList<T>(); |
| 169 | |
| 170 | for (Instruction instruction : keySet()) { |
| 171 | boolean used = false; |
| 172 | for (Iterator<T> o = input.iterator(); o.hasNext();) { |
| 173 | T oo = o.next(); |
| 174 | String s = oo.toString(); |
| 175 | if (instruction.matches(s)) { |
| 176 | if (!instruction.isNegated()) |
| 177 | result.add(oo); |
| 178 | o.remove(); |
| 179 | used = true; |
| 180 | } |
| 181 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 182 | if (!used && unused != null) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 183 | unused.add(instruction); |
| 184 | } |
| 185 | return result; |
| 186 | } |
| 187 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 188 | public <T> Collection<T> reject(Collection<T> set) { |
| 189 | List<T> input = new ArrayList<T>(set); |
| 190 | List<T> result = new ArrayList<T>(); |
| 191 | |
| 192 | for (Instruction instruction : keySet()) { |
| 193 | for (Iterator<T> o = input.iterator(); o.hasNext();) { |
| 194 | T oo = o.next(); |
| 195 | String s = oo.toString(); |
| 196 | if (instruction.matches(s)) { |
| 197 | if (instruction.isNegated()) |
| 198 | result.add(oo); |
| 199 | o.remove(); |
| 200 | } else |
| 201 | result.add(oo); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 202 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | return result; |
| 206 | } |
| 207 | |
| 208 | public boolean matches(String value) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 209 | if (size() == 0) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 210 | return true; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 211 | |
| 212 | for (Instruction i : keySet()) { |
| 213 | if (i.matches(value)) { |
| 214 | if (i.isNegated()) |
| 215 | return false; // we deny this one explicitly |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 216 | return true; // we allow it explicitly |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | } |