blob: 6ac56ddd34a3c727e09a91f05ef711c43a031ec9 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.lib.osgi;
2
3import java.util.*;
4
5import aQute.libg.header.*;
6
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
148 public String toString() {
149 return map == null ? "{}" : map.toString();
150 }
151
152 public void append(Parameters other) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000153 for (Map.Entry<String,Attrs> e : other.entrySet()) {
154 put(new Instruction(e.getKey()), e.getValue());
Stuart McCullochbb014372012-06-07 21:57:32 +0000155 }
156 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000157
Stuart McCullochbb014372012-06-07 21:57:32 +0000158 public <T> Collection<T> select(Collection<T> set, boolean emptyIsAll) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000159 return select(set, null, emptyIsAll);
Stuart McCullochbb014372012-06-07 21:57:32 +0000160 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000161
Stuart McCullochbb014372012-06-07 21:57:32 +0000162 public <T> Collection<T> select(Collection<T> set, Set<Instruction> unused, boolean emptyIsAll) {
163 List<T> input = new ArrayList<T>(set);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000164 if (emptyIsAll && isEmpty())
Stuart McCullochbb014372012-06-07 21:57:32 +0000165 return input;
Stuart McCulloch2286f232012-06-15 13:27:53 +0000166
Stuart McCullochbb014372012-06-07 21:57:32 +0000167 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 McCulloch2286f232012-06-15 13:27:53 +0000181 if (!used && unused != null)
Stuart McCullochbb014372012-06-07 21:57:32 +0000182 unused.add(instruction);
183 }
184 return result;
185 }
186
Stuart McCullochbb014372012-06-07 21:57:32 +0000187 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 McCulloch2286f232012-06-15 13:27:53 +0000201
Stuart McCullochbb014372012-06-07 21:57:32 +0000202 }
203 }
204 return result;
205 }
206
207 public boolean matches(String value) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000208 if (size() == 0)
Stuart McCullochbb014372012-06-07 21:57:32 +0000209 return true;
Stuart McCulloch2286f232012-06-15 13:27:53 +0000210
211 for (Instruction i : keySet()) {
212 if (i.matches(value)) {
213 if (i.isNegated())
214 return false; // we deny this one explicitly
Stuart McCullochd4826102012-06-26 16:34:24 +0000215 return true; // we allow it explicitly
Stuart McCullochbb014372012-06-07 21:57:32 +0000216 }
217 }
218 return false;
219 }
220
221}