001: /*
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */
015: package org.griphyn.vdl.annotation;
016:
017: import java.sql.Types;
018: import java.util.Date;
019: import java.text.SimpleDateFormat;
020: import java.sql.Timestamp;
021: import java.text.*;
022:
023: /**
024: * This simple class defines the Date values for the values at the
025: * heart of Yong's annotations.
026: *
027: * @author Jens-S. Vöckler
028: * @author Yong Zhao
029: * @version $Revision: 50 $
030: */
031: public class TupleDate extends Tuple {
032: /**
033: * Defines the key for which we store the annotation.
034: */
035: private Timestamp m_value;
036:
037: /**
038: * Constructs an instance of a Date tuple.
039: *
040: * @param key is the key to store annotations for.
041: * @param value is the Date value of the annotation.
042: */
043: public TupleDate(String key, Timestamp value) {
044: super (key);
045: m_value = value;
046: }
047:
048: /**
049: * Constructs an instance of a Date tuple.
050: *
051: * @param key is the key to store annotations for.
052: * @param value is the Date value of the annotation.
053: */
054: public TupleDate(String key, Date value) {
055: super (key);
056: m_value = new Timestamp(value.getTime());
057: }
058:
059: /**
060: * Constructs an instance of a Date tuple.
061: *
062: * @param key is the key to store annotations for.
063: * @param value is the information in milliseconds since epoch.
064: */
065: public TupleDate(String key, long value) {
066: super (key);
067: m_value = new Timestamp(value);
068: }
069:
070: /**
071: * Obtains the current value of the value part.
072: *
073: * @return the current value as Date.
074: * @see #setDate( Timestamp )
075: * @see #setDate( Timestamp )
076: * @see #setDate( Date )
077: * @see #setDate( long )
078: */
079: public Timestamp getDate() {
080: return m_value;
081: }
082:
083: /**
084: * Overwrites the current value of the value part.
085: *
086: * @param value is the new value to use from now on.
087: * @see #getDate()
088: * @see #setDate( Date )
089: * @see #setDate( long )
090: */
091: public void setDate(Timestamp value) {
092: m_value = value;
093: }
094:
095: /**
096: * Overwrites the current value of the value part.
097: *
098: * @param value is the new value to use from now on.
099: * @see #getDate()
100: * @see #setDate( Timestamp )
101: * @see #setDate( long )
102: */
103: public void setDate(Date value) {
104: m_value = new Timestamp(value.getTime());
105: }
106:
107: /**
108: * Overwrites the current value of the value part.
109: *
110: * @param value is the new value to use from now on.
111: * @see #getDate()
112: * @see #setDate( Timestamp )
113: * @see #setDate( Date )
114: */
115: public void setDate(long value) {
116: m_value = new Timestamp(value);
117: }
118:
119: /**
120: * Return generically the value as a copy of the original.
121: *
122: * @return the value wrapped as Java object.
123: * @see #setValue( Object )
124: * @see java.sql.Timestamp
125: */
126: public Object getValue() {
127: return new Timestamp(m_value.getTime());
128: }
129:
130: /**
131: * Generic interface to set a value in an instance. Note
132: * that this action may fail, if the instance is of an
133: * incompatible type.
134: *
135: * @param value is the data object to set.
136: * @see #getValue()
137: * @exception ClassCastException if the actual argument type of the value
138: * is incompatible with the value maintained by the instance.
139: */
140: public void oldSetValue(Object value) throws ClassCastException {
141: if (value instanceof java.sql.Timestamp)
142: m_value = (Timestamp) value;
143: else if (value instanceof java.util.Date)
144: m_value = new Timestamp(((Date) value).getTime());
145: else if (value instanceof java.lang.Number)
146: m_value = new Timestamp(((Number) value).longValue());
147: else if (value instanceof java.lang.String) {
148: try {
149: SimpleDateFormat df = new SimpleDateFormat("MM/dd/yy");
150: Date d = df.parse((String) value);
151: m_value = new Timestamp(d.getTime());
152: } catch (java.text.ParseException e) {
153: throw new ClassCastException(
154: "type cast failed: not a Date");
155: }
156: } else
157: throw new ClassCastException("type cast failed: not a Date");
158: }
159:
160: public void setValue(Object value) throws ClassCastException {
161: if (value instanceof java.sql.Timestamp)
162: m_value = (Timestamp) value;
163: else if (value instanceof java.util.Date)
164: m_value = new Timestamp(((Date) value).getTime());
165: else if (value instanceof java.lang.Number)
166: m_value = new Timestamp(((Number) value).longValue());
167: else if (value instanceof java.lang.String) {
168: try {
169: Date d = TupleDate.parse((String) value);
170: m_value = new Timestamp(d.getTime());
171: } catch (ClassCastException e) {
172: throw e;
173: }
174: } else
175: throw new ClassCastException("type cast failed: not a Date");
176: }
177:
178: /**
179: * Returns the type of the value as SQL Type.
180: *
181: * @return a constant from the set of SQL types.
182: * @see java.sql.Types
183: */
184: public int getType() {
185: return java.sql.Types.TIMESTAMP;
186: }
187:
188: /**
189: * Returns the string representation of the type.
190: */
191: public String getTypeString() {
192: return "date";
193: }
194:
195: // Utility Methods
196:
197: public static Date parse(String s) throws ClassCastException {
198: Date d = null;
199: SimpleDateFormat fmt[] = {
200: new SimpleDateFormat("MM/dd/yy HH:mm:ss.SSS"),
201: new SimpleDateFormat("MM/dd/yy HH:mm:ss"),
202: new SimpleDateFormat("MM/dd/yy HH:mm"),
203: new SimpleDateFormat("MM/dd/yy"),
204: new SimpleDateFormat("MM.dd.yy HH:mm:ss.SSS"),
205: new SimpleDateFormat("MM.dd.yy HH:mm:ss"),
206: new SimpleDateFormat("MM.dd.yy HH:mm"),
207: new SimpleDateFormat("MM.dd.yy"),
208: new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"),
209: new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),
210: new SimpleDateFormat("yyyy-MM-dd HH:mm"),
211: new SimpleDateFormat("yyyy-MM-dd"),
212: new SimpleDateFormat("yyyy.MM.dd HH:mm:ss.SSS"),
213: new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"),
214: new SimpleDateFormat("yyyy.MM.dd HH:mm"),
215: new SimpleDateFormat("yyyy.MM.dd"),
216: new SimpleDateFormat("HH:mm:ss.SSS"),
217: new SimpleDateFormat("HH:mm:ss"),
218: new SimpleDateFormat("HH:mm") };
219: for (int i = 0; i < fmt.length; i++) {
220: try {
221: d = fmt[i].parse(s);
222: break;
223: } catch (ParseException e) {
224: continue;
225: }
226: }
227: if (d != null)
228: return d;
229: else {
230: throw new ClassCastException("type cast failed: not a Date");
231: }
232: }
233:
234: }
|