KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > value > ValueTimestamp


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.value;
6
7 import java.math.BigDecimal JavaDoc;
8 import java.sql.PreparedStatement JavaDoc;
9 import java.sql.SQLException JavaDoc;
10 import java.sql.Timestamp JavaDoc;
11
12 import org.h2.message.Message;
13 import org.h2.util.DateTimeUtils;
14 import org.h2.util.MathUtils;
15
16 public class ValueTimestamp extends Value {
17     public static final int PRECISION = 23;
18     public static final int DEFAULT_SCALE = 10;
19     private Timestamp JavaDoc value;
20
21     private ValueTimestamp(Timestamp JavaDoc value) {
22         this.value = value;
23     }
24
25     public Timestamp JavaDoc getTimestamp() {
26         return (Timestamp JavaDoc)value.clone();
27     }
28     
29     public Timestamp JavaDoc getTimestampNoCopy() {
30         return value;
31     }
32
33     public String JavaDoc getSQL() {
34         return "TIMESTAMP '" + getString() + "'";
35     }
36
37     public static Timestamp JavaDoc parseTimestamp(String JavaDoc s) throws SQLException JavaDoc {
38         return (Timestamp JavaDoc) DateTimeUtils.parseDateTime(s, Value.TIMESTAMP, Message.TIMESTAMP_CONSTANT_1);
39     }
40
41     public int getType() {
42         return Value.TIMESTAMP;
43     }
44
45     protected int compareSecure(Value o, CompareMode mode) {
46         ValueTimestamp v = (ValueTimestamp) o;
47         int c = value.compareTo(v.value);
48         return c == 0 ? 0 : (c < 0 ? -1 : 1);
49     }
50
51     public String JavaDoc getString() {
52         return value.toString();
53     }
54
55     public long getPrecision() {
56         return PRECISION;
57     }
58
59     public int getScale() {
60         return DEFAULT_SCALE;
61     }
62
63     public int hashCode() {
64         return value.hashCode();
65     }
66
67     public Object JavaDoc getObject() {
68         // this class is mutable - must copy the object
69
return getTimestamp();
70     }
71
72     public void set(PreparedStatement JavaDoc prep, int parameterIndex) throws SQLException JavaDoc {
73         prep.setTimestamp(parameterIndex, value);
74     }
75
76     public static ValueTimestamp get(Timestamp JavaDoc timestamp) {
77         timestamp = (Timestamp JavaDoc) timestamp.clone();
78         return getNoCopy(timestamp);
79     }
80
81     public static ValueTimestamp getNoCopy(Timestamp JavaDoc timestamp) {
82         return (ValueTimestamp) Value.cache(new ValueTimestamp(timestamp));
83     }
84
85     public Value convertScale(boolean onlyToSmallerScale, int targetScale) throws SQLException JavaDoc {
86         if (targetScale < 0 || targetScale > DEFAULT_SCALE) {
87             // TODO convertScale for Timestamps: may throw an exception?
88
throw Message.getInvalidValueException(""+targetScale, "scale");
89         }
90         int nanos = value.getNanos();
91         BigDecimal JavaDoc bd = new BigDecimal JavaDoc("" + nanos);
92         bd = bd.movePointLeft(9);
93         bd = MathUtils.setScale(bd, targetScale);
94         bd = bd.movePointRight(9);
95         int n2 = bd.intValue();
96         if (n2 == nanos) {
97             return this;
98         }
99         long t = value.getTime();
100         while (n2 >= 1000000000) {
101             t += 1000;
102             n2 -= 1000000000;
103         }
104         Timestamp JavaDoc t2 = new Timestamp JavaDoc(t);
105         t2.setNanos(n2);
106         return ValueTimestamp.getNoCopy(t2);
107     }
108
109 // public String getJavaString() {
110
// return "Timestamp.valueOf(\"" + toString() + "\")";
111
// }
112

113     public int getDisplaySize() {
114         return "2001-01-01 23:59:59".length();
115     }
116     
117     protected boolean isEqual(Value v) {
118         return v instanceof ValueTimestamp && value.equals(((ValueTimestamp)v).value);
119     }
120
121 }
122
Popular Tags