KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.SimpleDateFormat JavaDoc;
47
48 import java.util.Calendar JavaDoc;
49 import java.util.Date JavaDoc;
50
51 import com.quadcap.sql.io.ExternalizeProxy;
52
53 /**
54  * A <b>DATE</b> value.
55  *
56  * @author Stan Bailes
57  */

58 public class ValueDate extends ValueDateTime implements Externalizable JavaDoc, ExternalizeProxy {
59     static SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
60     
61     public ValueDate() {}
62
63     public ValueDate(long val) {
64     super(val);
65     }
66     
67     public ValueDate(String JavaDoc str) throws antlr.RecognitionException {
68     try {
69             synchronized (df) {
70                 val = df.parse(str).getTime();
71             }
72     } catch (Throwable JavaDoc e) {
73         throw new antlr.RecognitionException(e.toString());
74     }
75     }
76
77     public String JavaDoc toString() {
78         synchronized (df) {
79             return df.format(new java.sql.Date JavaDoc(val));
80         }
81     }
82
83     public Value binop(int op, ValueString r) throws ValueException {
84         try {
85             return ValueDateTime.binop(op, this, new ValueDate(r.val));
86         } catch (antlr.RecognitionException e) {
87             throw new ValueException("Not a date: " + val);
88         }
89     }
90
91     public Value binop(int op, Value l) throws ValueException {
92     return l.binop(op, this);
93     }
94
95     public Value binop(int op, ValueInterval r) throws ValueException {
96     switch (op) {
97     case Op.PLUS:
98         if (r.ym) {
99         Calendar JavaDoc c = getCal();
100         c.add(Calendar.MONTH, (int)r.val);
101         return new ValueDate(c.getTime().getTime());
102         } else {
103         return new ValueDate(val + r.val);
104         }
105     case Op.MINUS:
106         if (r.ym) {
107         Calendar JavaDoc c = getCal();
108         c.add(Calendar.MONTH, 0 - (int)r.val);
109         return new ValueDate(c.getTime().getTime());
110         } else {
111         return new ValueDate(val - r.val);
112         }
113     default:
114         throw badBinop(op, r);
115     }
116     }
117
118     public Value unop(int op) throws ValueException {
119     switch (op) {
120     case Op.NULL:
121         return ValueBoolean.falseBoolean;
122     default:
123         throw new ValueException("Unary op: " + Op.toString(op) +
124                      " not implemented for this type");
125     }
126     }
127     
128     public void readExternal(ObjectInput JavaDoc in)
129     throws IOException JavaDoc, ClassNotFoundException JavaDoc
130     {
131     super.readExternal(in);
132     }
133     
134     public void writeExternal(ObjectOutput JavaDoc out)
135     throws IOException JavaDoc
136     {
137     super.writeExternal(out);
138     }
139
140     public Object JavaDoc readObject(ObjectInput JavaDoc in)
141     throws IOException JavaDoc, ClassNotFoundException JavaDoc
142     {
143     ValueDate v = new ValueDate();
144     v.readExternal(in);
145     return v;
146     }
147
148     public void writeObject(ObjectOutput JavaDoc out, Object JavaDoc object)
149     throws IOException JavaDoc
150     {
151     ((ValueDate)object).writeExternal(out);
152     }
153
154     public Object JavaDoc asJavaObject() {
155     return new java.sql.Date JavaDoc(val);
156     }
157
158     public void fromJavaObject(Object JavaDoc obj) throws ValueException {
159     if (obj instanceof Date JavaDoc) {
160         val = ((Date JavaDoc)obj).getTime();
161     } else {
162         throw new ValueException("bad type: " + obj);
163     }
164     }
165
166     public Type getType() {
167     return TypeDate.typeDate;
168     }
169
170     public Value convert(TypeDate t) {
171     return this;
172     }
173
174     public Value convert(TypeTimestamp t) {
175     return new ValueTimestamp(val);
176     }
177 }
178
Popular Tags