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