KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > test > DateWidget


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: DateWidget.java,v 1.5 2004/01/18 03:01:07 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test;
12
13
14 public class DateWidget extends Widget implements HasSCOFields
15 {
16     private java.util.Date JavaDoc dateField;
17     private java.sql.Date JavaDoc sqlDateField;
18     private java.sql.Timestamp JavaDoc sqlTimestampField;
19
20
21     public DateWidget()
22     {
23         super();
24     }
25
26
27     public java.util.Date JavaDoc getDateField()
28     {
29         return dateField;
30     }
31
32
33     public java.sql.Date JavaDoc getSQLDateField()
34     {
35         return sqlDateField;
36     }
37
38
39     public java.sql.Timestamp JavaDoc getSQLTimestampField()
40     {
41         return sqlTimestampField;
42     }
43
44
45     public Object JavaDoc[] getSCOFieldValues()
46     {
47         return new Object JavaDoc[] { dateField, sqlDateField, sqlTimestampField };
48     }
49
50
51     /**
52      * Fills all of the object's fields with random data values. Any non-
53      * primitive fields will also be assigned <code>null</code> on a random
54      * basis.
55      */

56
57     public void fillRandom()
58     {
59         super.fillRandom();
60
61         /*
62          * When updating a field (rather than nulling it), roughly half the time
63          * we mutate the existing object if possible. In the remaining cases we
64          * assign a new object.
65          *
66          * All dates below are made to be relative to an even second value
67          * because we don't insist that the database manage fractional seconds
68          * correctly.
69          */

70         if (nextNull())
71             dateField = null;
72         else
73         {
74             long t = r.nextInt() * 1000L;
75
76             if (dateField != null && r.nextBoolean())
77                 dateField.setTime(t);
78             else
79                 dateField = new java.util.Date JavaDoc(t);
80         }
81
82         if (nextNull())
83             sqlDateField = null;
84         else
85         {
86             /*
87              * We have to convert the random date value to String and back in order
88              * to discard the time-of-day portion of the data, otherwise the field
89              * won't compare exactly after it's been transferred to the database and
90              * back.
91              */

92             java.sql.Date JavaDoc rndDate = new java.sql.Date JavaDoc(r.nextInt() * 1000L);
93             long t = java.sql.Date.valueOf(rndDate.toString()).getTime();
94
95             if (sqlDateField != null && r.nextBoolean())
96                 sqlDateField.setTime(t);
97             else
98                 sqlDateField = new java.sql.Date JavaDoc(t);
99         }
100
101         if (nextNull())
102             sqlTimestampField = null;
103         else
104         {
105             long t = r.nextInt() * 1000L;
106
107             if (sqlTimestampField != null && r.nextBoolean())
108                 sqlTimestampField.setTime(t);
109             else
110                 sqlTimestampField = new java.sql.Timestamp JavaDoc(t);
111         }
112     }
113
114
115     /**
116      * Indicates whether some other object is "equal to" this one. By comparing
117      * against an original copy of the object, <code>compareTo()</code> can be
118      * used to verify that the object has been written to a database and read
119      * back correctly.
120      *
121      * @param obj the reference object with which to compare
122      *
123      * @return <code>true</code> if this object is equal to the obj argument;
124      * <code>false</code> otherwise.
125      */

126
127     public boolean compareTo(Object JavaDoc obj)
128     {
129         if (this == obj)
130             return true;
131
132         if (!(obj instanceof DateWidget) || !super.compareTo(obj))
133             return false;
134
135         DateWidget w = (DateWidget)obj;
136
137         if (dateField == null) { if (w.dateField != null) return false; }
138         else if (!dateField.equals(w.dateField)) return false;
139
140         if (sqlDateField == null) { if (w.sqlDateField != null) return false; }
141         else if (!sqlDateField.equals(w.sqlDateField)) return false;
142
143         if (sqlTimestampField == null) { if (w.sqlTimestampField != null) return false; }
144         else if (!sqlTimestampField.equals(w.sqlTimestampField)) return false;
145
146         return true;
147     }
148
149
150     /**
151      * Returns a string representation for this object. All of the field
152      * values are included in the string for debugging purposes.
153      *
154      * @return a string representation for this object.
155      */

156
157     public String JavaDoc toString()
158     {
159         StringBuffer JavaDoc s = new StringBuffer JavaDoc(super.toString());
160
161         s.append(" dateField = ").append(dateField);
162         if (dateField != null)
163             s.append(" (").append(dateField.getTime()).append(')');
164         s.append('\n');
165
166         s.append(" sqlDateField = ").append(sqlDateField);
167         if (sqlDateField != null)
168             s.append(" (").append(sqlDateField.getTime()).append(')');
169         s.append('\n');
170
171         s.append(" sqlTimestampField = ").append(sqlTimestampField);
172         if (sqlTimestampField != null)
173             s.append(" (").append(sqlTimestampField.getTime()).append(')');
174         s.append('\n');
175
176         return s.toString();
177     }
178 }
179
Popular Tags