Merge commit '8e4e41fa135641b23f68e205832e0696951c5f63'
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / ObjectUtils.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12
13 package org.simantics.databoard.util;
14
15 import java.lang.reflect.Array;\r
16 import java.lang.reflect.Field;\r
17 import java.util.Arrays;\r
18 import java.util.Collection;\r
19 import java.util.HashSet;\r
20 import java.util.Iterator;\r
21 import java.util.Objects;\r
22 import java.util.Set;\r
23
24 /**
25  * ObjectUtils
26  * 
27  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
28  */
29 public class ObjectUtils {
30
31     /**
32      * Equals test for two optional values. 
33      *  
34      * @param o1 obj1 or null
35      * @param o2 obj2 or null
36      * @return true if equal or both null
37      */
38     public static boolean objectEquals(Object o1, Object o2) {
39         if (o1==o2) return true;
40         if (o1==null && o2==null) return true;
41         if (o1==null || o2==null) return false;\r
42         if (o1.getClass().isArray() && o2.getClass().isArray())\r
43             return Objects.deepEquals(o1, o2);
44         return o1.equals(o2);
45     }
46     
47     public static int hashCode(Object obj) {
48         return (obj==null?0:obj.hashCode());
49     }
50
51         public static boolean equals(Collection<?> c1, Collection<?> c2)
52         {
53                 if (c1.size()!=c2.size()) return false;
54                 Iterator<?> i1 = c1.iterator();
55                 Iterator<?> i2 = c2.iterator();
56                 while (i1.hasNext())
57                 {
58                         if (!ObjectUtils.objectEquals(i1.next(), i2.next()))
59                                 return false;
60                 }
61                 return true;
62         }
63         
64         public static String toString(Collection<?> c)
65         {
66                 if (c.isEmpty()) return "[]";
67         StringBuilder sb = new StringBuilder();
68         sb.append("[");
69         int i=0;
70         for (Object o : c)
71         {
72                 if (i++>0) sb.append(", ");
73                 sb.append( o );
74         }
75         sb.append("]");
76         return sb.toString();
77                 
78         }
79         
80         
81         public static String printFieldsDeep(Object o)
82         {
83                 Set<Object> visitedObjects = new HashSet<Object>();
84                 StringBuilder sb = new StringBuilder();
85                 _printFieldsDeep(sb, o, "", visitedObjects);
86                 return sb.toString();
87         }
88         
89         private static void _printFieldsDeep(StringBuilder sb, Object o, String indent, Set<Object> visitedObjects)
90         {
91                 Class<?> c = o.getClass();
92                 sb.append(c.getSimpleName()+" (id="+System.identityHashCode(o)+")\n");
93                 if (!visitedObjects.add(o)) return;
94                 indent += "  ";
95                 while (!c.equals(Object.class)) {
96                         for (Field f : ReflectionUtils.getAllFields(c))
97                         {
98                                 boolean isStatic = (f.getModifiers() & 8) == 8;
99                                 boolean isArray = f.getType().isArray();
100
101                                 if (isStatic) continue;
102                                 f.setAccessible(true);
103                                 sb.append(indent+f.getName()+"=");
104                                 try {
105                                         Object dada = f.get(o);         
106                                         if (dada==null)
107                                                 sb.append("null\n");
108                                         else if (isArray) {
109                                         if (dada instanceof double[]) sb.append( Arrays.toString((double[])dada) +"\n"); 
110                                         else if (dada instanceof boolean[]) sb.append( Arrays.toString((boolean[])dada) +"\n"); 
111                                         else if (dada instanceof byte[]) sb.append( Arrays.toString((byte[])dada) +"\n"); 
112                                         else if (dada instanceof char[]) sb.append( Arrays.toString((char[])dada) +"\n"); 
113                                         else if (dada instanceof float[]) sb.append( Arrays.toString((float[])dada) +"\n"); 
114                                         else if (dada instanceof int[]) sb.append( Arrays.toString((int[])dada) +"\n"); 
115                                         else if (dada instanceof long[]) sb.append( Arrays.toString((long[])dada) +"\n"); 
116                                         else if (dada instanceof short[]) sb.append( Arrays.toString((short[])dada) +"\n");
117                                         else {
118                                                 sb.append(dada.getClass().getComponentType()+"["+Array.getLength(dada)+"]\n");
119                                                 for (int i=0; i<Array.getLength(dada); i++) {
120                                                         Object subDada = Array.get(dada, i);
121                                                         sb.append(indent+"  ["+i+"]=");
122                                                         try{
123                                                         if (subDada.getClass().getCanonicalName().startsWith("java")) {                                                          
124                                                                 sb.append(subDada.toString());
125                                                                 sb.append("\n");
126                                                         } else {
127                                                                 _printFieldsDeep(sb, subDada, indent, visitedObjects);
128                                                         }
129                                                         }
130                                                         catch (Exception e) {
131                                                                         System.out.println("ERROR From ObjectUtils / _printfieldsDeep");
132                                                                         if(subDada == null){
133                                                                                 System.out.println("Subdada null");
134                                                                                 System.out.println(sb.toString());
135                                                                         }
136                                                                 }
137                                                         
138                                                 }
139                                         }
140                                         } else {
141                                         if (dada.getClass().getCanonicalName().startsWith("java")) {
142                                                 sb.append(dada.toString());
143                                                 sb.append("\n");
144                                         } else {
145                                                 _printFieldsDeep(sb, dada, indent, visitedObjects);
146                                         }
147                                         }
148                                         //sb.append("\n");
149                                 } catch (IllegalArgumentException e) {
150                                         sb.append("?");
151                                         continue;
152                                 } catch (IllegalAccessException e) {
153                                         sb.append("?");
154                                         continue;
155                                 }
156                         }
157                         c = c.getSuperclass();          
158                 }
159         }
160         
161         public static String printFields(Object o)
162         {
163                 Class<?> c = o.getClass();
164                 StringBuilder sb = new StringBuilder();
165                 
166                 while (!c.equals(Object.class)) {
167                         for (Field f : c.getDeclaredFields())
168                         {
169                                 boolean isStatic = (f.getModifiers() & 8) == 8;
170                                 boolean isArray = f.getType().isArray();
171
172                                 if (isStatic) continue;
173                                 f.setAccessible(true);
174                                 if (sb.length()>0) sb.append(", ");
175                                 sb.append(f.getName()+"=");
176                                 try {
177                                         Object dada = f.get(o);         
178                                         if (dada==null)
179                                                 sb.append("null");
180                                         else if (isArray) 
181                                                 sb.append(toString(dada));
182                                         else                            
183                                                 sb.append(dada);
184                                 } catch (IllegalArgumentException e) {
185                                         sb.append("?");
186                                         continue;
187                                 } catch (IllegalAccessException e) {
188                                         sb.append("?");
189                                         continue;
190                                 }
191                         }
192                         c = c.getSuperclass();
193                 }
194                 
195                 return sb.toString();
196         }       
197
198     public static String toString(Object obj) {
199         if (obj instanceof Object[]) {            
200             return Arrays.toString((Object[])obj); 
201         }
202         if (obj instanceof double[]) return Arrays.toString((double[])obj); 
203         if (obj instanceof boolean[]) return Arrays.toString((boolean[])obj); 
204         if (obj instanceof byte[]) return Arrays.toString((byte[])obj); 
205         if (obj instanceof char[]) return Arrays.toString((char[])obj); 
206         if (obj instanceof float[]) return Arrays.toString((float[])obj); 
207         if (obj instanceof int[]) return Arrays.toString((int[])obj); 
208         if (obj instanceof long[]) return Arrays.toString((long[])obj); 
209         if (obj instanceof short[]) return Arrays.toString((short[])obj);
210         return obj.toString();
211     }           
212 }