View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
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   * Implementation of a relative <code>TimePoint</code>.
33   */
34  public class MediaRelTimePointImpl extends MediaTimePointImpl implements MediaTimePoint {
35  
36    /** Time delimiter */
37    private static final String TimeSpecDelimiter = "T";
38  
39    /** Fraction delimiter */
40    private static final String FractionSpecDelimiter = "F";
41  
42    /** Time separator */
43    private static final String TimeSeparator = ":";
44  
45    /** Number of milliseconds per second */
46    private static final long MS_PER_SECOND = 1000L;
47  
48    /** Number of milliseconds per minute */
49    private static final long MS_PER_MINUTE = 60000L;
50  
51    /** Number of milliseconds per hour */
52    private static final long MS_PER_HOUR = 3600000L;
53  
54    /** Number of milliseconds per day */
55    private static final long MS_PER_DAY = 86400000L;
56  
57    /**
58     * Creates a relative time point at <code>T00:00:00:0F1000</code>.
59     */
60    public MediaRelTimePointImpl() {
61      super();
62    }
63  
64    /**
65     * @param milliseconds
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     * @param hour
79     *          the number of hours
80     * @param minute
81     *          the number of minutes
82     * @param second
83     *          the number of seconds
84     * @param fraction
85     *          the number of fractions
86     * @param fractionsPerSecond
87     *          the number of fractions per second
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     * Creates a relative timepoint representation from the given time point string.
99     *
100    * @param text
101    *          the timepoint text representation
102    * @return the the timepoint
103    * @throws IllegalArgumentException
104    *           if <code>text</code> is malformed
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    * Parses a timepoint string.
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    * Parses the fractions of a time point.
131    *
132    * @param fractions
133    *          the fractions
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    * {@inheritDoc}
142    *
143    * @see java.lang.Object#hashCode()
144    */
145   @Override
146   public int hashCode() {
147     return super.hashCode();
148   }
149 
150   /**
151    * {@inheritDoc}
152    *
153    * @see java.lang.Object#equals(java.lang.Object)
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    * @see java.lang.Object#toString()
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    * @see org.opencastproject.mediapackage.XmlElement#toXml(org.w3c.dom.Document)
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 }