KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Date JavaDoc;
48
49 /**
50  * Base class for date, time, timestamp subclasses.
51  *
52  * @author Stan Bailes
53  */

54 public abstract class ValueDateTime extends Value implements Externalizable JavaDoc {
55     long val;
56
57     public ValueDateTime() {}
58
59     public ValueDateTime(long val) {
60     this.val = val;
61     }
62
63     Calendar JavaDoc getCal() {
64     Calendar JavaDoc c = Calendar.getInstance();
65     c.setTime(new Date JavaDoc(val));
66     return c;
67     }
68
69     public long getTime() { return val; }
70
71     public String JavaDoc toString() { return Long.toString(val); }
72
73     public Value unop(int op) throws ValueException {
74     switch (op) {
75     case Op.NULL:
76         return ValueBoolean.falseBoolean;
77     default:
78         throw new ValueException("Unary op: " + Op.toString(op) +
79                      " not implemented for this type");
80     }
81     }
82     
83     public abstract Value binop(int op, Value l) throws ValueException;
84
85     public Value binop(int op, ValueNull r) throws ValueException {
86     switch (op) {
87     case Op.EQ:
88     case Op.NE:
89     case Op.LT:
90     case Op.LE:
91     case Op.GT:
92     case Op.GE:
93         return ValueUnknown.valueUnknown;
94     case Op.COMPARE:
95         return r.valCmpNull();
96     default:
97         throw badBinop(op, r);
98     }
99     }
100
101     public Value binop(int op, ValueDate r) throws ValueException {
102     return ValueDateTime.binop(op, this, (ValueDateTime)r);
103     }
104
105     public Value binop(int op, ValueTime r) throws ValueException {
106     return ValueDateTime.binop(op, this, (ValueDateTime)r);
107     }
108
109     public Value binop(int op, ValueTimestamp r) throws ValueException {
110     return ValueDateTime.binop(op, this, (ValueDateTime)r);
111     }
112
113     public static final Value binop(int op, ValueDateTime e, ValueDateTime f)
114     throws ValueException
115     {
116     switch (op) {
117         case Op.EQ:
118         return new ValueBoolean(e.val == f.val);
119         case Op.GE:
120         return new ValueBoolean(e.val >= f.val);
121         case Op.GT:
122         return new ValueBoolean(e.val > f.val);
123         case Op.LE:
124         return new ValueBoolean(e.val <= f.val);
125         case Op.LT:
126         return new ValueBoolean(e.val < f.val);
127         case Op.NE:
128         return new ValueBoolean(e.val != f.val);
129     case Op.COMPARE:
130         if (e.val < f.val) return ValueInteger.MINUS_ONE;
131         if (e.val > f.val) return ValueInteger.PLUS_ONE;
132         return ValueInteger.ZERO;
133     default:
134         throw badBinop(op, e, f);
135     }
136     }
137     
138     public void readExternal(ObjectInput JavaDoc in)
139     throws IOException JavaDoc, ClassNotFoundException JavaDoc
140     {
141     val = in.readLong();
142     }
143     
144     public void writeExternal(ObjectOutput JavaDoc out)
145     throws IOException JavaDoc
146     {
147     out.writeLong(val);
148     }
149
150     public Object JavaDoc asJavaObject() {
151     return null;
152     }
153
154     abstract public Type getType();
155
156
157     public void serializeKey(KeyStream out) throws IOException JavaDoc {
158     out.writeLong(val);
159     }
160 }
161
Popular Tags