KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > types > ValueTimestamp


1 package com.quadcap.sql.types;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.Externalizable JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.ObjectInput JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45
46 import java.util.Calendar JavaDoc;
47
48 import java.sql.Timestamp JavaDoc;
49
50 import java.text.SimpleDateFormat JavaDoc;
51
52 import com.quadcap.sql.io.Extern;
53 import com.quadcap.sql.io.Externable;
54
55 /**
56  * A <b>TIMESTAMP</b> value.
57  *
58  * @author Stan Bailes
59  */

60 public class ValueTimestamp extends ValueDateTime
61 implements Externalizable JavaDoc, Externable {
62     int nanos = 0;
63
64     static SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss");
65     
66     public ValueTimestamp() {}
67
68     public ValueTimestamp(Timestamp JavaDoc val) {
69     super(val.getTime());
70     nanos = val.getNanos();
71     }
72
73     public ValueTimestamp(long val) {
74     super(val);
75     nanos = (int)((val % 1000) * 1000);
76     }
77     
78     public ValueTimestamp(String JavaDoc str) throws antlr.RecognitionException {
79     try {
80             synchronized (df) {
81                 val = df.parse(str).getTime();
82                 int idx = str.indexOf('.');
83                 if (idx > 0) {
84                     String JavaDoc frac = str.substring(idx+1);
85                     long fi = Integer.parseInt(frac);
86                     int digits = frac.length();
87                     if (digits < 9) {
88                         fi *= (long)Math.pow(10, 9 - digits);
89                     } else if (digits > 9) {
90                         fi /= (long)Math.pow(10, digits - 9);
91                     }
92                     nanos = (int)fi;
93                 } else {
94                     nanos = 0;
95                 }
96             }
97         
98     } catch (Throwable JavaDoc e) {
99         throw new antlr.RecognitionException(e.toString());
100     }
101     }
102
103     public String JavaDoc toString() {
104         synchronized (df) {
105             return df.format(new java.sql.Timestamp JavaDoc(val));
106         }
107     }
108
109     public Value unop(int op) throws ValueException {
110     switch (op) {
111     case Op.NULL:
112         return ValueBoolean.falseBoolean;
113     default:
114         throw new ValueException("Unary op: " + Op.toString(op) +
115                      " not implemented for this type");
116     }
117     }
118     
119     public Value binop(int op, Value l) throws ValueException {
120     return l.binop(op, this);
121     }
122
123     public Value binop(int op, ValueString r) throws ValueException {
124         try {
125             return ValueDateTime.binop(op, this, new ValueTimestamp(r.val));
126         } catch (antlr.RecognitionException e) {
127             throw new ValueException("Invalid timestamp format: " + val);
128         }
129     }
130
131     public Value binop(int op, ValueInterval r) throws ValueException {
132     switch (op) {
133     case Op.PLUS:
134         if (r.ym) {
135         Calendar JavaDoc c = getCal();
136         c.add(Calendar.MONTH, (int)r.val);
137         return new ValueTimestamp(c.getTime().getTime());
138         } else {
139         return new ValueTimestamp(val + r.val);
140         }
141     case Op.MINUS:
142         if (r.ym) {
143         Calendar JavaDoc c = getCal();
144         c.add(Calendar.MONTH, 0 - (int)r.val);
145         return new ValueTimestamp(c.getTime().getTime());
146         } else {
147         return new ValueTimestamp(val - r.val);
148         }
149     default:
150         throw badBinop(op, r);
151     }
152     }
153
154     public void readExternal(ObjectInput JavaDoc in)
155     throws IOException JavaDoc, ClassNotFoundException JavaDoc
156     {
157     super.readExternal(in);
158     nanos = in.readInt();
159     }
160     
161     public void writeExternal(ObjectOutput JavaDoc out)
162     throws IOException JavaDoc
163     {
164     super.writeExternal(out);
165     out.writeInt(nanos);
166     }
167
168     public Object JavaDoc asJavaObject() {
169     Timestamp JavaDoc t = new Timestamp JavaDoc(val);
170     t.setNanos(nanos);
171     return t;
172     }
173
174     public void fromJavaObject(Object JavaDoc obj) throws ValueException {
175     if (obj instanceof Timestamp JavaDoc) {
176         Timestamp JavaDoc t = (Timestamp JavaDoc)obj;
177         val = t.getTime();
178         nanos = t.getNanos();
179     } else if (obj instanceof java.util.Date JavaDoc) {
180         val = ((java.util.Date JavaDoc)obj).getTime();
181         nanos = 0;
182     } else {
183         throw new ValueException("bad type: " + obj);
184     }
185     }
186
187     public Type getType() {
188     return TypeTimestamp.typeTimestamp;
189     }
190
191     public Value convert(TypeTimestamp type) {
192     return this;
193     }
194
195     public Value convert(TypeDate type) {
196     return new ValueDate(val);
197     }
198
199     public Value convert(TypeTime type) {
200     return new ValueTime(val);
201     }
202
203     public void serializeKey(KeyStream out) throws IOException JavaDoc {
204         out.writeLength(2, KeyStream.COMPOUND);
205         out.writeLong(val);
206         out.writeInt(nanos);
207     }
208
209     static Extern extern = null;
210     public Extern getExtern() { return extern; }
211     public void setExtern(Extern extern) { ValueTimestamp.extern = extern; }
212 }
213
Popular Tags