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