blob: 413b6a5ac9ae3425c029ba2070b09483a9ce1814 [file] [log] [blame]
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00001package aQute.bnd.header;
Stuart McCullochbb014372012-06-07 21:57:32 +00002
3import java.util.*;
4
5import aQute.lib.collections.*;
Stuart McCulloch81d48de2012-06-29 19:23:09 +00006import aQute.service.reporter.*;
Stuart McCullochbb014372012-06-07 21:57:32 +00007
Stuart McCulloch2286f232012-06-15 13:27:53 +00008public class Parameters implements Map<String,Attrs> {
9 private LinkedHashMap<String,Attrs> map;
10 static Map<String,Attrs> EMPTY = Collections.emptyMap();
11 String error;
Stuart McCullochbb014372012-06-07 21:57:32 +000012
Stuart McCulloch2286f232012-06-15 13:27:53 +000013 public Parameters() {}
Stuart McCullochbb014372012-06-07 21:57:32 +000014
15 public Parameters(String header) {
16 OSGiHeader.parseHeader(header, null, this);
17 }
18
19 public Parameters(String header, Reporter reporter) {
20 OSGiHeader.parseHeader(header, reporter, this);
21 }
22
23 public void clear() {
24 map.clear();
25 }
26
27 public boolean containsKey(final String name) {
28 if (map == null)
29 return false;
30
31 return map.containsKey(name);
32 }
33
34 @SuppressWarnings("cast")
Stuart McCulloch2286f232012-06-15 13:27:53 +000035 @Deprecated
36 public boolean containsKey(Object name) {
Stuart McCullochbb014372012-06-07 21:57:32 +000037 assert name instanceof String;
38 if (map == null)
39 return false;
40
41 return map.containsKey((String) name);
42 }
43
44 public boolean containsValue(Attrs value) {
45 if (map == null)
46 return false;
47
48 return map.containsValue(value);
49 }
50
51 @SuppressWarnings("cast")
Stuart McCulloch2286f232012-06-15 13:27:53 +000052 @Deprecated
53 public boolean containsValue(Object value) {
Stuart McCullochbb014372012-06-07 21:57:32 +000054 assert value instanceof Attrs;
55 if (map == null)
56 return false;
57
58 return map.containsValue((Attrs) value);
59 }
60
Stuart McCulloch2286f232012-06-15 13:27:53 +000061 public Set<java.util.Map.Entry<String,Attrs>> entrySet() {
Stuart McCullochbb014372012-06-07 21:57:32 +000062 if (map == null)
63 return EMPTY.entrySet();
64
65 return map.entrySet();
66 }
67
68 @SuppressWarnings("cast")
Stuart McCulloch2286f232012-06-15 13:27:53 +000069 @Deprecated
70 public Attrs get(Object key) {
Stuart McCullochbb014372012-06-07 21:57:32 +000071 assert key instanceof String;
72 if (map == null)
73 return null;
74
75 return map.get((String) key);
76 }
77
78 public Attrs get(String key) {
79 if (map == null)
80 return null;
81
82 return map.get(key);
83 }
84
85 public boolean isEmpty() {
86 return map == null || map.isEmpty();
87 }
88
89 public Set<String> keySet() {
90 if (map == null)
91 return EMPTY.keySet();
92
93 return map.keySet();
94 }
95
96 public Attrs put(String key, Attrs value) {
97 assert key != null;
98 assert value != null;
99
100 if (map == null)
Stuart McCulloch2286f232012-06-15 13:27:53 +0000101 map = new LinkedHashMap<String,Attrs>();
Stuart McCullochbb014372012-06-07 21:57:32 +0000102
103 return map.put(key, value);
104 }
105
Stuart McCulloch2286f232012-06-15 13:27:53 +0000106 public void putAll(Map< ? extends String, ? extends Attrs> map) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000107 if (this.map == null) {
108 if (map.isEmpty())
109 return;
Stuart McCulloch2286f232012-06-15 13:27:53 +0000110 this.map = new LinkedHashMap<String,Attrs>();
Stuart McCullochbb014372012-06-07 21:57:32 +0000111 }
112 this.map.putAll(map);
113 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000114
Stuart McCullochbb014372012-06-07 21:57:32 +0000115 public void putAllIfAbsent(Map<String, ? extends Attrs> map) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000116 for (Map.Entry<String, ? extends Attrs> entry : map.entrySet()) {
117 if (!containsKey(entry.getKey()))
Stuart McCullochbb014372012-06-07 21:57:32 +0000118 put(entry.getKey(), entry.getValue());
119 }
120 }
121
122 @SuppressWarnings("cast")
Stuart McCulloch2286f232012-06-15 13:27:53 +0000123 @Deprecated
124 public Attrs remove(Object var0) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000125 assert var0 instanceof String;
126 if (map == null)
127 return null;
128
129 return map.remove((String) var0);
130 }
131
132 public Attrs remove(String var0) {
133 if (map == null)
134 return null;
135 return map.remove(var0);
136 }
137
138 public int size() {
139 if (map == null)
140 return 0;
141 return map.size();
142 }
143
144 public Collection<Attrs> values() {
145 if (map == null)
146 return EMPTY.values();
147
148 return map.values();
149 }
150
151 public String toString() {
152 StringBuilder sb = new StringBuilder();
153 append(sb);
154 return sb.toString();
155 }
156
157 public void append(StringBuilder sb) {
158 String del = "";
Stuart McCulloch2286f232012-06-15 13:27:53 +0000159 for (Map.Entry<String,Attrs> s : entrySet()) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000160 sb.append(del);
161 sb.append(s.getKey());
162 if (!s.getValue().isEmpty()) {
163 sb.append(';');
164 s.getValue().append(sb);
165 }
166
167 del = ",";
168 }
169 }
170
171 @Deprecated
172 public boolean equals(Object other) {
173 return super.equals(other);
174 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000175
Stuart McCullochbb014372012-06-07 21:57:32 +0000176 @Deprecated
177 public int hashCode() {
178 return super.hashCode();
179 }
Stuart McCullochbb014372012-06-07 21:57:32 +0000180
181 public boolean isEqual(Parameters other) {
182 if (this == other)
183 return true;
184
185 if (size() != other.size())
186 return false;
187
188 if (isEmpty())
189 return true;
190
191 SortedList<String> l = new SortedList<String>(keySet());
192 SortedList<String> lo = new SortedList<String>(other.keySet());
193 if (!l.isEqual(lo))
194 return false;
195
196 for (String key : keySet()) {
197 if (!get(key).isEqual(other.get(key)))
198 return false;
199 }
200 return true;
201 }
202
Stuart McCulloch2286f232012-06-15 13:27:53 +0000203 public Map<String, ? extends Map<String,String>> asMapMap() {
Stuart McCullochbb014372012-06-07 21:57:32 +0000204 return this;
205 }
206}