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  package org.opencastproject.caption.impl;
23  
24  import org.opencastproject.caption.api.IllegalTimeFormatException;
25  import org.opencastproject.caption.api.Time;
26  
27  /**
28   * Implementation of {@link Time}.
29   */
30  public class TimeImpl implements Time {
31  
32    private int hours;
33    private int minutes;
34    private int seconds;
35    private int milliseconds;
36  
37    public TimeImpl(int h, int m, int s, int ms) throws IllegalTimeFormatException {
38      this.setHours(h);
39      this.setMinutes(m);
40      this.setSeconds(s);
41      this.setMilliseconds(ms);
42    }
43  
44    /**
45     * {@inheritDoc}
46     *
47     * @see org.opencastproject.caption.api.Time#getHours()
48     */
49    @Override
50    public int getHours() {
51      return this.hours;
52    }
53  
54    /**
55     * {@inheritDoc}
56     *
57     * @see org.opencastproject.caption.api.Time#getMinutes()
58     */
59    @Override
60    public int getMinutes() {
61      return this.minutes;
62    }
63  
64    /**
65     * {@inheritDoc}
66     *
67     * @see org.opencastproject.caption.api.Time#getSeconds()
68     */
69    @Override
70    public int getSeconds() {
71      return this.seconds;
72    }
73  
74    /**
75     * {@inheritDoc}
76     *
77     * @see org.opencastproject.caption.api.Time#getMilliseconds()
78     */
79    @Override
80    public int getMilliseconds() {
81      return this.milliseconds;
82    }
83  
84    /**
85     * Checks if hours are inside the boundaries (between 0 and 99 hours).
86     *
87     * @param h
88     *          number of hours
89     * @throws IllegalTimeFormatException
90     *           if argument is less than 0 or more than 99.
91     */
92    private void setHours(int h) throws IllegalTimeFormatException {
93      if (h < 0 || h > 99)
94        throw new IllegalTimeFormatException("Invalid hour time: " + h);
95      this.hours = h;
96    }
97  
98    /**
99     * Checks if minutes are inside the boundaries (between 0 and 59).
100    *
101    * @param m
102    *          number of minutes
103    * @throws IllegalTimeFormatException
104    *           if argument is less than 0 or more than 59.
105    */
106   private void setMinutes(int m) throws IllegalTimeFormatException {
107     if (m < 0 || m > 59)
108       throw new IllegalTimeFormatException("Invalid minute time: " + m);
109     this.minutes = m;
110   }
111 
112   /**
113    * Checks if seconds are inside the boundaries (between 0 and 59).
114    *
115    * @param s
116    *          number of seconds
117    * @throws IllegalTimeFormatException
118    *           if argument is less than 0 or more than 59.
119    */
120   private void setSeconds(int s) throws IllegalTimeFormatException {
121     if (s < 0 || s > 59)
122       throw new IllegalTimeFormatException("Invalid second time: " + s);
123     this.seconds = s;
124   }
125 
126   /**
127    * Checks if milliseconds are inside the boundaries (between 0 and 999).
128    *
129    * @param ms
130    *          number of milliseconds
131    * @throws IllegalTimeFormatException
132    *           if argument is less than 0 or more than 999.
133    */
134   private void setMilliseconds(int ms) throws IllegalTimeFormatException {
135     if (ms < 0 || ms > 999)
136       throw new IllegalTimeFormatException("Invalid milisecond time: " + ms);
137     this.milliseconds = ms;
138   }
139 
140   /**
141    * @see java.lang.Comparable#compareTo(java.lang.Object)
142    */
143   @Override
144   public int compareTo(Time arg0) {
145     return getMilliseconds(this) - getMilliseconds(arg0);
146   }
147 
148   /**
149    * Helper function that converts time to milliseconds. Used for time comparing.
150    *
151    * @param time
152    *          to be converted
153    * @return milliseconds
154    */
155   private static int getMilliseconds(Time time) {
156     return (time.getHours() * 3600 + time.getMinutes() * 60 + time.getSeconds()) * 1000 + time.getMilliseconds();
157   }
158 }