2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import com
.sun
.star
.beans
.PropertyValue
;
22 import java
.lang
.reflect
.Array
;
23 import java
.lang
.reflect
.Field
;
24 import java
.lang
.reflect
.Modifier
;
25 import com
.sun
.star
.uno
.Type
;
26 import com
.sun
.star
.uno
.Enum
;
27 import com
.sun
.star
.uno
.XInterface
;
28 import com
.sun
.star
.uno
.Any
;
29 import com
.sun
.star
.uno
.AnyConverter
;
30 import java
.util
.HashMap
;
33 public class ValueComparer
{
35 // Method to change a Value, thought for properties
36 public static boolean equalValue( Object first
, Object second
) {
38 if (first
instanceof com
.sun
.star
.uno
.Any
) {
40 first
= AnyConverter
.toObject(((Any
) first
).getType(),first
);
41 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
44 if (second
instanceof com
.sun
.star
.uno
.Any
) {
46 second
= AnyConverter
.toObject(((Any
) second
).getType(),second
);
47 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
52 if ( (first
==null) || (second
== null) ) {
53 eq
= (first
== second
);
56 if ( util
.utils
.isVoid(first
) && util
.utils
.isVoid(second
) ) {
58 } else if ( util
.utils
.isVoid(first
) || util
.utils
.isVoid(second
) ) {
59 eq
= (first
== second
);
61 eq
= compareObjects(first
, second
);
66 System
.out
.println("Exception occurred while comparing Objects");
70 } // end of equalValue
72 private static boolean compareArrayOfPropertyValue(PropertyValue
[] pv1
, PropertyValue
[] pv2
){
73 if ( pv1
.length
!= pv2
.length
) {
76 HashMap
<String
, Object
> hm1
= new HashMap
<String
, Object
>();
77 boolean result
= true;
80 for (i
= 0; i
< pv1
.length
; i
++){
81 hm1
.put(pv1
[i
].Name
, pv1
[i
].Value
);
85 while (i
< pv2
.length
&& result
) {
86 result
&= equalValue(hm1
.get(pv2
[i
].Name
),pv2
[i
].Value
);
92 private static boolean compareArrays(Object op1
, Object op2
) throws Exception
{
94 if (op1
instanceof PropertyValue
[] && op2
instanceof PropertyValue
[]) {
95 return compareArrayOfPropertyValue((PropertyValue
[])op1
,(PropertyValue
[])op2
);
97 boolean result
= true;
98 if((op1
.getClass().getComponentType() == op2
.getClass().getComponentType())
99 && (Array
.getLength(op1
) == Array
.getLength(op2
)))
101 op1
.getClass().getComponentType();
103 for(int i
= 0; i
< Array
.getLength(op1
); ++ i
)
104 result
= result
& compareObjects(Array
.get(op1
, i
), Array
.get(op2
, i
));
112 private static boolean compareInterfaces(XInterface op1
, XInterface op2
) {
116 private static boolean compareUntil(Class
<?
> zClass
, Class
<?
> untilClass
, Object op1
, Object op2
) throws Exception
{
117 boolean result
= true;
119 // write inherited members first
120 Class
<?
> superClass
= zClass
.getSuperclass();
121 if(superClass
!= null && superClass
!= untilClass
)
122 result
= result
& compareUntil(superClass
, untilClass
, op1
, op2
);
124 Field fields
[] = zClass
.getDeclaredFields();
126 for(int i
= 0; i
< fields
.length
&& result
; ++ i
) {
127 if((fields
[i
].getModifiers() & (Modifier
.STATIC
| Modifier
.TRANSIENT
)) == 0) { // neither static nor transient ?
128 Object obj1
= fields
[i
].get(op1
);
129 Object obj2
= fields
[i
].get(op2
);
130 if (obj1
instanceof com
.sun
.star
.uno
.Any
) {
132 if (utils
.isVoid(obj1
)) {
135 obj1
= AnyConverter
.toObject(((Any
) obj1
).getType(),obj1
);
137 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
140 if (obj2
instanceof com
.sun
.star
.uno
.Any
) {
142 if (utils
.isVoid(obj2
)) {
145 obj2
= AnyConverter
.toObject(((Any
) obj2
).getType(),obj2
);
147 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
151 result
= result
& compareObjects(obj1
, obj2
);
159 private static boolean compareStructs(Object op1
, Object op2
) throws Exception
{
160 boolean result
= true;
162 if(op1
.getClass() != op2
.getClass())
165 result
= compareUntil(op1
.getClass(), Object
.class, op1
, op2
);
171 private static boolean compareThrowable(Throwable op1
, Throwable op2
) throws Exception
{
172 boolean result
= true;
174 if(op1
.getClass() != op2
.getClass())
177 result
= compareUntil(op1
.getClass(), Throwable
.class, op1
, op2
);
179 result
= result
& op1
.getMessage().equals(op2
.getMessage());
185 private static boolean compareEnums(Enum en1
, Enum en2
) {
186 return en1
.getValue() == en2
.getValue();
189 private static boolean compareObjects(Object op1
, Object op2
) throws Exception
{
192 else if(op1
==null || op2
== null)
194 else if(op1
.getClass().isPrimitive() && op2
.getClass().isPrimitive())
195 return op1
.equals(op2
);
196 else if(op1
.getClass() == Byte
.class && op2
.getClass() == Byte
.class)
197 return op1
.equals(op2
);
198 else if(op1
.getClass() == Type
.class && op2
.getClass() == Type
.class)
199 return op1
.equals(op2
);
200 else if(op1
.getClass() == Boolean
.class && op2
.getClass() == Boolean
.class)
201 return op1
.equals(op2
);
202 else if(op1
.getClass() == Short
.class && op2
.getClass() == Short
.class)
203 return op1
.equals(op2
);
204 else if(Throwable
.class.isAssignableFrom(op1
.getClass()) && Throwable
.class.isAssignableFrom(op2
.getClass()))
205 return compareThrowable((Throwable
)op1
, (Throwable
)op2
);
206 else if(op1
.getClass() == Integer
.class && op2
.getClass() == Integer
.class)
207 return op1
.equals(op2
);
208 else if(op1
.getClass() == Character
.class && op2
.getClass() == Character
.class)
209 return op1
.equals(op2
);
210 else if(op1
.getClass() == Long
.class && op2
.getClass() == Long
.class)
211 return op1
.equals(op2
);
212 else if(op1
.getClass() == Void
.class && op2
.getClass() == Void
.class)
213 return op1
.equals(op2
);
214 else if(op1
.getClass() == Float
.class && op2
.getClass() == Float
.class)
215 return op1
.equals(op2
);
216 else if(op1
.getClass() == Double
.class && op2
.getClass() == Double
.class)
217 return op1
.equals(op2
);
218 else if(op1
.getClass().isArray() && op2
.getClass().isArray())
219 return compareArrays(op1
, op2
);
220 else if(op1
.getClass() == Void
.class || op2
.getClass() == void.class) // write nothing ?
222 else if(XInterface
.class.isAssignableFrom(op1
.getClass()) && XInterface
.class.isAssignableFrom(op2
.getClass()))
223 return compareInterfaces((XInterface
)op1
, (XInterface
)op2
);
224 else if(Enum
.class.isAssignableFrom(op1
.getClass()) && Enum
.class.isAssignableFrom(op2
.getClass()))
225 return compareEnums((Enum
)op1
, (Enum
)op2
);
226 else if(op1
.getClass() == String
.class && op2
.getClass() == String
.class) // is it a String ?
227 return ((String
)op1
).equals(op2
);
228 else // otherwise it must be a struct
229 return compareStructs(op1
, op2
);