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.libg.header.*; |
| 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 | |
| 148 | public String toString() { |
| 149 | return map == null ? "{}" : map.toString(); |
| 150 | } |
| 151 | |
| 152 | public void append(Parameters other) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 153 | for (Map.Entry<String,Attrs> e : other.entrySet()) { |
| 154 | put(new Instruction(e.getKey()), e.getValue()); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 157 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 158 | public <T> Collection<T> select(Collection<T> set, boolean emptyIsAll) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 159 | return select(set, null, emptyIsAll); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 160 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 161 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 162 | public <T> Collection<T> select(Collection<T> set, Set<Instruction> unused, boolean emptyIsAll) { |
| 163 | List<T> input = new ArrayList<T>(set); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 164 | if (emptyIsAll && isEmpty()) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 165 | return input; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 166 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 167 | List<T> result = new ArrayList<T>(); |
| 168 | |
| 169 | for (Instruction instruction : keySet()) { |
| 170 | boolean used = false; |
| 171 | for (Iterator<T> o = input.iterator(); o.hasNext();) { |
| 172 | T oo = o.next(); |
| 173 | String s = oo.toString(); |
| 174 | if (instruction.matches(s)) { |
| 175 | if (!instruction.isNegated()) |
| 176 | result.add(oo); |
| 177 | o.remove(); |
| 178 | used = true; |
| 179 | } |
| 180 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 181 | if (!used && unused != null) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 182 | unused.add(instruction); |
| 183 | } |
| 184 | return result; |
| 185 | } |
| 186 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 187 | public <T> Collection<T> reject(Collection<T> set) { |
| 188 | List<T> input = new ArrayList<T>(set); |
| 189 | List<T> result = new ArrayList<T>(); |
| 190 | |
| 191 | for (Instruction instruction : keySet()) { |
| 192 | for (Iterator<T> o = input.iterator(); o.hasNext();) { |
| 193 | T oo = o.next(); |
| 194 | String s = oo.toString(); |
| 195 | if (instruction.matches(s)) { |
| 196 | if (instruction.isNegated()) |
| 197 | result.add(oo); |
| 198 | o.remove(); |
| 199 | } else |
| 200 | result.add(oo); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 201 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | return result; |
| 205 | } |
| 206 | |
| 207 | public boolean matches(String value) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 208 | if (size() == 0) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 209 | return true; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 210 | |
| 211 | for (Instruction i : keySet()) { |
| 212 | if (i.matches(value)) { |
| 213 | if (i.isNegated()) |
| 214 | return false; // we deny this one explicitly |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame^] | 215 | return true; // we allow it explicitly |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | } |