001: /*
002: * $Id: TimeType.java,v 1.8 2005/08/24 00:37:25 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.types;
042:
043: import java.io.DataInput;
044: import java.io.DataOutput;
045: import java.io.IOException;
046: import java.math.BigDecimal;
047: import java.sql.Date;
048: import java.sql.Time;
049: import java.sql.Timestamp;
050: import java.text.DateFormat;
051: import java.text.ParsePosition;
052: import java.text.SimpleDateFormat;
053: import java.util.TimeZone;
054:
055: import org.axiondb.AxionException;
056: import org.axiondb.DataType;
057:
058: /**
059: * Implements a date type which can generate instances of java.sql.Date and other JDBC
060: * date-related types.
061: *
062: * @author Jonathan Giron
063: * @author Ahimanikya Satapathy
064: * @version $Revision: 1.8 $
065: */
066: public class TimeType extends TimestampType {
067: /* Increment to use in computing a successor value. */
068: static final long INCREMENT_MS = 1;
069: static final long serialVersionUID = -1850471442550601883L;
070:
071: private static final Object LOCK_TIME_PARSING = new Object();
072: private static final Object LOCK_TIME_FORMATTING = new Object();
073:
074: // DateFormat objects are not thread safe. Do not share across threads w/o synch block.
075: private static final DateFormat[] TIME_PARSING_FORMATS = new DateFormat[] {
076: new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", LOCALE),
077: new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", LOCALE),
078: new SimpleDateFormat("yyyy-MM-dd", LOCALE),
079: new SimpleDateFormat("MM-dd-yyyy", LOCALE),
080: new SimpleDateFormat("HH:mm:ss", LOCALE),
081: DateFormat.getTimeInstance(DateFormat.SHORT, LOCALE) };
082: private static final DateFormat ISO_TIME_FORMAT = new SimpleDateFormat(
083: "HH:mm:ss");
084:
085: static {
086: for (int i = 0; i < TIME_PARSING_FORMATS.length; i++) {
087: TIME_PARSING_FORMATS[i].setLenient(false);
088: }
089: }
090:
091: private static TimeZone TIME_ZONE = TimeZone.getDefault();
092:
093: public static TimeZone getTimeZone() {
094: return TIME_ZONE;
095: }
096:
097: public static long normalizeTime(long rawTimeMillis) {
098: int dstOffset = (TIME_ZONE.inDaylightTime(new java.util.Date(
099: rawTimeMillis))) ? TIME_ZONE.getDSTSavings() : 0;
100: return (rawTimeMillis < DateType.INCREMENT_DAY) ? rawTimeMillis
101: : (rawTimeMillis % DateType.INCREMENT_DAY) + dstOffset;
102: }
103:
104: /**
105: * @see org.axiondb.DataType#accepts(java.lang.Object)
106: */
107: public boolean accepts(Object value) {
108: if (null == value) {
109: return true;
110: } else if (value instanceof Number) {
111: return true;
112: } else if (value instanceof String) {
113: return true;
114: } else if (value instanceof java.util.Date) {
115: return true;
116: } else if (value instanceof Date) {
117: return true;
118: } else if (value instanceof Time) {
119: return true;
120: } else {
121: return false;
122: }
123: }
124:
125: private Time getNormalizedTime(long time) {
126: Time ret = null;
127: ret = new Time(normalizeTime(time));
128: return ret;
129: }
130:
131: private Time convertToTime(Object value) throws AxionException {
132: if (null == value) {
133: return null;
134: } else if (value instanceof Number) {
135: return getNormalizedTime(((Number) value).longValue());
136: } else if (value instanceof Timestamp) {
137: return getNormalizedTime(((Timestamp) value).getTime());
138: } else if (value instanceof java.sql.Date) {
139: return getNormalizedTime(((java.sql.Date) value).getTime());
140: } else if (value instanceof java.sql.Time) {
141: return (Time) value;
142: } else if (value instanceof java.util.Date) {
143: return getNormalizedTime(((java.util.Date) value).getTime());
144: } else if (value instanceof String) {
145: java.util.Date dVal = null;
146: int i = 0;
147: while (dVal == null && i < TIME_PARSING_FORMATS.length) {
148: synchronized (LOCK_TIME_PARSING) {
149: dVal = TIME_PARSING_FORMATS[i].parse(
150: (String) value, new ParsePosition(0));
151: }
152: i++;
153: }
154:
155: if (dVal == null) {
156: throw new AxionException(22007);
157: }
158: return getNormalizedTime(dVal.getTime());
159: } else {
160: throw new AxionException(22007);
161: }
162: }
163:
164: /**
165: * Returns a <tt>java.sql.Date</tt> converted from the given <i>value </i>, or
166: * throws {@link AxionException}if the given <i>value </i> isn't
167: * {@link #accepts acceptable}.
168: */
169: public Object convert(Object value) throws AxionException {
170: if (value instanceof Time) {
171: return value;
172: }
173: return convertToTime(value);
174: }
175:
176: /**
177: * @see org.axiondb.DataType#getColumnDisplaySize()
178: */
179: public int getColumnDisplaySize() {
180: return 8;
181: }
182:
183: public int getPrecision() {
184: return 8;
185: }
186:
187: /**
188: * @see org.axiondb.DataType#getJdbcType()
189: */
190: public int getJdbcType() {
191: return java.sql.Types.TIME;
192: }
193:
194: /**
195: * @see org.axiondb.DataTypeFactory#makeNewInstance()
196: */
197: public DataType makeNewInstance() {
198: return new TimeType();
199: }
200:
201: /**
202: * @see org.axiondb.DataType#successor(java.lang.Object)
203: */
204: public Object successor(Object value)
205: throws IllegalArgumentException {
206: try {
207: return new Time(((Time) convert(value)).getTime()
208: + INCREMENT_MS);
209: } catch (AxionException e) {
210: throw new IllegalArgumentException(e.getMessage());
211: }
212: }
213:
214: /**
215: * @see org.axiondb.DataType#supportsSuccessor()
216: */
217: public boolean supportsSuccessor() {
218: return true;
219: }
220:
221: /**
222: * @see org.axiondb.DataType#toBigDecimal(java.lang.Object)
223: */
224: public BigDecimal toBigDecimal(Object value) throws AxionException {
225: Date dval = (Date) convert(value);
226: Long time = new Long(dval.getTime());
227:
228: return super .toBigDecimal(time);
229: }
230:
231: /**
232: * @see org.axiondb.DataType#toDate(java.lang.Object)
233: */
234: public Date toDate(Object value) throws AxionException {
235: throw new AxionException("Can't convert " + toString()
236: + " to Date.");
237: }
238:
239: /**
240: * @see java.lang.Object#toString()
241: */
242: public String toString() {
243: return "time";
244: }
245:
246: /**
247: * @see org.axiondb.DataType#toString(java.lang.Object)
248: */
249: public String toString(Object value) throws AxionException {
250: Time tval = toTime(value);
251: if (null == tval) {
252: return null;
253: }
254:
255: synchronized (LOCK_TIME_FORMATTING) {
256: return ISO_TIME_FORMAT.format(tval).toString();
257: }
258: }
259:
260: /**
261: * @see org.axiondb.DataType#toTime(java.lang.Object)
262: */
263: public Time toTime(Object value) throws AxionException {
264: return (value instanceof Time) ? (Time) value
265: : convertToTime(value);
266: }
267:
268: /**
269: * @see org.axiondb.DataType#toTimestamp(java.lang.Object)
270: */
271: public Timestamp toTimestamp(Object value) throws AxionException {
272: return new Timestamp(convertToTime(value).getTime());
273: }
274:
275: /**
276: * Overrides parent implementation to read only milliseconds (as a long) from the
277: * input stream, ignoring any nanoseconds written by TimestampType.write(). We read
278: * TimeType data assuming that they are in the same form as that of TimestampType data
279: * in order to preserve backwards compatibility, as java.sql.Time was originally
280: * mapped to {@link org.axiondb.types.TimestampType}.
281: *
282: * @param value Time object (typically a <code>java.sql.Time</code> or other
283: * convertible form) to be unpersisted
284: * @param out DataOutput to supply serialized data
285: * @throws IOException if error occurs during read
286: * @see org.axiondb.DataType#read
287: */
288: public Object read(DataInput in) throws IOException {
289: Time result = null;
290: int nanos = in.readInt();
291: if (Integer.MIN_VALUE != nanos) {
292: long millis = in.readLong();
293: result = new Time(millis);
294: }
295: return result;
296: }
297:
298: /**
299: * Overrides parent implementation to always write time (in milliseconds) as a long,
300: * writing a placeholder zero for the nanosecond field usually written by
301: * TimestampType.write(). We write TimeType data in the same form as that of
302: * TimestampType data to preserve backwards compatibility, as java.sql.Date was
303: * originally mapped to {@link org.axiondb.types.TimestampType}.
304: *
305: * @param value time object (typically a <code>java.sql.Time</code> or other
306: * convertible form) to be persisted
307: * @param out DataOutput to receive serialized data
308: * @throws IOException if error occurs during write
309: * @see org.axiondb.DataType#write
310: */
311: public void write(Object value, DataOutput out) throws IOException {
312: if (null == value) {
313: out.writeInt(Integer.MIN_VALUE);
314: } else {
315: try {
316: Time val = (Time) convert(value);
317: out.writeInt(0);
318: out.writeLong(val.getTime());
319: } catch (AxionException e) {
320: throw new IOException(e.getMessage());
321: }
322: }
323: }
324:
325: public static void setTimeZone(String id) {
326: TimeZone tZone = TimeZone.getTimeZone(id);
327: if (tZone != null) {
328: synchronized (LOCK_TIME_PARSING) {
329: TIME_ZONE = tZone;
330: for (int i = 0; i < TIME_PARSING_FORMATS.length; i++) {
331: TIME_PARSING_FORMATS[i].setTimeZone(tZone);
332: }
333: }
334:
335: synchronized (LOCK_TIME_FORMATTING) {
336: ISO_TIME_FORMAT.setTimeZone(tZone);
337: }
338: }
339: }
340:
341: }
|