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.metadata.mpeg7;
24
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27 import org.w3c.dom.Node;
28
29 import java.util.ArrayList;
30 import java.util.Comparator;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.SortedSet;
34 import java.util.TreeSet;
35
36
37
38
39 public class SegmentImpl implements Segment, AudioSegment, VideoSegment, AudioVisualSegment {
40
41
42 protected Segment.Type type = null;
43
44
45 protected String id = null;
46
47
48 protected MediaTime mediaTime = null;
49
50
51 protected List<TextAnnotation> annotations = null;
52
53
54 protected SpatioTemporalDecomposition spatioTemporalDecomposition = null;
55
56
57
58
59
60
61
62
63
64 public SegmentImpl(Segment.Type type, String id) {
65 this.type = type;
66 this.id = id;
67 annotations = new ArrayList<TextAnnotation>();
68 }
69
70
71
72
73 public String getIdentifier() {
74 return id;
75 }
76
77
78
79
80 public void setMediaTime(MediaTime mediaTime) {
81 this.mediaTime = mediaTime;
82 }
83
84
85
86
87 public MediaTime getMediaTime() {
88 return mediaTime;
89 }
90
91
92
93
94 public boolean hasTextAnnotations() {
95 return annotations.size() > 0;
96 }
97
98
99
100
101 public boolean hasTextAnnotations(String language) {
102 return hasTextAnnotations(0.0f, 0.0f, language);
103 }
104
105
106
107
108 public boolean hasTextAnnotations(float relevance, float confidence) {
109 return hasTextAnnotations(relevance, confidence, null);
110 }
111
112
113
114
115 public boolean hasTextAnnotations(float relevance, float confidence, String language) {
116 for (TextAnnotation annotation : annotations) {
117 if (annotation.getRelevance() >= relevance && annotation.getConfidence() >= confidence) {
118 if (language != null) {
119 if (language.equals(annotation.getLanguage())) {
120 return true;
121 }
122 } else {
123 return true;
124 }
125 }
126 }
127 return false;
128 }
129
130
131
132
133 public int getTextAnnotationCount() {
134 return annotations.size();
135 }
136
137
138
139
140 public Iterator<TextAnnotation> textAnnotationsByConfidence() {
141 SortedSet<TextAnnotation> set = new TreeSet<TextAnnotation>(new Comparator<TextAnnotation>() {
142 public int compare(TextAnnotation a1, TextAnnotation a2) {
143 if (a1.getConfidence() > a2.getConfidence()) {
144 return 1;
145 } else if (a1.getConfidence() > a2.getConfidence()) {
146 return -1;
147 }
148 return 0;
149 }
150 });
151 set.addAll(annotations);
152 return set.iterator();
153 }
154
155
156
157
158 public Iterator<TextAnnotation> textAnnotationsByRelevance() {
159 SortedSet<TextAnnotation> set = new TreeSet<TextAnnotation>(new Comparator<TextAnnotation>() {
160 public int compare(TextAnnotation a1, TextAnnotation a2) {
161 if (a1.getRelevance() > a2.getRelevance()) {
162 return 1;
163 } else if (a1.getRelevance() > a2.getRelevance()) {
164 return -1;
165 }
166 return 0;
167 }
168 });
169 set.addAll(annotations);
170 return set.iterator();
171 }
172
173
174
175
176 public TextAnnotation createTextAnnotation(float relevance, float confidence, String language) {
177 TextAnnotationImpl annotation = new TextAnnotationImpl(relevance, confidence, language);
178 annotations.add(annotation);
179 return annotation;
180 }
181
182
183
184
185 public Iterator<TextAnnotation> textAnnotations() {
186 return annotations.iterator();
187 }
188
189
190
191
192
193
194 @Override
195 public SpatioTemporalDecomposition createSpatioTemporalDecomposition(boolean gap, boolean overlap)
196 throws IllegalStateException {
197 if (spatioTemporalDecomposition != null) {
198 throw new IllegalStateException("A spatio temporal decomposition has already been created");
199 }
200 spatioTemporalDecomposition = new SpatioTemporalDecompositionImpl(true, false);
201 return spatioTemporalDecomposition;
202 }
203
204
205
206
207
208
209 @Override
210 public SpatioTemporalDecomposition getSpatioTemporalDecomposition() {
211 return spatioTemporalDecomposition;
212 }
213
214
215
216
217
218
219 @Override
220 public boolean hasSpatioTemporalDecomposition() {
221 return spatioTemporalDecomposition != null;
222 }
223
224
225
226
227 @Override
228 public int hashCode() {
229 return id.hashCode();
230 }
231
232
233
234
235 @Override
236 public boolean equals(Object obj) {
237 if (obj instanceof Segment) {
238 return id.equals(((Segment) obj).getIdentifier());
239 }
240 return super.equals(obj);
241 }
242
243
244
245
246 public Node toXml(Document document) {
247 Element node = document.createElement(type.toString());
248 node.setAttribute("id", id);
249 node.appendChild(mediaTime.toXml(document));
250 if (spatioTemporalDecomposition != null) {
251 node.appendChild(spatioTemporalDecomposition.toXml(document));
252 }
253 for (TextAnnotation annotation : annotations) {
254 node.appendChild(annotation.toXml(document));
255 }
256 return node;
257 }
258
259 }