1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 import com
.sun
.star
.beans
.PropertyValue
;
31 import java
.lang
.reflect
.Array
;
32 import java
.lang
.reflect
.Field
;
33 import java
.lang
.reflect
.Modifier
;
34 import com
.sun
.star
.uno
.Type
;
35 import com
.sun
.star
.uno
.Enum
;
36 import com
.sun
.star
.uno
.XInterface
;
37 import com
.sun
.star
.uno
.Any
;
38 import com
.sun
.star
.uno
.AnyConverter
;
39 import java
.util
.HashMap
;
42 public class ValueComparer
{
44 // Method to change a Value, thought for properties
45 public static boolean equalValue( Object first
, Object second
) {
47 if (first
instanceof com
.sun
.star
.uno
.Any
) {
49 first
= AnyConverter
.toObject(((Any
) first
).getType(),first
);
50 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
53 if (second
instanceof com
.sun
.star
.uno
.Any
) {
55 second
= AnyConverter
.toObject(((Any
) second
).getType(),second
);
56 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
61 if ( (first
==null) || (second
== null) ) {
62 eq
= (first
== second
);
65 if ( util
.utils
.isVoid(first
) && util
.utils
.isVoid(second
) ) {
67 } else if ( util
.utils
.isVoid(first
) || util
.utils
.isVoid(second
) ) {
68 eq
= (first
== second
);
70 eq
= compareObjects(first
, second
);
75 System
.out
.println("Exception occurred while comparing Objects");
79 } // end of equalValue
81 static boolean compareArrayOfPropertyValue(PropertyValue
[] pv1
, PropertyValue
[] pv2
){
82 if ( pv1
.length
!= pv2
.length
) {
85 HashMap hm1
= new HashMap();
86 boolean result
= true;
89 for (i
= 0; i
< pv1
.length
; i
++){
90 hm1
.put(pv1
[i
].Name
, pv1
[i
].Value
);
94 while (i
< pv2
.length
&& result
) {
95 result
&= equalValue(hm1
.get(pv2
[i
].Name
),pv2
[i
].Value
);
101 static boolean compareArrays(Object op1
, Object op2
) throws Exception
{
103 if (op1
instanceof PropertyValue
[] && op2
instanceof PropertyValue
[]) {
104 return compareArrayOfPropertyValue((PropertyValue
[])op1
,(PropertyValue
[])op2
);
106 boolean result
= true;
107 if((op1
.getClass().getComponentType() == op2
.getClass().getComponentType())
108 && (Array
.getLength(op1
) == Array
.getLength(op2
)))
110 Class zClass
= op1
.getClass().getComponentType();
112 for(int i
= 0; i
< Array
.getLength(op1
); ++ i
)
113 result
= result
& compareObjects(Array
.get(op1
, i
), Array
.get(op2
, i
));
121 static boolean compareInterfaces(XInterface op1
, XInterface op2
) {
125 static boolean compareUntil(Class zClass
, Class untilClass
, Object op1
, Object op2
) throws Exception
{
126 boolean result
= true;
128 // write inherited members first
129 Class superClass
= zClass
.getSuperclass();
130 if(superClass
!= null && superClass
!= untilClass
)
131 result
= result
& compareUntil(superClass
, untilClass
, op1
, op2
);
133 Field fields
[] = zClass
.getDeclaredFields();
135 for(int i
= 0; i
< fields
.length
&& result
; ++ i
) {
136 if((fields
[i
].getModifiers() & (Modifier
.STATIC
| Modifier
.TRANSIENT
)) == 0) { // neither static nor transient ?
137 Object obj1
= fields
[i
].get(op1
);
138 Object obj2
= fields
[i
].get(op2
);
139 if (obj1
instanceof com
.sun
.star
.uno
.Any
) {
141 if (utils
.isVoid(obj1
)) {
144 obj1
= AnyConverter
.toObject(((Any
) obj1
).getType(),obj1
);
146 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
149 if (obj2
instanceof com
.sun
.star
.uno
.Any
) {
151 if (utils
.isVoid(obj2
)) {
154 obj2
= AnyConverter
.toObject(((Any
) obj2
).getType(),obj2
);
156 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
160 result
= result
& compareObjects(obj1
, obj2
);
168 static boolean compareStructs(Object op1
, Object op2
) throws Exception
{
169 boolean result
= true;
171 if(op1
.getClass() != op2
.getClass())
174 result
= compareUntil(op1
.getClass(), Object
.class, op1
, op2
);
180 static boolean compareThrowable(Throwable op1
, Throwable op2
) throws Exception
{
181 boolean result
= true;
183 if(op1
.getClass() != op2
.getClass())
186 result
= compareUntil(op1
.getClass(), Throwable
.class, op1
, op2
);
188 result
= result
& op1
.getMessage().equals(op2
.getMessage());
194 static boolean compareEnums(Enum en1
, Enum en2
) {
195 return en1
.getValue() == en2
.getValue();
198 static boolean compareObjects(Object op1
, Object op2
) throws Exception
{
199 boolean result
= false;
204 if ( (op1
==null) || (op2
== null) ) {
205 result
= (op1
== op2
);
208 else if(op1
.getClass().isPrimitive() && op2
.getClass().isPrimitive())
209 result
= op1
.equals(op2
);
211 else if(op1
.getClass() == Byte
.class && op2
.getClass() == Byte
.class)
212 result
= op1
.equals(op2
);
214 else if(op1
.getClass() == Type
.class && op2
.getClass() == Type
.class)
215 result
= op1
.equals(op2
);
217 else if(op1
.getClass() == Boolean
.class && op2
.getClass() == Boolean
.class)
218 result
= op1
.equals(op2
);
220 else if(op1
.getClass() == Short
.class && op2
.getClass() == Short
.class)
221 result
= op1
.equals(op2
);
223 else if(Throwable
.class.isAssignableFrom(op1
.getClass()) && Throwable
.class.isAssignableFrom(op2
.getClass()))
224 result
= compareThrowable((Throwable
)op1
, (Throwable
)op2
);
226 else if(op1
.getClass() == Integer
.class && op2
.getClass() == Integer
.class)
227 result
= op1
.equals(op2
);
229 else if(op1
.getClass() == Character
.class && op2
.getClass() == Character
.class)
230 result
= op1
.equals(op2
);
232 else if(op1
.getClass() == Long
.class && op2
.getClass() == Long
.class)
233 result
= op1
.equals(op2
);
235 else if(op1
.getClass() == Void
.class && op2
.getClass() == Void
.class)
236 result
= op1
.equals(op2
);
238 else if(op1
.getClass() == Float
.class && op2
.getClass() == Float
.class)
239 result
= op1
.equals(op2
);
241 else if(op1
.getClass() == Double
.class && op2
.getClass() == Double
.class)
242 result
= op1
.equals(op2
);
244 else if(op1
.getClass().isArray() && op2
.getClass().isArray())
245 result
= compareArrays(op1
, op2
);
247 else if(op1
.getClass() == Void
.class || op2
.getClass() == void.class) // write nothing ?
250 else if(XInterface
.class.isAssignableFrom(op1
.getClass()) && XInterface
.class.isAssignableFrom(op2
.getClass()))
251 compareInterfaces((XInterface
)op1
, (XInterface
)op2
);
253 else if(Enum
.class.isAssignableFrom(op1
.getClass()) && Enum
.class.isAssignableFrom(op2
.getClass()))
254 compareEnums((Enum
)op1
, (Enum
)op2
);
256 else if(op1
.getClass() == String
.class && op2
.getClass() == String
.class) // is it a String ?
257 result
= ((String
)op1
).equals((String
)op2
);
259 else // otherwise it must be a struct
260 result
= compareStructs(op1
, op2
);