1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.mediapackage;
23
24 import static java.lang.String.format;
25
26 import java.io.Serializable;
27 import java.util.Objects;
28
29 import javax.xml.bind.annotation.adapters.XmlAdapter;
30 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
31
32
33
34
35
36
37 @XmlJavaTypeAdapter(MediaPackageElementFlavor.FlavorAdapter.class)
38 public class MediaPackageElementFlavor implements Cloneable, Comparable<MediaPackageElementFlavor>, Serializable {
39
40
41
42
43 public static final String WILDCARD = "*";
44
45
46
47
48 private static final long serialVersionUID = 1L;
49
50
51
52
53 public static final String SEPARATOR = "/";
54
55
56
57
58 private String type = null;
59
60
61
62
63 private String subtype = null;
64
65
66 private MediaPackageElementFlavor() {
67 }
68
69
70
71
72
73
74
75
76
77 public MediaPackageElementFlavor(String type, String subtype) {
78 this.type = checkPartSyntax(type);
79 this.subtype = checkPartSyntax(subtype);
80 }
81
82
83
84
85
86
87
88 private String checkPartSyntax(String part) {
89
90 if (part == null)
91 throw new IllegalArgumentException("Flavor parts may not be null!");
92
93
94 if (part.contains(SEPARATOR))
95 throw new IllegalArgumentException(
96 format("Invalid flavor part \"%s\". Flavor parts may not contain '%s'!", part, SEPARATOR));
97
98
99 String adaptedPart = part.trim().toLowerCase();
100
101
102 if (adaptedPart.isEmpty())
103 throw new IllegalArgumentException(
104 format("Invalid flavor part \"%s\". Flavor parts may not be blank or empty!", part));
105
106 return adaptedPart;
107 }
108
109
110 public static MediaPackageElementFlavor flavor(String type, String subtype) {
111 return new MediaPackageElementFlavor(type, subtype);
112 }
113
114
115
116
117
118
119
120
121
122
123 public String getType() {
124 return type;
125 }
126
127
128
129
130
131
132
133
134
135
136 public String getSubtype() {
137 return subtype;
138 }
139
140
141
142
143
144
145
146
147
148
149 public MediaPackageElementFlavor applyTo(MediaPackageElementFlavor target) {
150 String type = this.type;
151 String subtype = this.subtype;
152 if (WILDCARD.equals(this.type)) {
153 type = target.getType();
154 }
155 if (WILDCARD.equals(this.subtype)) {
156 subtype = target.getSubtype();
157 }
158 return new MediaPackageElementFlavor(type, subtype);
159 }
160
161
162
163
164 @Override
165 public MediaPackageElementFlavor clone() throws CloneNotSupportedException {
166 MediaPackageElementFlavor m = (MediaPackageElementFlavor) super.clone();
167 m.type = this.type;
168 m.subtype = this.subtype;
169 return m;
170 }
171
172
173
174
175
176
177
178 public boolean eq(String flavor) {
179 return flavor != null && flavor.equals(toString());
180 }
181
182
183
184
185 @Override
186 public int compareTo(MediaPackageElementFlavor m) {
187 return toString().compareTo(m.toString());
188 }
189
190
191
192
193 @Override
194 public String toString() {
195 return type + SEPARATOR + subtype;
196 }
197
198
199
200
201
202
203
204
205
206
207 public static MediaPackageElementFlavor parseFlavor(String s) throws IllegalArgumentException {
208 if (s == null)
209 throw new IllegalArgumentException("Unable to create element flavor from 'null'");
210 String[] parts = s.split(SEPARATOR);
211 if (parts.length != 2)
212 throw new IllegalArgumentException(format("Unable to create element flavor from \"%s\"", s));
213 return new MediaPackageElementFlavor(parts[0], parts[1]);
214 }
215
216
217
218
219 static class FlavorAdapter extends XmlAdapter<String, MediaPackageElementFlavor> {
220 @Override
221 public String marshal(MediaPackageElementFlavor flavor) throws Exception {
222 if (flavor == null) {
223 return null;
224 }
225 return flavor.toString();
226 }
227
228 @Override
229 public MediaPackageElementFlavor unmarshal(String str) throws Exception {
230 return parseFlavor(str);
231 }
232 }
233
234
235
236
237
238 private static boolean typeMatches(String a, String b) {
239 return a.equals(b) || WILDCARD.equals(a) || WILDCARD.equals(b);
240 }
241
242
243
244
245
246
247
248
249
250 public boolean matches(MediaPackageElementFlavor other) {
251 return other != null
252 && typeMatches(type, other.type)
253 && typeMatches(subtype, other.subtype);
254 }
255
256 @Override
257 public int hashCode() {
258 return Objects.hash(type, subtype);
259 }
260
261 @Override
262 public boolean equals(Object other) {
263 return (other instanceof MediaPackageElementFlavor)
264 && type.equals(((MediaPackageElementFlavor) other).type)
265 && subtype.equals(((MediaPackageElementFlavor) other).subtype);
266 }
267
268 }