001: package com.quadcap.sql.types;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.Externalizable;
042: import java.io.IOException;
043: import java.io.ObjectInput;
044: import java.io.ObjectOutput;
045:
046: import java.text.SimpleDateFormat;
047:
048: import java.util.Calendar;
049: import java.util.Date;
050:
051: import com.quadcap.sql.io.ExternalizeProxy;
052:
053: /**
054: * A <b>DATE</b> value.
055: *
056: * @author Stan Bailes
057: */
058: public class ValueDate extends ValueDateTime implements Externalizable,
059: ExternalizeProxy {
060: static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
061:
062: public ValueDate() {
063: }
064:
065: public ValueDate(long val) {
066: super (val);
067: }
068:
069: public ValueDate(String str) throws antlr.RecognitionException {
070: try {
071: synchronized (df) {
072: val = df.parse(str).getTime();
073: }
074: } catch (Throwable e) {
075: throw new antlr.RecognitionException(e.toString());
076: }
077: }
078:
079: public String toString() {
080: synchronized (df) {
081: return df.format(new java.sql.Date(val));
082: }
083: }
084:
085: public Value binop(int op, ValueString r) throws ValueException {
086: try {
087: return ValueDateTime.binop(op, this , new ValueDate(r.val));
088: } catch (antlr.RecognitionException e) {
089: throw new ValueException("Not a date: " + val);
090: }
091: }
092:
093: public Value binop(int op, Value l) throws ValueException {
094: return l.binop(op, this );
095: }
096:
097: public Value binop(int op, ValueInterval r) throws ValueException {
098: switch (op) {
099: case Op.PLUS:
100: if (r.ym) {
101: Calendar c = getCal();
102: c.add(Calendar.MONTH, (int) r.val);
103: return new ValueDate(c.getTime().getTime());
104: } else {
105: return new ValueDate(val + r.val);
106: }
107: case Op.MINUS:
108: if (r.ym) {
109: Calendar c = getCal();
110: c.add(Calendar.MONTH, 0 - (int) r.val);
111: return new ValueDate(c.getTime().getTime());
112: } else {
113: return new ValueDate(val - r.val);
114: }
115: default:
116: throw badBinop(op, r);
117: }
118: }
119:
120: public Value unop(int op) throws ValueException {
121: switch (op) {
122: case Op.NULL:
123: return ValueBoolean.falseBoolean;
124: default:
125: throw new ValueException("Unary op: " + Op.toString(op)
126: + " not implemented for this type");
127: }
128: }
129:
130: public void readExternal(ObjectInput in) throws IOException,
131: ClassNotFoundException {
132: super .readExternal(in);
133: }
134:
135: public void writeExternal(ObjectOutput out) throws IOException {
136: super .writeExternal(out);
137: }
138:
139: public Object readObject(ObjectInput in) throws IOException,
140: ClassNotFoundException {
141: ValueDate v = new ValueDate();
142: v.readExternal(in);
143: return v;
144: }
145:
146: public void writeObject(ObjectOutput out, Object object)
147: throws IOException {
148: ((ValueDate) object).writeExternal(out);
149: }
150:
151: public Object asJavaObject() {
152: return new java.sql.Date(val);
153: }
154:
155: public void fromJavaObject(Object obj) throws ValueException {
156: if (obj instanceof Date) {
157: val = ((Date) obj).getTime();
158: } else {
159: throw new ValueException("bad type: " + obj);
160: }
161: }
162:
163: public Type getType() {
164: return TypeDate.typeDate;
165: }
166:
167: public Value convert(TypeDate t) {
168: return this ;
169: }
170:
171: public Value convert(TypeTimestamp t) {
172: return new ValueTimestamp(val);
173: }
174: }
|