001: /*
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 2001 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: Time.java 6565 2006-12-22 16:05:26Z ekuns $
044: * Date Author Changes
045: * 10/02/2002 Arnaud Added support for 3 figure millisecond and clean up
046: * 04/18/2002 Arnaud String Constructor
047: * 05/22/2001 Arnaud Blandin Created
048: */
049: package org.exolab.castor.types;
050:
051: import java.text.SimpleDateFormat;
052: import java.text.ParseException;
053: import java.util.Date;
054:
055: /**
056: * Describes an XML schema Time.
057: * <p>
058: * The format is defined by W3C XML Schema Recommendation and ISO8601 i.e
059: * <tt>hh:mm:ss.sss(Z|(+|-)hh:mm)</tt>
060: * <p>
061: * Deep support of fractional seconds is not implemented. This implementation
062: * only supports fractional second resolution of milliseconds.
063: *
064: * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
065: * @author <a href="mailto:edward.kuns@aspect.com">Edward Kuns</a>
066: * @version $Revision: 6565 $
067: * @see DateTimeBase
068: */
069: public class Time extends DateTimeBase {
070:
071: /** SerialVersionUID */
072: private static final long serialVersionUID = -8268707778437931489L;
073:
074: /** The Time Format used by the toDate() method */
075: private static final String TIME_FORMAT_MILLI = "HH:mm:ss.SSS";
076: private static final String TIME_FORMAT_NO_MILLI = "HH:mm:ss";
077:
078: private static final String BAD_TIME = "Bad Time format: ";
079:
080: /**
081: * No-arg constructor.
082: */
083: public Time() {
084: // Nothing to do
085: }
086:
087: /**
088: * Constructs a XML Schema Time instance given all the values of
089: * the different fields.
090: * By default a Time is not UTC and is local.
091: * @param values an array of shorts that represent the different fields of Time.
092: */
093: public Time(short[] values) {
094: setValues(values);
095: }
096:
097: /**
098: * Constructs a XML Schema Time instance given a long representing the time in milliseconds.
099: * By default a Time is not UTC and is local.
100: * @param l The long value that represents the time instance.
101: */
102: public Time(long l) {
103: if (l > 86400000L) {
104: throw new IllegalArgumentException(
105: "Bad Time: the long value can't represent more than 24h.");
106: }
107: this .setHour((short) (l / 3600000));
108: l = l % 3600000;
109: this .setMinute((short) (l / 60000));
110: l = l % 60000;
111: this .setSecond((short) (l / 1000), (short) (l % 1000));
112: }
113:
114: /**
115: * Constructs a Time given a string representation.
116: * @param time the string representation of the Time to instantiate
117: * @throws ParseException a parse exception is thrown if the string to parse
118: * does not follow the rigth format (see the description
119: * of this class)
120: */
121: public Time(String time) throws ParseException {
122: parseTimeInternal(time, this );
123: }
124:
125: /**
126: * Sets all the fields by reading the values in an array.
127: * <p>
128: * if a Time Zone is specified it has to be set by using
129: * {@link DateTimeBase#setZone(short, short) setZone}.
130: *
131: * @param values
132: * an array of shorts with the values the array is supposed to be
133: * of length 4 and ordered like that:
134: * <ul>
135: * <li>hour</li>
136: * <li>minute</li>
137: * <li>second</li>
138: * <li>millisecond</li>
139: * </ul>
140: */
141: public void setValues(short[] values) {
142: if (values.length != 4) {
143: throw new IllegalArgumentException(
144: "Time#setValues: not the right number of values");
145: }
146: this .setHour(values[0]);
147: this .setMinute(values[1]);
148: this .setSecond(values[2], values[3]);
149: }
150:
151: /**
152: * returns an array of short with all the fields that describe
153: * this time type.
154: * <p>Note:the time zone is not included.
155: * @return an array of short with all the fields that describe
156: * this time type.
157: */
158: public short[] getValues() {
159: short[] result = new short[4];
160: result[0] = this .getHour();
161: result[1] = this .getMinute();
162: result[2] = this .getSeconds();
163: result[3] = this .getMilli();
164: return result;
165: } //getValues
166:
167: /**
168: * converts this Time into a local java Date.
169: * @return a local date representing this Time
170: */
171: public Date toDate() {
172: Date date = null;
173: SimpleDateFormat df = null;
174: String temp = this .toString();
175: if (temp.indexOf('.') > 0) {
176: df = new SimpleDateFormat(TIME_FORMAT_MILLI);
177: } else {
178: df = new SimpleDateFormat(TIME_FORMAT_NO_MILLI);
179: }
180:
181: setDateFormatTimeZone(df);
182:
183: try {
184: date = df.parse(temp);
185: } catch (ParseException e) {
186: //this can't happen since toString() should return the proper string format
187: e.printStackTrace();
188: return null;
189: }
190: return date;
191: } //toDate()
192:
193: /**
194: * convert this Time to a string
195: * The format is defined by W3C XML Schema Recommendation and ISO8601
196: * i.e hh:mm:ss.sss(Z|(+|-)hh:mm)
197: * @return a string representing this Time
198: */
199: public String toString() {
200: StringBuffer result = new StringBuffer();
201: appendTimeString(result);
202: appendTimeZoneString(result);
203: return result.toString();
204: } //toString
205:
206: /**
207: * parses a String and converts it into a java.lang.Object
208: * @param str the string to parse
209: * @return the java.lang.Object represented by the string
210: * @throws ParseException a parse exception is thrown if the string to parse
211: * does not follow the rigth format (see the description
212: * of this class)
213: */
214: public static Object parse(String str) throws ParseException {
215: return parseTime(str);
216: }
217:
218: /**
219: * parses a String and converts it into a Time.
220: * @param str the string to parse
221: * @return the Time represented by the string
222: * @throws ParseException a parse exception is thrown if the string to parse
223: * does not follow the rigth format (see the description
224: * of this class)
225: */
226: public static Time parseTime(String str) throws ParseException {
227: return parseTimeInternal(str, null);
228: }
229:
230: private static Time parseTimeInternal(String str, Time result)
231: throws ParseException {
232: if (str == null) {
233: throw new IllegalArgumentException(
234: "The string to be parsed must not be null.");
235: }
236:
237: if (result == null) {
238: result = new Time();
239: }
240:
241: char[] chars = str.toCharArray();
242: int idx = parseTime(str, result, chars, 0, BAD_TIME);
243: parseTimeZone(str, result, chars, idx, BAD_TIME);
244:
245: return result;
246: } //parse
247:
248: /////////////////////////// DISALLOWED METHODS ///////////////////////////
249:
250: public boolean hasIsNegative() {
251: return false;
252: }
253:
254: public boolean isNegative() {
255: String err = "org.exolab.castor.types.Time does not have a 'negative' field.";
256: throw new OperationNotSupportedException(err);
257: }
258:
259: public void setNegative() {
260: String err = "org.exolab.castor.types.Time cannot be negative.";
261: throw new OperationNotSupportedException(err);
262: }
263:
264: public boolean hasCentury() {
265: return false;
266: }
267:
268: public short getCentury() {
269: String err = "org.exolab.castor.types.Time does not have a Century field.";
270: throw new OperationNotSupportedException(err);
271: }
272:
273: public void setCentury(short century) {
274: String err = "org.exolab.castor.types.Time does not have a Century field.";
275: throw new OperationNotSupportedException(err);
276: }
277:
278: public boolean hasYear() {
279: return false;
280: }
281:
282: public short getYear() {
283: String err = "org.exolab.castor.types.Time does not have a Year field.";
284: throw new OperationNotSupportedException(err);
285: }
286:
287: public void setYear(short year) {
288: String err = "org.exolab.castor.types.Time does not have a Year field.";
289: throw new OperationNotSupportedException(err);
290: }
291:
292: public boolean hasMonth() {
293: return false;
294: }
295:
296: public short getMonth() {
297: String err = "org.exolab.castor.types.Time does not have a Month field.";
298: throw new OperationNotSupportedException(err);
299: }
300:
301: public void setMonth(short month) {
302: String err = "org.exolab.castor.types.Time does not have a Month field.";
303: throw new OperationNotSupportedException(err);
304: }
305:
306: public boolean hasDay() {
307: return false;
308: }
309:
310: public short getDay() {
311: String err = "org.exolab.castor.types.Time does not have a Day field.";
312: throw new OperationNotSupportedException(err);
313: }
314:
315: public void setDay(short month) {
316: String err = "org.exolab.castor.types.Time does not have a Day field.";
317: throw new OperationNotSupportedException(err);
318: }
319:
320: } //Time
|