1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ValueComparer.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import com
.sun
.star
.beans
.PropertyValue
;
34 import java
.lang
.reflect
.Array
;
35 import java
.lang
.reflect
.Field
;
36 import java
.lang
.reflect
.Modifier
;
37 import com
.sun
.star
.uno
.Type
;
38 import com
.sun
.star
.uno
.Enum
;
39 import com
.sun
.star
.uno
.XInterface
;
40 import com
.sun
.star
.uno
.Any
;
41 import com
.sun
.star
.uno
.AnyConverter
;
42 import java
.util
.HashMap
;
45 public class ValueComparer
{
47 // Method to change a Value, thought for properties
48 public static boolean equalValue( Object first
, Object second
) {
50 if (first
instanceof com
.sun
.star
.uno
.Any
) {
52 first
= AnyConverter
.toObject(((Any
) first
).getType(),first
);
53 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
56 if (second
instanceof com
.sun
.star
.uno
.Any
) {
58 second
= AnyConverter
.toObject(((Any
) second
).getType(),second
);
59 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
64 if ( (first
==null) || (second
== null) ) {
65 eq
= (first
== second
);
68 if ( util
.utils
.isVoid(first
) && util
.utils
.isVoid(second
) ) {
70 } else if ( util
.utils
.isVoid(first
) || util
.utils
.isVoid(second
) ) {
71 eq
= (first
== second
);
73 eq
= compareObjects(first
, second
);
78 System
.out
.println("Exception occured while comparing Objects");
82 } // end of equalValue
84 static boolean compareArrayOfPropertyValue(PropertyValue
[] pv1
, PropertyValue
[] pv2
){
85 if ( pv1
.length
!= pv2
.length
) {
88 HashMap hm1
= new HashMap();
89 boolean result
= true;
92 for (i
= 0; i
< pv1
.length
; i
++){
93 hm1
.put(pv1
[i
].Name
, pv1
[i
].Value
);
97 while (i
< pv2
.length
&& result
) {
98 result
&= equalValue(hm1
.get(pv2
[i
].Name
),pv2
[i
].Value
);
104 static boolean compareArrays(Object op1
, Object op2
) throws Exception
{
106 if (op1
instanceof PropertyValue
[] && op2
instanceof PropertyValue
[]) {
107 return compareArrayOfPropertyValue((PropertyValue
[])op1
,(PropertyValue
[])op2
);
109 boolean result
= true;
110 if((op1
.getClass().getComponentType() == op2
.getClass().getComponentType())
111 && (Array
.getLength(op1
) == Array
.getLength(op2
)))
113 Class zClass
= op1
.getClass().getComponentType();
115 for(int i
= 0; i
< Array
.getLength(op1
); ++ i
)
116 result
= result
& compareObjects(Array
.get(op1
, i
), Array
.get(op2
, i
));
124 static boolean compareInterfaces(XInterface op1
, XInterface op2
) {
128 static boolean compareUntil(Class zClass
, Class untilClass
, Object op1
, Object op2
) throws Exception
{
129 boolean result
= true;
131 // write inherited members first
132 Class superClass
= zClass
.getSuperclass();
133 if(superClass
!= null && superClass
!= untilClass
)
134 result
= result
& compareUntil(superClass
, untilClass
, op1
, op2
);
136 Field fields
[] = zClass
.getDeclaredFields();
138 for(int i
= 0; i
< fields
.length
&& result
; ++ i
) {
139 if((fields
[i
].getModifiers() & (Modifier
.STATIC
| Modifier
.TRANSIENT
)) == 0) { // neither static nor transient ?
140 Object obj1
= fields
[i
].get(op1
);
141 Object obj2
= fields
[i
].get(op2
);
142 if (obj1
instanceof com
.sun
.star
.uno
.Any
) {
144 if (utils
.isVoid(obj1
)) {
147 obj1
= AnyConverter
.toObject(((Any
) obj1
).getType(),obj1
);
149 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
152 if (obj2
instanceof com
.sun
.star
.uno
.Any
) {
154 if (utils
.isVoid(obj2
)) {
157 obj2
= AnyConverter
.toObject(((Any
) obj2
).getType(),obj2
);
159 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
163 result
= result
& compareObjects(obj1
, obj2
);
171 static boolean compareStructs(Object op1
, Object op2
) throws Exception
{
172 boolean result
= true;
174 if(op1
.getClass() != op2
.getClass())
177 result
= compareUntil(op1
.getClass(), Object
.class, op1
, op2
);
183 static boolean compareThrowable(Throwable op1
, Throwable op2
) throws Exception
{
184 boolean result
= true;
186 if(op1
.getClass() != op2
.getClass())
189 result
= compareUntil(op1
.getClass(), Throwable
.class, op1
, op2
);
191 result
= result
& op1
.getMessage().equals(op2
.getMessage());
197 static boolean compareEnums(Enum en1
, Enum en2
) {
198 return en1
.getValue() == en2
.getValue();
201 static boolean compareObjects(Object op1
, Object op2
) throws Exception
{
202 boolean result
= false;
207 if ( (op1
==null) || (op2
== null) ) {
208 result
= (op1
== op2
);
211 else if(op1
.getClass().isPrimitive() && op2
.getClass().isPrimitive())
212 result
= op1
.equals(op2
);
214 else if(op1
.getClass() == Byte
.class && op2
.getClass() == Byte
.class)
215 result
= op1
.equals(op2
);
217 else if(op1
.getClass() == Type
.class && op2
.getClass() == Type
.class)
218 result
= op1
.equals(op2
);
220 else if(op1
.getClass() == Boolean
.class && op2
.getClass() == Boolean
.class)
221 result
= op1
.equals(op2
);
223 else if(op1
.getClass() == Short
.class && op2
.getClass() == Short
.class)
224 result
= op1
.equals(op2
);
226 else if(Throwable
.class.isAssignableFrom(op1
.getClass()) && Throwable
.class.isAssignableFrom(op2
.getClass()))
227 result
= compareThrowable((Throwable
)op1
, (Throwable
)op2
);
229 else if(op1
.getClass() == Integer
.class && op2
.getClass() == Integer
.class)
230 result
= op1
.equals(op2
);
232 else if(op1
.getClass() == Character
.class && op2
.getClass() == Character
.class)
233 result
= op1
.equals(op2
);
235 else if(op1
.getClass() == Long
.class && op2
.getClass() == Long
.class)
236 result
= op1
.equals(op2
);
238 else if(op1
.getClass() == Void
.class && op2
.getClass() == Void
.class)
239 result
= op1
.equals(op2
);
241 else if(op1
.getClass() == Float
.class && op2
.getClass() == Float
.class)
242 result
= op1
.equals(op2
);
244 else if(op1
.getClass() == Double
.class && op2
.getClass() == Double
.class)
245 result
= op1
.equals(op2
);
247 else if(op1
.getClass().isArray() && op2
.getClass().isArray())
248 result
= compareArrays(op1
, op2
);
250 else if(op1
.getClass() == Void
.class || op2
.getClass() == void.class) // write nothing ?
253 else if(XInterface
.class.isAssignableFrom(op1
.getClass()) && XInterface
.class.isAssignableFrom(op2
.getClass()))
254 compareInterfaces((XInterface
)op1
, (XInterface
)op2
);
256 else if(Enum
.class.isAssignableFrom(op1
.getClass()) && Enum
.class.isAssignableFrom(op2
.getClass()))
257 compareEnums((Enum
)op1
, (Enum
)op2
);
259 else if(op1
.getClass() == String
.class && op2
.getClass() == String
.class) // is it a String ?
260 result
= ((String
)op1
).equals((String
)op2
);
262 else // otherwise it must be a struct
263 result
= compareStructs(op1
, op2
);