xref: /AOO41X/main/qadevOOo/runner/util/ValueComparer.java (revision ef39d40d3f5e66cf3f035b3e93783012b340500d)
1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package util;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
27cdf0e10cSrcweir import java.lang.reflect.Array;
28cdf0e10cSrcweir import java.lang.reflect.Field;
29cdf0e10cSrcweir import java.lang.reflect.Modifier;
30cdf0e10cSrcweir import com.sun.star.uno.Type;
31cdf0e10cSrcweir import com.sun.star.uno.Enum;
32cdf0e10cSrcweir import com.sun.star.uno.XInterface;
33cdf0e10cSrcweir import com.sun.star.uno.Any;
34cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
35cdf0e10cSrcweir import java.util.HashMap;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 
38cdf0e10cSrcweir public class ValueComparer {
39cdf0e10cSrcweir 
40cdf0e10cSrcweir     // Method to change a Value, thought for properties
equalValue( Object first, Object second )41cdf0e10cSrcweir     public static boolean equalValue( Object first, Object second ) {
42cdf0e10cSrcweir 
43cdf0e10cSrcweir         if (first instanceof com.sun.star.uno.Any) {
44cdf0e10cSrcweir             try {
45cdf0e10cSrcweir                 first = AnyConverter.toObject(((Any) first).getType(),first);
46cdf0e10cSrcweir             } catch (com.sun.star.lang.IllegalArgumentException iae) {
47cdf0e10cSrcweir             }
48cdf0e10cSrcweir         }
49cdf0e10cSrcweir         if (second instanceof com.sun.star.uno.Any) {
50cdf0e10cSrcweir             try {
51cdf0e10cSrcweir                 second = AnyConverter.toObject(((Any) second).getType(),second);
52cdf0e10cSrcweir             } catch (com.sun.star.lang.IllegalArgumentException iae) {
53cdf0e10cSrcweir             }
54cdf0e10cSrcweir         }
55cdf0e10cSrcweir         boolean eq = false;
56cdf0e10cSrcweir         try {
57cdf0e10cSrcweir             if ( (first==null) || (second == null) ) {
58cdf0e10cSrcweir                 eq = (first == second);
59cdf0e10cSrcweir             }
60cdf0e10cSrcweir             else {
61cdf0e10cSrcweir                 if ( util.utils.isVoid(first) && util.utils.isVoid(second) ) {
62cdf0e10cSrcweir                     eq=true;
63cdf0e10cSrcweir                 } else if ( util.utils.isVoid(first) || util.utils.isVoid(second) ) {
64cdf0e10cSrcweir                     eq = (first == second);
65cdf0e10cSrcweir                 } else {
66cdf0e10cSrcweir                     eq = compareObjects(first, second);
67cdf0e10cSrcweir                 }
68cdf0e10cSrcweir             }
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir         catch (Exception e) {
71cdf0e10cSrcweir             System.out.println("Exception occured while comparing Objects");
72cdf0e10cSrcweir             e.printStackTrace();
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir         return eq;
75cdf0e10cSrcweir     } // end of equalValue
76cdf0e10cSrcweir 
compareArrayOfPropertyValue(PropertyValue[] pv1, PropertyValue[] pv2)77cdf0e10cSrcweir     static boolean compareArrayOfPropertyValue(PropertyValue[] pv1, PropertyValue[] pv2){
78cdf0e10cSrcweir         if ( pv1.length != pv2.length) {
79cdf0e10cSrcweir             return  false;
80cdf0e10cSrcweir         }
81cdf0e10cSrcweir         HashMap hm1 = new HashMap();
82cdf0e10cSrcweir         boolean result = true;
83cdf0e10cSrcweir         int i = 0;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         for (i = 0; i < pv1.length; i++){
86cdf0e10cSrcweir             hm1.put(pv1[i].Name, pv1[i].Value);
87cdf0e10cSrcweir         }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir         i = 0;
90cdf0e10cSrcweir         while (i < pv2.length && result) {
91cdf0e10cSrcweir             result &= equalValue(hm1.get(pv2[i].Name),pv2[i].Value);
92cdf0e10cSrcweir             i++;
93cdf0e10cSrcweir         }
94cdf0e10cSrcweir         return result;
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir 
compareArrays(Object op1, Object op2)97cdf0e10cSrcweir     static boolean compareArrays(Object op1, Object op2) throws Exception {
98cdf0e10cSrcweir 
99cdf0e10cSrcweir         if (op1 instanceof PropertyValue[] && op2 instanceof PropertyValue[]) {
100cdf0e10cSrcweir            return compareArrayOfPropertyValue((PropertyValue[])op1,(PropertyValue[])op2);
101cdf0e10cSrcweir        }
102cdf0e10cSrcweir         boolean result = true;
103cdf0e10cSrcweir         if((op1.getClass().getComponentType() == op2.getClass().getComponentType())
104cdf0e10cSrcweir            && (Array.getLength(op1) == Array.getLength(op2)))
105cdf0e10cSrcweir         {
106cdf0e10cSrcweir             Class zClass = op1.getClass().getComponentType();
107cdf0e10cSrcweir 
108cdf0e10cSrcweir             for(int i = 0; i < Array.getLength(op1); ++ i)
109cdf0e10cSrcweir                 result = result & compareObjects(Array.get(op1, i), Array.get(op2, i));
110cdf0e10cSrcweir         } else {
111cdf0e10cSrcweir             result = false ;
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         return result;
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
compareInterfaces(XInterface op1, XInterface op2)117cdf0e10cSrcweir     static boolean compareInterfaces(XInterface op1, XInterface op2) {
118cdf0e10cSrcweir         return op1 == op2;
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
compareUntil(Class zClass, Class untilClass, Object op1, Object op2)121cdf0e10cSrcweir     static boolean compareUntil(Class zClass, Class untilClass, Object op1, Object op2) throws Exception {
122cdf0e10cSrcweir         boolean result = true;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         // write inherited members first
125cdf0e10cSrcweir         Class superClass = zClass.getSuperclass();
126cdf0e10cSrcweir         if(superClass != null && superClass != untilClass)
127cdf0e10cSrcweir             result = result & compareUntil(superClass, untilClass, op1, op2);
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         Field fields[] = zClass.getDeclaredFields();
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         for(int i = 0; i < fields.length && result; ++ i) {
132cdf0e10cSrcweir             if((fields[i].getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) { // neither static nor transient ?
133cdf0e10cSrcweir                 Object obj1 = fields[i].get(op1);
134cdf0e10cSrcweir                 Object obj2 = fields[i].get(op2);
135cdf0e10cSrcweir                 if (obj1 instanceof com.sun.star.uno.Any) {
136cdf0e10cSrcweir                     try {
137cdf0e10cSrcweir                         if (utils.isVoid(obj1)) {
138cdf0e10cSrcweir                             obj1 = null;
139cdf0e10cSrcweir                         } else {
140cdf0e10cSrcweir                             obj1 = AnyConverter.toObject(((Any) obj1).getType(),obj1);
141cdf0e10cSrcweir                         }
142cdf0e10cSrcweir                     } catch (com.sun.star.lang.IllegalArgumentException iae) {
143cdf0e10cSrcweir                     }
144cdf0e10cSrcweir                 }
145cdf0e10cSrcweir                 if (obj2 instanceof com.sun.star.uno.Any) {
146cdf0e10cSrcweir                     try {
147cdf0e10cSrcweir                         if (utils.isVoid(obj2)) {
148cdf0e10cSrcweir                             obj2 = null;
149cdf0e10cSrcweir                         } else {
150cdf0e10cSrcweir                             obj2 = AnyConverter.toObject(((Any) obj2).getType(),obj2);
151cdf0e10cSrcweir                         }
152cdf0e10cSrcweir                     } catch (com.sun.star.lang.IllegalArgumentException iae) {
153cdf0e10cSrcweir                     }
154cdf0e10cSrcweir                 }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir                 result = result & compareObjects(obj1, obj2);
157cdf0e10cSrcweir 
158cdf0e10cSrcweir             }
159cdf0e10cSrcweir         }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         return result;
162cdf0e10cSrcweir     }
163cdf0e10cSrcweir 
compareStructs(Object op1, Object op2)164cdf0e10cSrcweir     static boolean compareStructs(Object op1, Object op2) throws Exception {
165cdf0e10cSrcweir         boolean result = true;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir         if(op1.getClass() != op2.getClass())
168cdf0e10cSrcweir             result = false;
169cdf0e10cSrcweir         else {
170cdf0e10cSrcweir             result = compareUntil(op1.getClass(), Object.class, op1, op2);
171cdf0e10cSrcweir         }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir         return result;
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir 
compareThrowable(Throwable op1, Throwable op2)176cdf0e10cSrcweir     static boolean compareThrowable(Throwable op1, Throwable op2) throws Exception {
177cdf0e10cSrcweir         boolean result = true;
178cdf0e10cSrcweir 
179cdf0e10cSrcweir         if(op1.getClass() != op2.getClass())
180cdf0e10cSrcweir             result = false;
181cdf0e10cSrcweir         else {
182cdf0e10cSrcweir             result = compareUntil(op1.getClass(), Throwable.class, op1, op2);
183cdf0e10cSrcweir 
184cdf0e10cSrcweir             result = result & op1.getMessage().equals(op2.getMessage());
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         return result;
188cdf0e10cSrcweir     }
189cdf0e10cSrcweir 
compareEnums(Enum en1, Enum en2)190cdf0e10cSrcweir     static boolean compareEnums(Enum en1, Enum en2) {
191cdf0e10cSrcweir         return en1.getValue() == en2.getValue();
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
compareObjects(Object op1, Object op2)194cdf0e10cSrcweir     static boolean compareObjects(Object op1, Object op2) throws Exception {
195cdf0e10cSrcweir         boolean result = false;
196cdf0e10cSrcweir 
197cdf0e10cSrcweir         if(op1 == op2)
198cdf0e10cSrcweir             result = true;
199cdf0e10cSrcweir 
200cdf0e10cSrcweir         if ( (op1==null) || (op2 == null) ) {
201cdf0e10cSrcweir             result = (op1 == op2);
202cdf0e10cSrcweir             }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir         else if(op1.getClass().isPrimitive() && op2.getClass().isPrimitive())
205cdf0e10cSrcweir             result = op1.equals(op2);
206cdf0e10cSrcweir 
207cdf0e10cSrcweir         else if(op1.getClass() == Byte.class && op2.getClass() == Byte.class)
208cdf0e10cSrcweir             result = op1.equals(op2);
209cdf0e10cSrcweir 
210cdf0e10cSrcweir         else if(op1.getClass() == Type.class && op2.getClass() == Type.class)
211cdf0e10cSrcweir             result = op1.equals(op2);
212cdf0e10cSrcweir 
213cdf0e10cSrcweir         else if(op1.getClass() == Boolean.class && op2.getClass() == Boolean.class)
214cdf0e10cSrcweir             result = op1.equals(op2);
215cdf0e10cSrcweir 
216cdf0e10cSrcweir         else if(op1.getClass() == Short.class && op2.getClass() == Short.class)
217cdf0e10cSrcweir             result = op1.equals(op2);
218cdf0e10cSrcweir 
219cdf0e10cSrcweir         else if(Throwable.class.isAssignableFrom(op1.getClass()) && Throwable.class.isAssignableFrom(op2.getClass()))
220cdf0e10cSrcweir             result = compareThrowable((Throwable)op1, (Throwable)op2);
221cdf0e10cSrcweir 
222cdf0e10cSrcweir         else if(op1.getClass() == Integer.class && op2.getClass() == Integer.class)
223cdf0e10cSrcweir             result = op1.equals(op2);
224cdf0e10cSrcweir 
225cdf0e10cSrcweir         else if(op1.getClass() == Character.class && op2.getClass() == Character.class)
226cdf0e10cSrcweir             result = op1.equals(op2);
227cdf0e10cSrcweir 
228cdf0e10cSrcweir         else if(op1.getClass() == Long.class && op2.getClass() == Long.class)
229cdf0e10cSrcweir             result = op1.equals(op2);
230cdf0e10cSrcweir 
231cdf0e10cSrcweir         else if(op1.getClass() == Void.class && op2.getClass() == Void.class)
232cdf0e10cSrcweir             result = op1.equals(op2);
233cdf0e10cSrcweir 
234cdf0e10cSrcweir         else if(op1.getClass() == Float.class && op2.getClass() == Float.class)
235cdf0e10cSrcweir             result = op1.equals(op2);
236cdf0e10cSrcweir 
237cdf0e10cSrcweir         else if(op1.getClass() == Double.class && op2.getClass() == Double.class)
238cdf0e10cSrcweir             result = op1.equals(op2);
239cdf0e10cSrcweir 
240cdf0e10cSrcweir         else if(op1.getClass().isArray() && op2.getClass().isArray())
241cdf0e10cSrcweir             result = compareArrays(op1, op2);
242cdf0e10cSrcweir 
243cdf0e10cSrcweir         else if(op1.getClass() == Void.class || op2.getClass() == void.class) // write nothing ?
244cdf0e10cSrcweir             result = true;
245cdf0e10cSrcweir 
246cdf0e10cSrcweir         else if(XInterface.class.isAssignableFrom(op1.getClass()) && XInterface.class.isAssignableFrom(op2.getClass()))
247cdf0e10cSrcweir             compareInterfaces((XInterface)op1, (XInterface)op2);
248cdf0e10cSrcweir 
249cdf0e10cSrcweir         else if(Enum.class.isAssignableFrom(op1.getClass()) && Enum.class.isAssignableFrom(op2.getClass()))
250cdf0e10cSrcweir             compareEnums((Enum)op1, (Enum)op2);
251cdf0e10cSrcweir 
252cdf0e10cSrcweir         else if(op1.getClass() == String.class && op2.getClass() == String.class) // is it a String ?
253cdf0e10cSrcweir             result = ((String)op1).equals((String)op2);
254cdf0e10cSrcweir 
255cdf0e10cSrcweir         else // otherwise it must be a struct
256cdf0e10cSrcweir             result = compareStructs(op1, op2);
257cdf0e10cSrcweir 
258cdf0e10cSrcweir         return result;
259cdf0e10cSrcweir     }
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 
262cdf0e10cSrcweir }
263