KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > lib > TCompare


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.test.lib;
22
23
24 import java.lang.reflect.*;
25 import java.util.*;
26
27 import com.db4o.*;
28
29 public class TCompare {
30
31     public boolean isEqual(Object JavaDoc a_compare, Object JavaDoc a_with) {
32         return isEqual(a_compare, a_with, null, null);
33     }
34
35     public boolean isEqual(Object JavaDoc a_compare, Object JavaDoc a_with, String JavaDoc a_path, Stack a_stack) {
36         if (a_path == null || a_path.length() < 1) {
37             if (a_compare != null) {
38                 a_path = a_compare.getClass().getName() + ":";
39             } else {
40                 if (a_with != null) {
41                     a_path = a_with.getClass().getName() + ":";
42                 }
43             }
44         }
45
46         String JavaDoc path = a_path;
47
48         if (a_compare == null) {
49             return a_with == null;
50         }
51         if (a_with == null) {
52             return false;
53         }
54         Class JavaDoc clazz = a_compare.getClass();
55         if (clazz != a_with.getClass()) {
56             return false;
57         }
58
59         if (Platform4.isSimple(clazz)) {
60             return a_compare.equals(a_with);
61         }
62
63         if (a_stack == null) {
64             a_stack = new Stack();
65         }
66
67         // takes care of repeating calls to the same object
68
if (a_stack.contains(a_compare)) {
69             return true;
70         }
71         a_stack.push(a_compare);
72
73         Field fields[] = clazz.getDeclaredFields();
74         for (int i = 0; i < fields.length; i++) {
75             if (storeableField(clazz, fields[i])) {
76                 Platform4.setAccessible(fields[i]);
77                 try {
78                     path = a_path + fields[i].getName() + ":";
79                     Object JavaDoc compare = fields[i].get(a_compare);
80                     Object JavaDoc with = fields[i].get(a_with);
81                     if (compare == null) {
82                         if (with != null) {
83                             return false;
84                         }
85                     } else if (with == null) {
86                         return false;
87                     } else {
88                         if (compare.getClass().isArray()) {
89                             if (!with.getClass().isArray()) {
90                                 return false;
91                             } else {
92                                 compare = normalizeNArray(compare);
93                                 with = normalizeNArray(with);
94                                 int len = Array.getLength(compare);
95                                 if (len != Array.getLength(with)) {
96                                     return false;
97                                 } else {
98                                     for (int j = 0; j < len; j++) {
99                                         Object JavaDoc elementCompare = Array.get(compare, j);
100                                         Object JavaDoc elementWith = Array.get(with, j);
101                                         // if (l_persistentArray)
102
if (!isEqual(elementCompare, elementWith, path, a_stack)) {
103                                             return false;
104                                         } else if (elementCompare == null) {
105                                             if (elementWith != null) {
106                                                 return false;
107                                             }
108                                         } else if (elementWith == null) {
109                                             return false;
110                                         } else {
111                                             Class JavaDoc elementCompareClass = elementCompare.getClass();
112                                             if (elementCompareClass != elementWith.getClass()) {
113                                                 return false;
114                                             }
115                                             if (hasPublicConstructor(elementCompareClass)) {
116                                                 if (!isEqual(elementCompare,
117                                                     elementWith,
118                                                     path,
119                                                     a_stack)) {
120                                                     return false;
121
122                                                 }
123                                             } else if (!elementCompare.equals(elementWith)) {
124                                                 return false;
125                                             }
126                                         }
127
128                                     }
129
130                                 }
131                             }
132                         } else if (hasPublicConstructor(fields[i].getType())) {
133                             if (!isEqual(compare, with, path, a_stack)) {
134                                 return false;
135                             }
136                         } else {
137                             if (!compare.equals(with)) {
138                                 return false;
139                             }
140                         }
141                     }
142                 } catch (IllegalAccessException JavaDoc ex) {
143                     // probably JDK 1
144
// never mind this field
145
return true;
146                 } catch (Exception JavaDoc e) {
147                     System.err.println("TCompare failure executing path:" + path);
148                     e.printStackTrace();
149                     return false;
150                 }
151             }
152         }
153         return true;
154     }
155
156     boolean hasPublicConstructor(Class JavaDoc a_class) {
157         if (a_class != String JavaDoc.class) {
158             try {
159                 return a_class.newInstance() != null;
160             } catch (Throwable JavaDoc t) {
161             }
162         }
163         return false;
164     }
165
166     Object JavaDoc normalizeNArray(Object JavaDoc a_object) {
167         if (Array.getLength(a_object) > 0) {
168             Object JavaDoc first = Array.get(a_object, 0);
169             if (first != null && first.getClass().isArray()) {
170                 int dim[] = arrayDimensions(a_object);
171                 Object JavaDoc all = new Object JavaDoc[arrayElementCount(dim)];
172                 normalizeNArray1(a_object, all, 0, dim, 0);
173                 return all;
174             }
175         }
176         return a_object;
177     }
178
179     int normalizeNArray1(Object JavaDoc a_object, Object JavaDoc a_all, int a_next, int a_dim[], int a_index) {
180         if (a_index == a_dim.length - 1) {
181             for (int i = 0; i < a_dim[a_index]; i++) {
182                 Array.set(a_all, a_next++, Array.get(a_object, i));
183             }
184         } else {
185             for (int i = 0; i < a_dim[a_index]; i++) {
186                 a_next =
187                     normalizeNArray1(Array.get(a_object, i), a_all, a_next, a_dim, a_index + 1);
188             }
189
190         }
191         return a_next;
192     }
193
194     int[] arrayDimensions(Object JavaDoc a_object) {
195         int count = 0;
196         for (Class JavaDoc clazz = a_object.getClass();
197             clazz.isArray();
198             clazz = clazz.getComponentType()) {
199             count++;
200         }
201         int dim[] = new int[count];
202         for (int i = 0; i < count; i++) {
203             dim[i] = Array.getLength(a_object);
204             a_object = Array.get(a_object, 0);
205         }
206         return dim;
207     }
208
209     int arrayElementCount(int a_dim[]) {
210         int elements = a_dim[0];
211         for (int i = 1; i < a_dim.length; i++) {
212             elements *= a_dim[i];
213         }
214         return elements;
215     }
216
217     public boolean storeableField(Class JavaDoc a_class, Field a_field) {
218         return (!Modifier.isStatic(a_field.getModifiers()))
219             && (!Modifier.isTransient(a_field.getModifiers())
220                 & !(a_field.getName().indexOf("$") > -1));
221     }
222
223
224 }
225
Popular Tags