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