KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002 (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: OwnerWidget.java,v 1.5 2004/03/22 04:58:13 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test;
12
13 import javax.jdo.InstanceCallbacks;
14 import javax.jdo.JDOHelper;
15 import javax.jdo.PersistenceManager;
16
17
18 public class OwnerWidget extends TestObject implements InstanceCallbacks
19 {
20     private Cloneable JavaDoc cloneable;
21     private TestObject testObject;
22
23
24     public OwnerWidget()
25     {
26         cloneable = null;
27         testObject = null;
28     }
29
30
31     public Object JavaDoc clone()
32     {
33         OwnerWidget ow = (OwnerWidget)super.clone();
34
35         ow.cloneable = (Cloneable JavaDoc)((TestObject)cloneable).clone();
36         ow.testObject = (TestObject)testObject.clone();
37
38         return ow;
39     }
40
41
42     public Cloneable JavaDoc getCloneable()
43     {
44         return cloneable;
45     }
46
47
48     public TestObject getTestObject()
49     {
50         return testObject;
51     }
52
53
54     private Widget randomWidget()
55     {
56         Widget obj;
57
58         switch (r.nextInt(4))
59         {
60             case 0:
61             default:
62                 obj = new Widget();
63                 break;
64
65             case 1:
66                 obj = new DateWidget();
67                 break;
68
69             case 2:
70                 obj = new StringWidget();
71                 break;
72
73             case 3:
74                 obj = new BinaryWidget();
75                 break;
76         }
77
78         obj.fillRandom();
79
80         return obj;
81     }
82
83
84     /**
85      * Fills all of the object's fields with random data values.
86      */

87
88     public void fillRandom()
89     {
90         PersistenceManager myPM = JDOHelper.getPersistenceManager(this);
91
92         if (myPM != null)
93         {
94             myPM.deletePersistent(cloneable);
95             myPM.deletePersistent(testObject);
96         }
97
98         cloneable = randomWidget();
99         testObject = randomWidget();
100     }
101
102
103     /**
104      * Indicates whether some other object is "equal to" this one. By comparing
105      * against an original copy of the object, <code>compareTo()</code> can be
106      * used to verify that the object has been written to a database and read
107      * back correctly.
108      *
109      * @param obj the reference object with which to compare
110      *
111      * @return <code>true</code> if this object is equal to the obj argument;
112      * <code>false</code> otherwise.
113      */

114
115     public boolean compareTo(Object JavaDoc obj)
116     {
117         if (obj == this)
118             return true;
119
120         if (!(obj instanceof OwnerWidget))
121             return false;
122
123         OwnerWidget w = (OwnerWidget)obj;
124
125         return ((TestObject)cloneable).compareTo(w.cloneable)
126             && testObject.compareTo(w.testObject);
127     }
128
129
130     /**
131      * Returns a string representation for this object. All of the field
132      * values are included in the string for debugging purposes.
133      *
134      * @return a string representation for this object.
135      */

136
137     public String JavaDoc toString()
138     {
139         StringBuffer JavaDoc s = new StringBuffer JavaDoc(super.toString());
140
141         s.append(" cloneable = ").append(cloneable);
142         s.append('\n');
143         s.append(" testObject = ").append(testObject);
144         s.append('\n');
145
146         return s.toString();
147     }
148
149
150     public void jdoPostLoad()
151     {
152         return;
153     }
154
155     public void jdoPreClear()
156     {
157         return;
158     }
159
160     public void jdoPreDelete()
161     {
162         PersistenceManager myPM = JDOHelper.getPersistenceManager(this);
163
164         myPM.deletePersistent(cloneable);
165         myPM.deletePersistent(testObject);
166     }
167
168     public void jdoPreStore()
169     {
170         return;
171     }
172 }
173
Popular Tags