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.Formatter;
30
31
32
33
34 public class MediaRelTimePointImpl extends MediaTimePointImpl implements MediaTimePoint {
35
36
37 private static final String TimeSpecDelimiter = "T";
38
39
40 private static final String FractionSpecDelimiter = "F";
41
42
43 private static final String TimeSeparator = ":";
44
45
46 private static final long MS_PER_SECOND = 1000L;
47
48
49 private static final long MS_PER_MINUTE = 60000L;
50
51
52 private static final long MS_PER_HOUR = 3600000L;
53
54
55 private static final long MS_PER_DAY = 86400000L;
56
57
58
59
60 public MediaRelTimePointImpl() {
61 super();
62 }
63
64
65
66
67 public MediaRelTimePointImpl(long milliseconds) {
68 fractions = (int) (milliseconds % MS_PER_SECOND);
69 second = (int) ((milliseconds / MS_PER_SECOND) % 60);
70 minute = (int) ((milliseconds / MS_PER_MINUTE) % 60);
71 hour = (int) ((milliseconds / MS_PER_HOUR) % 24);
72 day = (int) (milliseconds / MS_PER_DAY);
73 hour += day * 24;
74 fractionsPerSecond = 1000;
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89 public MediaRelTimePointImpl(int hour, int minute, int second, int fraction, int fractionsPerSecond) {
90 this.fractionsPerSecond = fractionsPerSecond;
91 this.fractions = fraction;
92 this.second = second;
93 this.minute = minute;
94 this.hour = hour;
95 }
96
97
98
99
100
101
102
103
104
105
106 public static MediaRelTimePointImpl parseTimePoint(String text) throws IllegalArgumentException {
107 MediaRelTimePointImpl timePoint = new MediaRelTimePointImpl();
108 timePoint.parse(text);
109 return timePoint;
110 }
111
112
113
114
115 private void parse(String text) throws IllegalArgumentException {
116 String time = null;
117 String fractions = null;
118 time = text.substring(text.indexOf(TimeSpecDelimiter) + 1, text.indexOf(TimeSpecDelimiter) + 9);
119 fractions = text.substring(text.indexOf(TimeSpecDelimiter) + time.length() + 2);
120 if (fractions.contains(TimeSeparator)) {
121 timeZone = fractions.substring(fractions.length() - 6);
122 fractions = fractions.substring(0, fractions.length() - 7);
123 }
124 parseTime(time);
125 parseDate(time);
126 parseFractions(fractions);
127 }
128
129
130
131
132
133
134
135 private void parseFractions(String fractions) {
136 this.fractions = Integer.parseInt(fractions.substring(0, fractions.indexOf(FractionSpecDelimiter)));
137 this.fractionsPerSecond = Integer.parseInt(fractions.substring(fractions.indexOf(FractionSpecDelimiter) + 1));
138 }
139
140
141
142
143
144
145 @Override
146 public int hashCode() {
147 return super.hashCode();
148 }
149
150
151
152
153
154
155 @Override
156 public boolean equals(Object obj) {
157 if (obj instanceof MediaRelTimePointImpl) {
158 return ((MediaRelTimePointImpl) obj).getTimeInMilliseconds() == getTimeInMilliseconds();
159 }
160 return false;
161 }
162
163
164
165
166 @Override
167 public String toString() {
168 Formatter f = new Formatter();
169 String result = null;
170 result = f.format("T%02d:%02d:%02d:%dF%d", hour, minute, second, fractions, fractionsPerSecond).toString();
171 f.close();
172 return result;
173 }
174
175
176
177
178 public Node toXml(Document document) {
179 Element node = null;
180 node = document.createElement("MediaRelTimePoint");
181 node.setTextContent(toString());
182 return node;
183 }
184
185 }