blob: b157f889e1fc1a09f44dde0e7b1d1e347d105e36 [file] [log] [blame]
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00001package aQute.bnd.osgi;
Stuart McCullochbb014372012-06-07 21:57:32 +00002
3import java.util.*;
4
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00005import aQute.bnd.header.*;
Stuart McCullochbb014372012-06-07 21:57:32 +00006
Stuart McCulloch2286f232012-06-15 13:27:53 +00007public class Instructions implements Map<Instruction,Attrs> {
8 private LinkedHashMap<Instruction,Attrs> map;
9 static Map<Instruction,Attrs> EMPTY = Collections.emptyMap();
Stuart McCullochbb014372012-06-07 21:57:32 +000010
11 public Instructions(Instructions other) {
12 if (other.map != null && !other.map.isEmpty()) {
Stuart McCulloch2286f232012-06-15 13:27:53 +000013 map = new LinkedHashMap<Instruction,Attrs>(other.map);
Stuart McCullochbb014372012-06-07 21:57:32 +000014 }
15 }
16
17 public Instructions(Collection<String> other) {
Stuart McCulloch2286f232012-06-15 13:27:53 +000018 if (other != null)
19 for (String s : other) {
20 put(new Instruction(s), null);
Stuart McCullochbb014372012-06-07 21:57:32 +000021 }
22 }
23
Stuart McCulloch2286f232012-06-15 13:27:53 +000024 public Instructions() {}
Stuart McCullochbb014372012-06-07 21:57:32 +000025
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 McCulloch2286f232012-06-15 13:27:53 +000045 @Deprecated
46 public boolean containsKey(Object name) {
Stuart McCullochbb014372012-06-07 21:57:32 +000047 assert name instanceof Instruction;
48 if (map == null)
49 return false;
50
Stuart McCullochd4826102012-06-26 16:34:24 +000051 return map.containsKey(name);
Stuart McCullochbb014372012-06-07 21:57:32 +000052 }
53
54 public boolean containsValue(Attrs value) {
55 if (map == null)
56 return false;
57
58 return map.containsValue(value);
59 }
60
Stuart McCulloch2286f232012-06-15 13:27:53 +000061 @Deprecated
62 public boolean containsValue(Object value) {
Stuart McCullochbb014372012-06-07 21:57:32 +000063 assert value instanceof Attrs;
64 if (map == null)
65 return false;
66
Stuart McCullochd4826102012-06-26 16:34:24 +000067 return map.containsValue(value);
Stuart McCullochbb014372012-06-07 21:57:32 +000068 }
69
Stuart McCulloch2286f232012-06-15 13:27:53 +000070 public Set<java.util.Map.Entry<Instruction,Attrs>> entrySet() {
Stuart McCullochbb014372012-06-07 21:57:32 +000071 if (map == null)
72 return EMPTY.entrySet();
73
74 return map.entrySet();
75 }
76
Stuart McCulloch2286f232012-06-15 13:27:53 +000077 @Deprecated
78 public Attrs get(Object key) {
Stuart McCullochbb014372012-06-07 21:57:32 +000079 assert key instanceof Instruction;
80 if (map == null)
81 return null;
82
Stuart McCullochd4826102012-06-26 16:34:24 +000083 return map.get(key);
Stuart McCullochbb014372012-06-07 21:57:32 +000084 }
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 McCulloch2286f232012-06-15 13:27:53 +0000106 map = new LinkedHashMap<Instruction,Attrs>();
Stuart McCullochbb014372012-06-07 21:57:32 +0000107
108 return map.put(key, value);
109 }
110
Stuart McCulloch2286f232012-06-15 13:27:53 +0000111 public void putAll(Map< ? extends Instruction, ? extends Attrs> map) {
Stuart McCullochd4826102012-06-26 16:34:24 +0000112 if (this.map == null) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000113 if (map.isEmpty())
114 return;
Stuart McCullochd4826102012-06-26 16:34:24 +0000115 this.map = new LinkedHashMap<Instruction,Attrs>();
116 }
Stuart McCullochbb014372012-06-07 21:57:32 +0000117 this.map.putAll(map);
118 }
119
Stuart McCulloch2286f232012-06-15 13:27:53 +0000120 @Deprecated
121 public Attrs remove(Object var0) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000122 assert var0 instanceof Instruction;
123 if (map == null)
124 return null;
125
Stuart McCullochd4826102012-06-26 16:34:24 +0000126 return map.remove(var0);
Stuart McCullochbb014372012-06-07 21:57:32 +0000127 }
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 McCulloch55d4dfe2012-08-07 10:57:21 +0000148 @Override
Stuart McCullochbb014372012-06-07 21:57:32 +0000149 public String toString() {
150 return map == null ? "{}" : map.toString();
151 }
152
153 public void append(Parameters other) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000154 for (Map.Entry<String,Attrs> e : other.entrySet()) {
155 put(new Instruction(e.getKey()), e.getValue());
Stuart McCullochbb014372012-06-07 21:57:32 +0000156 }
157 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000158
Stuart McCullochbb014372012-06-07 21:57:32 +0000159 public <T> Collection<T> select(Collection<T> set, boolean emptyIsAll) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000160 return select(set, null, emptyIsAll);
Stuart McCullochbb014372012-06-07 21:57:32 +0000161 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000162
Stuart McCullochbb014372012-06-07 21:57:32 +0000163 public <T> Collection<T> select(Collection<T> set, Set<Instruction> unused, boolean emptyIsAll) {
164 List<T> input = new ArrayList<T>(set);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000165 if (emptyIsAll && isEmpty())
Stuart McCullochbb014372012-06-07 21:57:32 +0000166 return input;
Stuart McCulloch2286f232012-06-15 13:27:53 +0000167
Stuart McCullochbb014372012-06-07 21:57:32 +0000168 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 McCulloch2286f232012-06-15 13:27:53 +0000182 if (!used && unused != null)
Stuart McCullochbb014372012-06-07 21:57:32 +0000183 unused.add(instruction);
184 }
185 return result;
186 }
187
Stuart McCullochbb014372012-06-07 21:57:32 +0000188 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 McCulloch2286f232012-06-15 13:27:53 +0000202
Stuart McCullochbb014372012-06-07 21:57:32 +0000203 }
204 }
205 return result;
206 }
207
208 public boolean matches(String value) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000209 if (size() == 0)
Stuart McCullochbb014372012-06-07 21:57:32 +0000210 return true;
Stuart McCulloch2286f232012-06-15 13:27:53 +0000211
212 for (Instruction i : keySet()) {
213 if (i.matches(value)) {
214 if (i.isNegated())
215 return false; // we deny this one explicitly
Stuart McCullochd4826102012-06-26 16:34:24 +0000216 return true; // we allow it explicitly
Stuart McCullochbb014372012-06-07 21:57:32 +0000217 }
218 }
219 return false;
220 }
221
222}