KickJava   Java API By Example, From Geeks To Geeks.

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