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 }
96 this.hours = h;
97 }
98
99 /**
100 * Checks if minutes are inside the boundaries (between 0 and 59).
101 *
102 * @param m
103 * number of minutes
104 * @throws IllegalTimeFormatException
105 * if argument is less than 0 or more than 59.
106 */
107 private void setMinutes(int m) throws IllegalTimeFormatException {
108 if (m < 0 || m > 59) {
109 throw new IllegalTimeFormatException("Invalid minute time: " + m);
110 }
111 this.minutes = m;
112 }
113
114 /**
115 * Checks if seconds are inside the boundaries (between 0 and 59).
116 *
117 * @param s
118 * number of seconds
119 * @throws IllegalTimeFormatException
120 * if argument is less than 0 or more than 59.
121 */
122 private void setSeconds(int s) throws IllegalTimeFormatException {
123 if (s < 0 || s > 59) {
124 throw new IllegalTimeFormatException("Invalid second time: " + s);
125 }
126 this.seconds = s;
127 }
128
129 /**
130 * Checks if milliseconds are inside the boundaries (between 0 and 999).
131 *
132 * @param ms
133 * number of milliseconds
134 * @throws IllegalTimeFormatException
135 * if argument is less than 0 or more than 999.
136 */
137 private void setMilliseconds(int ms) throws IllegalTimeFormatException {
138 if (ms < 0 || ms > 999) {
139 throw new IllegalTimeFormatException("Invalid milisecond time: " + ms);
140 }
141 this.milliseconds = ms;
142 }
143
144 /**
145 * @see java.lang.Comparable#compareTo(java.lang.Object)
146 */
147 @Override
148 public int compareTo(Time arg0) {
149 return getMilliseconds(this) - getMilliseconds(arg0);
150 }
151
152 /**
153 * Helper function that converts time to milliseconds. Used for time comparing.
154 *
155 * @param time
156 * to be converted
157 * @return milliseconds
158 */
159 private static int getMilliseconds(Time time) {
160 return (time.getHours() * 3600 + time.getMinutes() * 60 + time.getSeconds()) * 1000 + time.getMilliseconds();
161 }
162 }