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
95 if (part.contains(SEPARATOR)) {
96 throw new IllegalArgumentException(
97 format("Invalid flavor part \"%s\". Flavor parts may not contain '%s'!", part, SEPARATOR));
98 }
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
109 return adaptedPart;
110 }
111
112
113 public static MediaPackageElementFlavor flavor(String type, String subtype) {
114 return new MediaPackageElementFlavor(type, subtype);
115 }
116
117
118
119
120
121
122
123
124
125
126 public String getType() {
127 return type;
128 }
129
130
131
132
133
134
135
136
137
138
139 public String getSubtype() {
140 return subtype;
141 }
142
143
144
145
146
147
148
149
150
151
152 public MediaPackageElementFlavor applyTo(MediaPackageElementFlavor target) {
153 String type = this.type;
154 String subtype = this.subtype;
155 if (WILDCARD.equals(this.type)) {
156 type = target.getType();
157 }
158 if (WILDCARD.equals(this.subtype)) {
159 subtype = target.getSubtype();
160 }
161 return new MediaPackageElementFlavor(type, subtype);
162 }
163
164
165
166
167 @Override
168 public MediaPackageElementFlavor clone() throws CloneNotSupportedException {
169 MediaPackageElementFlavor m = (MediaPackageElementFlavor) super.clone();
170 m.type = this.type;
171 m.subtype = this.subtype;
172 return m;
173 }
174
175
176
177
178
179
180
181 public boolean eq(String flavor) {
182 return flavor != null && flavor.equals(toString());
183 }
184
185
186
187
188 @Override
189 public int compareTo(MediaPackageElementFlavor m) {
190 return toString().compareTo(m.toString());
191 }
192
193
194
195
196 @Override
197 public String toString() {
198 return type + SEPARATOR + subtype;
199 }
200
201
202
203
204
205
206
207
208
209
210 public static MediaPackageElementFlavor parseFlavor(String s) throws IllegalArgumentException {
211 if (s == null) {
212 throw new IllegalArgumentException("Unable to create element flavor from 'null'");
213 }
214 String[] parts = s.split(SEPARATOR);
215 if (parts.length != 2) {
216 throw new IllegalArgumentException(format("Unable to create element flavor from \"%s\"", s));
217 }
218 return new MediaPackageElementFlavor(parts[0], parts[1]);
219 }
220
221
222
223
224 static class FlavorAdapter extends XmlAdapter<String, MediaPackageElementFlavor> {
225 @Override
226 public String marshal(MediaPackageElementFlavor flavor) throws Exception {
227 if (flavor == null) {
228 return null;
229 }
230 return flavor.toString();
231 }
232
233 @Override
234 public MediaPackageElementFlavor unmarshal(String str) throws Exception {
235 return parseFlavor(str);
236 }
237 }
238
239
240
241
242
243 private static boolean typeMatches(String a, String b) {
244 return a.equals(b) || WILDCARD.equals(a) || WILDCARD.equals(b);
245 }
246
247
248
249
250
251
252
253
254
255 public boolean matches(MediaPackageElementFlavor other) {
256 return other != null
257 && typeMatches(type, other.type)
258 && typeMatches(subtype, other.subtype);
259 }
260
261 @Override
262 public int hashCode() {
263 return Objects.hash(type, subtype);
264 }
265
266 @Override
267 public boolean equals(Object other) {
268 return (other instanceof MediaPackageElementFlavor)
269 && type.equals(((MediaPackageElementFlavor) other).type)
270 && subtype.equals(((MediaPackageElementFlavor) other).subtype);
271 }
272
273 }