1 /*
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio, Inc. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio, Inc. Exolab is a registered
23 * trademark of Intalio, Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
42 *
43 * $Id: CompareHelper.java 6785 2007-01-29 05:09:59Z ekuns $
44 */
45 package org.castor.xmlctf;
46
47 import java.lang.reflect.Array;
48
49 /**
50 * Assists in the comparison of objects. This method is used by generated
51 * code but is not used within the CTF directly.
52 *
53 * @author <a href="mailto:gignoux@intalio.com">Sebastien Gignoux</a>
54 * @version $Revision: 6785 $ $Date: 2003-10-15 09:17:49 -0600 (Wed, 15 Oct 2003) $
55 */
56 public class CompareHelper {
57
58 /**
59 * Compare two objects. Return true if they are both null or if they are
60 * equal. This comparison method has special handling for arrays: For
61 * arrays, each element is compared.
62 * <p>
63 * Warning: We will throw a NullPointerException if any element of either
64 * array is null.
65 *
66 * @param o1 first object
67 * @param o2 second object
68 * @return true if both objects are both null or otherwise are equal
69 */
70 public static boolean equals(Object o1, Object o2) {
71 if (o1 == null && o2 == null) {
72 return true;
73 }
74
75 if ((o1 != null && o2 == null) || (o1 == null && o2 != null)) {
76 return false;
77 }
78
79 // From now we can safely assume (o1 != null && o2 != null)
80
81 if (! o1.getClass().equals(o2.getClass())) {
82 return false;
83 }
84
85 if (o1.getClass().isArray()) {
86 return compareArray(o1, o2);
87 }
88
89 return o1.equals(o2);
90 }
91
92 /**
93 * Compares two arrays, returning true if the arrays contain the same
94 * values.
95 * <p>
96 * Warning: We will throw a NullPointerException if any element of either
97 * array is null.
98 *
99 * @param o1 The first array
100 * @param o2 The second array
101 * @return true if the two objects represent arrays with the same values
102 */
103 private static boolean compareArray(Object o1, Object o2) {
104 int size = Array.getLength(o1);
105
106 if (size != Array.getLength(o2))
107 return false;
108
109 Class component = o1.getClass().getComponentType();
110
111 if ( ! component.equals(o2.getClass().getComponentType()))
112 return false;
113
114 if (component.isPrimitive()) {
115 return comparePrimitiveArray(o1, o2);
116 }
117
118 for (int i=0; i < size; ++i) {
119 if (! Array.get(o1, i).equals(Array.get(o2, i))) {
120 return false;
121 }
122 }
123
124 return true;
125 }
126
127 /**
128 * Compares two arrays of primitive values. The caller should have tested
129 * that the two array have the same length and that the component type are
130 * equal.
131 *
132 * @param o1 The first array
133 * @param o2 The second array
134 * @return true if the two objects represent arrays of the same primitive
135 * values
136 */
137 public static boolean comparePrimitiveArray(Object o1, Object o2) {
138 Class component = o1.getClass().getComponentType();
139 int size = Array.getLength(o1);
140
141 if (component.equals(Boolean.TYPE)) {
142 for (int i=0; i<size; ++i) {
143 if (Array.getBoolean(o1, i) != Array.getBoolean(o2, i)) {
144 return false;
145 }
146 }
147
148 return true;
149 } else if (component.equals(Byte.TYPE)) {
150 for (int i=0; i<size; ++i) {
151 if (Array.getByte(o1, i) != Array.getByte(o2, i)) {
152 return false;
153 }
154 }
155
156 return true;
157 } else if (component.equals(Character.TYPE)) {
158 for (int i=0; i<size; ++i) {
159 if (Array.getChar(o1, i) != Array.getChar(o2, i)) {
160 return false;
161 }
162 }
163
164 return true;
165 } else if (component.equals(Double.TYPE)) {
166 for (int i=0; i<size; ++i) {
167 if (Array.getDouble(o1, i) != Array.getDouble(o2, i)) {
168 return false;
169 }
170 }
171
172 return true;
173 } else if (component.equals(Float.TYPE)) {
174 for (int i=0; i<size; ++i) {
175 if (Array.getFloat(o1, i) != Array.getFloat(o2, i)) {
176 return false;
177 }
178 }
179
180 return true;
181 } else if (component.equals(Integer.TYPE)) {
182 for (int i=0; i<size; ++i) {
183 if (Array.getInt(o1, i) != Array.getInt(o2, i)) {
184 return false;
185 }
186 }
187
188 return true;
189 } else if (component.equals(Long.TYPE)) {
190 for (int i=0; i<size; ++i) {
191 if (Array.getLong(o1, i) != Array.getLong(o2, i)) {
192 return false;
193 }
194 }
195
196 return true;
197 } else if (component.equals(Short.TYPE)) {
198 for (int i=0; i<size; ++i) {
199 if (Array.getShort(o1, i) != Array.getShort(o2, i)) {
200 return false;
201 }
202 }
203
204 return true;
205 } else {
206 throw new IllegalArgumentException("Unexpected primitive type " + component.toString());
207 }
208 }
209
210 }