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 }
67 type = TYPE_MEDIAPACKAGE;
68 if (mediaPackage.getIdentifier() != null) {
69 identifier = mediaPackage.getIdentifier().toString();
70 } else {
71 identifier = SELF;
72 }
73 properties = new HashMap<String, String>();
74 }
75
76
77
78
79
80
81
82
83
84
85
86 public MediaPackageReferenceImpl(MediaPackageElement mediaPackageElement) {
87 if (mediaPackageElement == null) {
88 throw new IllegalArgumentException("Parameter media package element must not be null");
89 }
90 this.type = mediaPackageElement.getElementType().toString().toLowerCase();
91 this.identifier = mediaPackageElement.getIdentifier();
92 if (identifier == null) {
93 throw new IllegalArgumentException("Media package element must have an identifier");
94 }
95 this.properties = new HashMap<String, String>();
96 }
97
98
99
100
101
102
103
104
105
106 public MediaPackageReferenceImpl(String type, String identifier) {
107 if (type == null) {
108 throw new IllegalArgumentException("Parameter type must not be null");
109 }
110 if (identifier == null) {
111 throw new IllegalArgumentException("Parameter identifier must not be null");
112 }
113 this.type = type;
114 this.identifier = identifier;
115 this.properties = new HashMap<String, String>();
116 }
117
118
119
120
121
122
123
124
125 public static MediaPackageReference fromString(String reference) throws IllegalArgumentException {
126 if (reference == null) {
127 throw new IllegalArgumentException("Reference is null");
128 }
129
130 MediaPackageReference ref = null;
131
132 String[] parts = reference.split(";");
133 String elementReference = parts[0];
134
135
136 if ("self".equals(elementReference)) {
137 ref = new MediaPackageReferenceImpl(MediaPackageReference.TYPE_MEDIAPACKAGE, "self");
138 } else {
139 String[] elementReferenceParts = elementReference.split(":");
140 if (elementReferenceParts.length != 2) {
141 throw new IllegalArgumentException("Reference " + reference + " is malformed");
142 }
143 ref = new MediaPackageReferenceImpl(elementReferenceParts[0], elementReferenceParts[1]);
144 }
145
146
147 for (int i = 1; i < parts.length; i++) {
148 String[] propertyParts = parts[i].split("=");
149 if (propertyParts.length != 2) {
150 throw new IllegalStateException("malformatted reference properties");
151 }
152 String key = propertyParts[0];
153 String value = propertyParts[1];
154 ref.setProperty(key, value);
155 }
156
157 return ref;
158 }
159
160
161
162
163 public String getIdentifier() {
164 return identifier;
165 }
166
167
168
169
170 public String getType() {
171 return type;
172 }
173
174
175
176
177 public Map<String, String> getProperties() {
178 return properties;
179 }
180
181
182
183
184
185 public void setProperties(Map<String, String> properties) {
186 this.properties = properties;
187 }
188
189
190
191
192
193
194 @Override
195 public String getProperty(String key) {
196 return properties.get(key);
197 }
198
199
200
201
202
203
204 @Override
205 public void setProperty(String key, String value) {
206 if (value == null) {
207 this.properties.remove(key);
208 }
209 this.properties.put(key, value);
210 }
211
212
213
214
215
216 public boolean matches(MediaPackageReference reference) {
217 if (reference == null) {
218 return false;
219 }
220
221
222 if (!type.equals(reference.getType())) {
223 return false;
224 }
225
226
227 if (properties != null && !properties.equals(reference.getProperties())) {
228 return false;
229 } else if (reference.getProperties() != null && !reference.getProperties().equals(properties)) {
230 return false;
231 }
232
233
234 if (identifier.equals(reference.getIdentifier())) {
235 return true;
236 } else if (ANY.equals(identifier) || ANY.equals(reference.getIdentifier())) {
237 return true;
238 } else if (SELF.equals(identifier) || SELF.equals(reference.getIdentifier())) {
239 return true;
240 }
241
242 return false;
243 }
244
245
246
247
248
249
250 @Override
251 public Object clone() {
252 MediaPackageReferenceImpl clone = new MediaPackageReferenceImpl(type, identifier);
253 clone.getProperties().putAll(properties);
254 return clone;
255 }
256
257
258
259
260 @Override
261 public int hashCode() {
262 return toString().hashCode();
263 }
264
265
266
267
268 @Override
269 public boolean equals(Object obj) {
270 if (obj == null || !(obj instanceof MediaPackageReference)) {
271 return false;
272 }
273 MediaPackageReference ref = (MediaPackageReference) obj;
274 return type.equals(ref.getType()) && identifier.equals(ref.getIdentifier());
275 }
276
277
278
279
280 @Override
281 public String toString() {
282 if (externalForm == null) {
283 StringBuffer buf = new StringBuffer();
284 if (TYPE_MEDIAPACKAGE.equals(type) && SELF.equals(identifier)) {
285 buf.append("self");
286 } else {
287 buf.append(type);
288 buf.append(":");
289 buf.append(identifier);
290 }
291 if (properties.size() > 0) {
292 for (Map.Entry<String, String> entry : properties.entrySet()) {
293 buf.append(";");
294 buf.append(entry.getKey());
295 buf.append("=");
296 buf.append(entry.getValue());
297 }
298 }
299 externalForm = buf.toString();
300 }
301 return externalForm;
302 }
303
304 public static class Adapter extends XmlAdapter<String, MediaPackageReference> {
305 @Override
306 public String marshal(MediaPackageReference ref) throws Exception {
307 if (ref == null) {
308 return null;
309 }
310 return ref.toString();
311 }
312
313 @Override
314 public MediaPackageReference unmarshal(String ref) throws Exception {
315 if (ref == null) {
316 return null;
317 }
318 return MediaPackageReferenceImpl.fromString(ref);
319 }
320 }
321 }