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 } else {
122 return true;
123 }
124 }
125 }
126 return false;
127 }
128
129
130
131
132 public int getTextAnnotationCount() {
133 return annotations.size();
134 }
135
136
137
138
139 public Iterator<TextAnnotation> textAnnotationsByConfidence() {
140 SortedSet<TextAnnotation> set = new TreeSet<TextAnnotation>(new Comparator<TextAnnotation>() {
141 public int compare(TextAnnotation a1, TextAnnotation a2) {
142 if (a1.getConfidence() > a2.getConfidence())
143 return 1;
144 else if (a1.getConfidence() > a2.getConfidence())
145 return -1;
146 return 0;
147 }
148 });
149 set.addAll(annotations);
150 return set.iterator();
151 }
152
153
154
155
156 public Iterator<TextAnnotation> textAnnotationsByRelevance() {
157 SortedSet<TextAnnotation> set = new TreeSet<TextAnnotation>(new Comparator<TextAnnotation>() {
158 public int compare(TextAnnotation a1, TextAnnotation a2) {
159 if (a1.getRelevance() > a2.getRelevance())
160 return 1;
161 else if (a1.getRelevance() > a2.getRelevance())
162 return -1;
163 return 0;
164 }
165 });
166 set.addAll(annotations);
167 return set.iterator();
168 }
169
170
171
172
173 public TextAnnotation createTextAnnotation(float relevance, float confidence, String language) {
174 TextAnnotationImpl annotation = new TextAnnotationImpl(relevance, confidence, language);
175 annotations.add(annotation);
176 return annotation;
177 }
178
179
180
181
182 public Iterator<TextAnnotation> textAnnotations() {
183 return annotations.iterator();
184 }
185
186
187
188
189
190
191 @Override
192 public SpatioTemporalDecomposition createSpatioTemporalDecomposition(boolean gap, boolean overlap)
193 throws IllegalStateException {
194 if (spatioTemporalDecomposition != null)
195 throw new IllegalStateException("A spatio temporal decomposition has already been created");
196 spatioTemporalDecomposition = new SpatioTemporalDecompositionImpl(true, false);
197 return spatioTemporalDecomposition;
198 }
199
200
201
202
203
204
205 @Override
206 public SpatioTemporalDecomposition getSpatioTemporalDecomposition() {
207 return spatioTemporalDecomposition;
208 }
209
210
211
212
213
214
215 @Override
216 public boolean hasSpatioTemporalDecomposition() {
217 return spatioTemporalDecomposition != null;
218 }
219
220
221
222
223 @Override
224 public int hashCode() {
225 return id.hashCode();
226 }
227
228
229
230
231 @Override
232 public boolean equals(Object obj) {
233 if (obj instanceof Segment) {
234 return id.equals(((Segment) obj).getIdentifier());
235 }
236 return super.equals(obj);
237 }
238
239
240
241
242 public Node toXml(Document document) {
243 Element node = document.createElement(type.toString());
244 node.setAttribute("id", id);
245 node.appendChild(mediaTime.toXml(document));
246 if (spatioTemporalDecomposition != null)
247 node.appendChild(spatioTemporalDecomposition.toXml(document));
248 for (TextAnnotation annotation : annotations) {
249 node.appendChild(annotation.toXml(document));
250 }
251 return node;
252 }
253
254 }