1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.opencastproject.mediapackage;
24
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import javax.xml.bind.annotation.adapters.XmlAdapter;
29
30
31
32
33 public class MediaPackageReferenceImpl implements MediaPackageReference {
34
35
36 public static final MediaPackageReference ANY_MEDIAPACKAGE = new MediaPackageReferenceImpl(TYPE_MEDIAPACKAGE, ANY);
37
38
39 protected String identifier = null;
40
41
42 protected String type = null;
43
44
45 private String externalForm = null;
46
47
48 private Map<String, String> properties = null;
49
50
51
52
53 public MediaPackageReferenceImpl() {
54 this(TYPE_MEDIAPACKAGE, SELF);
55 }
56
57
58
59
60
61
62
63 public MediaPackageReferenceImpl(MediaPackage mediaPackage) {
64 if (mediaPackage == null)
65 throw new IllegalArgumentException("Parameter media package must not be null");
66 type = TYPE_MEDIAPACKAGE;
67 if (mediaPackage.getIdentifier() != null)
68 identifier = mediaPackage.getIdentifier().toString();
69 else
70 identifier = SELF;
71 properties = new HashMap<String, String>();
72 }
73
74
75
76
77
78
79
80
81
82
83
84 public MediaPackageReferenceImpl(MediaPackageElement mediaPackageElement) {
85 if (mediaPackageElement == null)
86 throw new IllegalArgumentException("Parameter media package element must not be null");
87 this.type = mediaPackageElement.getElementType().toString().toLowerCase();
88 this.identifier = mediaPackageElement.getIdentifier();
89 if (identifier == null)
90 throw new IllegalArgumentException("Media package element must have an identifier");
91 this.properties = new HashMap<String, String>();
92 }
93
94
95
96
97
98
99
100
101
102 public MediaPackageReferenceImpl(String type, String identifier) {
103 if (type == null)
104 throw new IllegalArgumentException("Parameter type must not be null");
105 if (identifier == null)
106 throw new IllegalArgumentException("Parameter identifier must not be null");
107 this.type = type;
108 this.identifier = identifier;
109 this.properties = new HashMap<String, String>();
110 }
111
112
113
114
115
116
117
118
119 public static MediaPackageReference fromString(String reference) throws IllegalArgumentException {
120 if (reference == null)
121 throw new IllegalArgumentException("Reference is null");
122
123 MediaPackageReference ref = null;
124
125 String[] parts = reference.split(";");
126 String elementReference = parts[0];
127
128
129 if ("self".equals(elementReference))
130 ref = new MediaPackageReferenceImpl(MediaPackageReference.TYPE_MEDIAPACKAGE, "self");
131 else {
132 String[] elementReferenceParts = elementReference.split(":");
133 if (elementReferenceParts.length != 2)
134 throw new IllegalArgumentException("Reference " + reference + " is malformed");
135 ref = new MediaPackageReferenceImpl(elementReferenceParts[0], elementReferenceParts[1]);
136 }
137
138
139 for (int i = 1; i < parts.length; i++) {
140 String[] propertyParts = parts[i].split("=");
141 if (propertyParts.length != 2)
142 throw new IllegalStateException("malformatted reference properties");
143 String key = propertyParts[0];
144 String value = propertyParts[1];
145 ref.setProperty(key, value);
146 }
147
148 return ref;
149 }
150
151
152
153
154 public String getIdentifier() {
155 return identifier;
156 }
157
158
159
160
161 public String getType() {
162 return type;
163 }
164
165
166
167
168 public Map<String, String> getProperties() {
169 return properties;
170 }
171
172
173
174
175
176 public void setProperties(Map<String, String> properties) {
177 this.properties = properties;
178 }
179
180
181
182
183
184
185 @Override
186 public String getProperty(String key) {
187 return properties.get(key);
188 }
189
190
191
192
193
194
195 @Override
196 public void setProperty(String key, String value) {
197 if (value == null)
198 this.properties.remove(key);
199 this.properties.put(key, value);
200 }
201
202
203
204
205 public boolean matches(MediaPackageReference reference) {
206 if (reference == null)
207 return false;
208
209
210 if (!type.equals(reference.getType()))
211 return false;
212
213
214 if (properties != null && !properties.equals(reference.getProperties()))
215 return false;
216 else if (reference.getProperties() != null && !reference.getProperties().equals(properties))
217 return false;
218
219
220 if (identifier.equals(reference.getIdentifier()))
221 return true;
222 else if (ANY.equals(identifier) || ANY.equals(reference.getIdentifier()))
223 return true;
224 else if (SELF.equals(identifier) || SELF.equals(reference.getIdentifier()))
225 return true;
226
227 return false;
228 }
229
230
231
232
233
234
235 @Override
236 public Object clone() {
237 MediaPackageReferenceImpl clone = new MediaPackageReferenceImpl(type, identifier);
238 clone.getProperties().putAll(properties);
239 return clone;
240 }
241
242
243
244
245 @Override
246 public int hashCode() {
247 return toString().hashCode();
248 }
249
250
251
252
253 @Override
254 public boolean equals(Object obj) {
255 if (obj == null || !(obj instanceof MediaPackageReference))
256 return false;
257 MediaPackageReference ref = (MediaPackageReference) obj;
258 return type.equals(ref.getType()) && identifier.equals(ref.getIdentifier());
259 }
260
261
262
263
264 @Override
265 public String toString() {
266 if (externalForm == null) {
267 StringBuffer buf = new StringBuffer();
268 if (TYPE_MEDIAPACKAGE.equals(type) && SELF.equals(identifier)) {
269 buf.append("self");
270 } else {
271 buf.append(type);
272 buf.append(":");
273 buf.append(identifier);
274 }
275 if (properties.size() > 0) {
276 for (Map.Entry<String, String> entry : properties.entrySet()) {
277 buf.append(";");
278 buf.append(entry.getKey());
279 buf.append("=");
280 buf.append(entry.getValue());
281 }
282 }
283 externalForm = buf.toString();
284 }
285 return externalForm;
286 }
287
288 public static class Adapter extends XmlAdapter<String, MediaPackageReference> {
289 @Override
290 public String marshal(MediaPackageReference ref) throws Exception {
291 if (ref == null)
292 return null;
293 return ref.toString();
294 }
295
296 @Override
297 public MediaPackageReference unmarshal(String ref) throws Exception {
298 if (ref == null)
299 return null;
300 return MediaPackageReferenceImpl.fromString(ref);
301 }
302 }
303 }