KickJava   Java API By Example, From Geeks To Geeks.

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


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: TestObject.java,v 1.8 2004/03/22 04:58:13 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test;
12
13 import com.triactive.jdo.store.OID;
14 import java.io.Serializable JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Map.Entry;
21 import java.util.Random JavaDoc;
22 import javax.jdo.JDOHelper;
23
24
25 public abstract class TestObject implements Cloneable JavaDoc, Serializable JavaDoc
26 {
27     protected static Random JavaDoc r = new Random JavaDoc(0);
28
29     public static boolean allowNegativeByteValues = true;
30
31
32     protected byte nextByte()
33     {
34         if (allowNegativeByteValues)
35             return (byte)(r.nextInt((int)Byte.MAX_VALUE * 2) - Byte.MAX_VALUE);
36         else
37             return (byte)r.nextInt((int)Byte.MAX_VALUE + 1);
38     }
39
40
41     protected char nextCharacter()
42     {
43         return (char)('!' + r.nextInt(93));
44     }
45
46
47     protected String JavaDoc nextString(int length)
48     {
49         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
50
51         while (length-- > 0)
52             s.append(nextCharacter());
53
54         return s.toString();
55     }
56
57
58     protected byte[] nextBinary(int length)
59     {
60         byte[] ba = new byte[length];
61
62         for (int i = 0; i < length; ++i)
63             ba[i] = (byte)(r.nextInt((int)Byte.MAX_VALUE * 2) - Byte.MAX_VALUE);
64
65         return ba;
66     }
67
68
69     /*
70      * Indicates whether or not the next random nullable field value should be
71      * null. Returns true approx. 20% of the time.
72      */

73     protected boolean nextNull()
74     {
75         return r.nextInt(5) < 1;
76     }
77
78
79     public Object JavaDoc clone()
80     {
81         Object JavaDoc obj = null;
82
83         try { obj = super.clone(); } catch (CloneNotSupportedException JavaDoc e) {}
84
85         return obj;
86     }
87
88
89     public abstract void fillRandom();
90
91
92     /**
93      * Indicates whether some other object is "equal to" this one. By comparing
94      * against an original copy of the object, <code>compareTo()</code> can be
95      * used to verify that the object has been written to a database and read
96      * back correctly.
97      *
98      * @param obj the reference object with which to compare
99      *
100      * @return <code>true</code> if this object is equal to the obj argument;
101      * <code>false</code> otherwise.
102      */

103
104     public abstract boolean compareTo(Object JavaDoc obj);
105
106
107     public boolean equals(Object JavaDoc obj)
108     {
109         if (obj == this)
110             return true;
111
112         Object JavaDoc id = JDOHelper.getObjectId(this);
113
114         return id == null ? super.equals(obj) : id.equals(JDOHelper.getObjectId(obj));
115     }
116
117
118     public int hashCode()
119     {
120         Object JavaDoc id = JDOHelper.getObjectId(this);
121
122         return id == null ? super.hashCode() : id.hashCode();
123     }
124
125     public String JavaDoc toString()
126     {
127         StringBuffer JavaDoc s = new StringBuffer JavaDoc(getClass().getName() + ":");
128
129         s.append(" JVM id = ").append(System.identityHashCode(this));
130         s.append('\n');
131
132         Object JavaDoc id = JDOHelper.getObjectId(this);
133         s.append(" JDO id = ").append(id);
134         if (id instanceof OID)
135             s.append(" (").append(((OID)id).longValue()).append(')');
136         s.append('\n');
137
138         return s.toString();
139     }
140
141
142     public static Object JavaDoc clone(Object JavaDoc o)
143     {
144         if (o instanceof Number JavaDoc || o instanceof String JavaDoc || o instanceof Boolean JavaDoc)
145             return o;
146
147         try
148         {
149             return o.getClass().getMethod("clone", null).invoke(o, null);
150         }
151         catch (RuntimeException JavaDoc e)
152         {
153             throw e;
154         }
155         catch (Exception JavaDoc e)
156         {
157             throw new RuntimeException JavaDoc("Clone failed", e);
158         }
159     }
160
161
162     /**
163      * Compares two objects for equality.
164      * Returns true if and only if the two objects compare equal to each other.
165      * If the objects are an instance of TestObject the {@link #compareTo} is
166      * used, which ensures that the object's contents are compared and not just
167      * their JDO ID's.
168      * Otherwise the equals() method is used.
169      *
170      * @return <tt>true</tt> if the objects compare equal,
171      * <tt>false</tt> otherwise.
172      */

173
174     public static boolean compareObject(Object JavaDoc o1, Object JavaDoc o2)
175     {
176         if (o1 == null)
177             return o2 == null;
178         else if (o1 instanceof TestObject)
179             return ((TestObject)o1).compareTo(o2);
180         else
181             return o1.equals(o2);
182     }
183
184
185     /**
186      * Compares two collections for equality.
187      * Returns true if and only if the two collections contain the same number
188      * of objects and each element of the first collection has exactly one
189      * corresponding element in the second collection that compares equal
190      * according to {@link #compareObject}.
191      *
192      * @return <tt>true</tt> if the collections compare equal,
193      * <tt>false</tt> otherwise.
194      */

195
196     public static boolean compareCollection(Collection JavaDoc c1, Collection JavaDoc c2)
197     {
198         if (c1 == null)
199             return c2 == null;
200         else if (c2 == null)
201             return false;
202
203         if (c1.size() != c2.size())
204             return false;
205
206         c2 = new ArrayList JavaDoc(c2);
207
208         Iterator JavaDoc i = c1.iterator();
209
210         while (i.hasNext())
211         {
212             Object JavaDoc obj = i.next();
213
214             boolean found = false;
215             Iterator JavaDoc j = c2.iterator();
216
217             while (j.hasNext())
218             {
219                 if (compareObject(obj, j.next()))
220                 {
221                     j.remove();
222                     found = true;
223                     break;
224                 }
225             }
226
227             if (!found)
228                 return false;
229         }
230
231         return c2.isEmpty();
232     }
233
234
235     /**
236      * Compares two maps.
237      * Returns true if and only if the two maps contain the same number of
238      * entries and each entry of the first map has a corresponding entry in the
239      * second map whose key and value both compare equal according to
240      * {@link #compareObject}.
241      *
242      * @return <tt>true</tt> if the maps compare equal,
243      * <tt>false</tt> otherwise.
244      */

245
246     public static boolean compareMap(Map JavaDoc m1, Map JavaDoc m2)
247     {
248         if (m1 == null)
249             return m2 == null;
250         else if (m2 == null)
251             return false;
252
253         if (m1.size() != m2.size())
254             return false;
255
256         m2 = new HashMap JavaDoc(m2);
257
258         Iterator JavaDoc i = m1.entrySet().iterator();
259
260         while (i.hasNext())
261         {
262             Entry e1 = (Entry)i.next();
263
264             boolean found = false;
265             Iterator JavaDoc j = m2.entrySet().iterator();
266
267             while (j.hasNext())
268             {
269                 Entry e2 = (Entry)j.next();
270
271                 if (compareObject(e1.getKey(), e2.getKey()) &&
272                     compareObject(e1.getValue(), e2.getValue()))
273                 {
274                     j.remove();
275                     found = true;
276                     break;
277                 }
278             }
279
280             if (!found)
281                 return false;
282         }
283
284         return m2.isEmpty();
285     }
286 }
287
Popular Tags